Aws-cdk: Cannot set context at app level after v1.9.0

Created on 3 Oct 2019  路  5Comments  路  Source: aws/aws-cdk


Cannot set context at app level anymore after v1.9.0. This is because a Tree child is automatically created under the app and context cannot be set after children have been added.

Comes from #4194 (3cca03d).

Reproduction Steps

const app = new cdk.App();
app.node.setContext('stage', 'dev');

Error Log


Error: Cannot set context after children have been added: Tree

Environment

  • CLI Version : > 1.9.0
  • Framework Version: > 1.9.0
  • OS : all
  • Language : all

Other


This is :bug: Bug Report

bug needs-triage

Most helpful comment

You can specify the context when you initialise the app:

new App({ context: { key: 'value' });

Closing for now.

All 5 comments

You can specify the context when you initialise the app:

new App({ context: { key: 'value' });

Closing for now.

How would you set the context if not already set by the CLI?

I was doing:

const app = new cdk.App();

// Get and set stage context
const stage = app.node.tryGetContext('stage') || process.env.STAGE || 'dev';
app.node.setContext('stage', stage);

This should work since the CLI context is applied after the initializer鈥檚:

new App({
  context: { stage: process.env.STAGE || 'dev' }
})

Reference: https://github.com/aws/aws-cdk/blob/934d36fed04bdaf24332fc5d38aaee663c6f7347/packages/%40aws-cdk/core/lib/app.ts#L149

Let me know if this works.

It works. Thanks 馃憤

(could be documented)

@eladb In the case of setting context when we initialize the app, how should the nested structures be handled? Context is expecting [key: string]: string which doesn't allow setting a key to a nested object. Trying to set the context for js unit test.

Was this page helpful?
0 / 5 - 0 ratings