How to configure multiple route53 configuration for prod and non-prod in cdk.context.json

0

I am new in AWS I was trying to do context configuration for route53 in my cdk.context.json As following: cdk.context.json

{
    "hosted-zone:account=21342543:domainName=prod.demo.com:region=ap-southeast-2": {
        "Id": "/hostedzone/FDSRGSTRKA",
        "Name": "prod.demo.com"
    },
    "hosted-zone:account=22675345:domainName=nprod.demo.com:region=ap-southeast-2": {
        "Id": "/hostedzone/FDSRGSGGRS",
        "Name": "nprod.demo.com"
    }
}

and in the code base, I was doing something like this:

 export class Animation extends cdk.Stack{
  constructor(scope: Construct, id:string, props: StackProperties){
       super(scope, id, props)
   }
}

Is this will work when I push these changes to prod? If not what is the correct way in this scenario?

1개 답변
1

In your case, you're using context to define hosted zones for different environments. The configuration in cdk.context.json seems fine, but when referencing these values in your CDK code, you need to use the correct keys. The key in your context should be hosted-zone:account=<account>:domainName=<domain>:region=<region>.

In your CDK code, you can retrieve the hosted zone configuration for a specific environment using the context values:

This is a typescript

import * as cdk from 'aws-cdk-lib';
import { Construct, Stack, StackProps } from 'aws-cdk-lib';
import { HostedZone } from 'aws-cdk-lib/aws-route53';

export class Animation extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const hostedZoneConfigKey = `hosted-zone:account=<account>:domainName=<domain>:region=<region>`;
    const hostedZoneConfig = this.node.tryGetContext(hostedZoneConfigKey);

    if (!hostedZoneConfig) {
      throw new Error(`Hosted zone configuration not found for key: ${hostedZoneConfigKey}`);
    }

    new HostedZone(this, 'MyHostedZone', {
      hostedZoneId: hostedZoneConfig.Id,
      zoneName: hostedZoneConfig.Name,
    });

    // ... rest of your stack
  }
}

Make sure to replace <account>, <domain>, and <region> with the actual values

When deploying to different environments (prod, non-prod), you need to ensure that the correct values are set in your cdk.context.json for each environment. eg

{
  "hosted-zone:account=<prod-account>:domainName=prod.demo.com:region=<prod-region>": {
    "Id": "/hostedzone/FDSRGSTRKA",
    "Name": "prod.demo.com"
  },
  "hosted-zone:account=<non-prod-account>:domainName=nprod.demo.com:region=<non-prod-region>": {
    "Id": "/hostedzone/FDSRGSGGRS",
    "Name": "nprod.demo.com"
  }
}

Make sure to replace <prod-account>, <non-prod-account>, <prod-region>, and <non-prod-region> with the actual values for your production and non-production environments.

This way, your CDK application can dynamically select the correct hosted zone configuration based on the environment during deployment

Hope it clarifies and if does I would appreciate answer to be accepted so that community can benefit for clarity, thanks ;)

profile picture
전문가
답변함 3달 전
  • Thanks @Debolek for your answer. But in my existing code base we are not calling tryGetContext method anywhere it has been called internally (I can see this in generate output templates), so do we really need all those steps ? Can’t it just pickup the correct configuration when I perform deployment in prod or nonprod ?

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠