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 Answer
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
EXPERT
answered 4 months ago
  • 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 ?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions