Slide 24
Slide 24 text
24
contextによる環境分離
{
"app": "npx ts-node --prefer-ts-exts bin/test.ts",
...
"context": {
"projectName": "testproject",
"dev": {
"accountId": "111122223333",
"envName": "dev",
"region": "ap-northeast-1"
},
"stg": {
"accountId": "111122223333",
"envName": "dev",
"region": "ap-northeast-1"
},
"prd": {
"accountId": "444455556666",
"envName": "prd",
"region": "ap-northeast-1"
},
...
}
}
cdk.json
const app = new cdk.App();
const projectName = app.node.tryGetContext('projectName');
const envKey = app.node.tryGetContext('env');
const envValues = app.node.tryGetContext(envKey);
const env = {
account: envValues.accountId,
region: envValues.region,
};
new TestStack(app, `${projectName}-${envValues.envName}-stack`, {
projectName: projectName,
envName: envValues.envName,
env,
});
bin/test.ts
export interface props extends cdk.StackProps {
projectName: string;
envName: string;
}
export class TestStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: props) {
super(scope, id, props);
new ec2.Vpc(this, 'Vpc', {
vpcName: `${props.projectName}-${props.envName}-vpc`,
});
}
}
lib/test-stack.ts
> cdk deploy –c env=dev
envValuesでdevのオブジェクトを扱える