15
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS CDKでステージ別のスタックを作る[context編]

Last updated at Posted at 2019-02-08

AWS CDKでステージ別にスタックを作る を書いてからもっといい方法が見つかったので続報。

$ cdk --version
0.23.0 (build 1d705e7)

Contextを使う

cdk synthまたはcdk deployには-c / --contextオプションがつけれます。これを使うことで、ステージやさまざまな値をスタックに流し込めます。

$ npm run build && cdk synth -c stage=development --output output/

Contextの受け取り方

getContext()メソッドで受け取れます。

const app = new cdk.App();
const stage = app.node.getContext('stage')
new TestStack(app, `TestStack-${stage}`, stage)
app.run();

stack内でも使える様子です。

class TestStack extends cdk.Stack {
  constructor(
    scope: cdk.App,
    id: string,
    props?: cdk.StackProps
  ) {
    super(scope, id, props);
    const stage = console.log(this.node.getContext('stage'))
    
    new sqs.Queue(this, `ProjdynamoQueue-${stage}`, {
      visibilityTimeoutSec: 300,
    })
  }
}
15
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?