Aws-cdk: Parsing types for context values

Created on 9 Apr 2019  路  3Comments  路  Source: aws/aws-cdk

cdk synth works fine if the context values are defined in cdk.json with their corresponding types (numeric / boolean). For example:

{
  "app": "node bin/topdesk.js",
  "context": {
    "max_expected_uri_size": 512,
    "foo": false
  }
}

However, when using the cli flag --context these same context values do not get parsed to any type. They are treated as strings and thus breaks cdk synth --context "foo=false" if a type is expected besides string.

$ cdk synth --context "foo=false"

/Users/ash/Projects/VFZiggo/aws/topdesk/node_modules/@aws-cdk/cdk/lib/runtime.ts:141
      throw new CfnSynthesisError(message);
            ^
While synthesizing TopdeskStack/WAFSizeRestrictionRule: Supplied properties not correct for "CfnRuleProps"
  predicates: element 0: supplied properties not correct for "PredicateProperty"
    negated: "false" should be a boolean
  --- resource created at ---
  at new TopdeskStack (/Users/ash/Projects/VFZiggo/aws/topdesk/lib/topdesk-stack.ts:28:36)
  at Object.<anonymous> (/Users/ash/Projects/VFZiggo/aws/topdesk/bin/topdesk.ts:7:1)
  at Module._compile (module.js:652:30)
  at Object.Module._extensions..js (module.js:663:10)
  at Module.load (module.js:565:32)
  at tryModuleLoad (module.js:505:12)
  at Function.Module._load (module.js:497:3)
  at Function.Module.runMain (module.js:693:10)
  at startup (bootstrap_node.js:188:16)
  at bootstrap_node.js:609:3
  --- problem discovered at ---
    at ValidationResult.assertSuccess (/Users/ash/Projects/VFZiggo/aws/topdesk/node_modules/@aws-cdk/cdk/lib/runtime.ts:141:13)
    at cfnRulePropsToCloudFormation (/Users/ash/Projects/VFZiggo/aws/topdesk/node_modules/@aws-cdk/aws-wafregional/lib/wafregional.generated.ts:405:39)
    at CfnRule.renderProperties (/Users/ash/Projects/VFZiggo/aws/topdesk/node_modules/@aws-cdk/aws-wafregional/lib/wafregional.generated.ts:444:16)
    at CfnRule._toCloudFormation (/Users/ash/Projects/VFZiggo/aws/topdesk/node_modules/@aws-cdk/cdk/lib/cfn-resource.ts:214:31)
    at elements.map.e (/Users/ash/Projects/VFZiggo/aws/topdesk/node_modules/@aws-cdk/cdk/lib/stack.ts:203:63)
    at Array.map (<anonymous>)
    at TopdeskStack._toCloudFormation (/Users/ash/Projects/VFZiggo/aws/topdesk/node_modules/@aws-cdk/cdk/lib/stack.ts:203:34)
    at TopdeskStack.synthesize (/Users/ash/Projects/VFZiggo/aws/topdesk/node_modules/@aws-cdk/cdk/lib/stack.ts:497:44)
    at Synthesizer.synthesize (/Users/ash/Projects/VFZiggo/aws/topdesk/node_modules/@aws-cdk/cdk/lib/synthesis.ts:58:11)
    at App.run (/Users/ash/Projects/VFZiggo/aws/topdesk/node_modules/@aws-cdk/cdk/lib/app.ts:74:27)
Subprocess exited with error 1
bug efformedium managemendevenv needs-reproduction p2 packagtools

Most helpful comment

The types of values passed over the CLI are ambiguous - how do we know you meant true instead of the string "true"? I can think of three options for addressing this:

  1. Support type annotations, e.g. cdk synth --context "foo:boolean=false", or change the --context flag to accept JSON, e.g. cdk synth --context '{"foo": false}
  2. We could implement some heuristics such as attempting to parseInt, parseFloat and parseBool before assuming a value is a string. Doesn't solve the problem as the ambiguity is still there ... we can't assume all "true" strings are of type boolean.
  3. Defensively type-check context parameters where they are used.

All 3 comments

The types of values passed over the CLI are ambiguous - how do we know you meant true instead of the string "true"? I can think of three options for addressing this:

  1. Support type annotations, e.g. cdk synth --context "foo:boolean=false", or change the --context flag to accept JSON, e.g. cdk synth --context '{"foo": false}
  2. We could implement some heuristics such as attempting to parseInt, parseFloat and parseBool before assuming a value is a string. Doesn't solve the problem as the ambiguity is still there ... we can't assume all "true" strings are of type boolean.
  3. Defensively type-check context parameters where they are used.

@sam-goodwin I think both option 2 and 3 can introduce breaking changes.

Option 2: can introduce incorrect type for AWS::ECS::TaskDefinition -> CPU attribute. This attribute can contain an integer value as a string.

Option 3: can prevent existing deployments from deploying (thus breaking change) based on some unforseen reason (I can't think of any right now)

With regards to option 1, type annotation seems like yet another syntax to remember even though it is Typescript it differs from what is in cdk.json.

I think accepting JSON is a nice way of dealing with this. :)

+1 For setting context as JSON as @sam-goodwin suggested.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Kent1 picture Kent1  路  3Comments

kawamoto picture kawamoto  路  3Comments

v-do picture v-do  路  3Comments

ababra picture ababra  路  3Comments

nzspambot picture nzspambot  路  3Comments