- Newest
- Most votes
- Most comments
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 ;)
Relevant content
- asked 6 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated a year 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 ?