Sub topic 8
Choosing the target account and region
This chapter assumes you can write a basic CDK stack and run cdk synth. You should also know what an AWS account and a Region are. The chapter explains how a stack gets its target, which is called its environment.
What an environment is
In the CDK, an environment is an account and Region pair. Each stack is deployed to exactly one environment. You set it with the env property when you create the stack.
There are two ways to set it:
- Environment-agnostic: you leave
envout. The stack can be deployed anywhere, but the CDK cannot look up real values at synthesis time. - Environment-specific: you give an account and Region. The CDK knows the target during synthesis, so lookups work and the template can use Region-specific details.
Using the CLI's environment
The first code example in this series used CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION. The CDK CLI sets both variables from your current AWS credentials and profile. The stack then follows whichever profile you deploy with.
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
class AppStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
}
}
const app = new cdk.App();
new AppStack(app, 'AppStack', {
env: {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION,
},
});This is convenient for development. For production, hard-code the values so a deployment can't land in the wrong account by accident.
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
class AppStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
}
}
const app = new cdk.App();
new AppStack(app, 'ProdStack', {
env: { account: '123456789012', region: 'eu-west-1' },
});Deploying one app to several environments
You can create the same stack more than once with different environments. Each stack needs a unique ID.
from aws_cdk import App, Environment, Stack
from constructs import Construct
class AppStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
app = App()
AppStack(app, "AppDev", env=Environment(account="111111111111", region="us-east-1"))
AppStack(app, "AppProd", env=Environment(account="222222222222", region="eu-west-1"))
app.synth()Then deploy one stack by name, for example cdk deploy AppProd.
What changes with a concrete environment
The CDK caches lookup results in cdk.context.json. Commit that file so every teammate and CI run sees the same values.
Common errors
- Deploying to the wrong account. Check the output of
aws sts get-caller-identitybefore you runcdk deploy. - Missing bootstrap. Each account and Region pair you target must be bootstrapped first. The next chapter covers this.
- Lookup failures. A call such as
Vpc.fromLookupfails if the stack has no explicit account and Region.
Exercise
Why might a stack that calls Vpc.fromLookup fail to synthesize when env is omitted?
Show answer
Without env, the stack is environment-agnostic. The CDK doesn't know which account and Region to query during synthesis, so it can't look up the VPC. Setting env to a concrete account and Region fixes it.