AWS CDK, CfnParameter valueAsList: how to pass lists in cmd params

Viewed 605

I can pass a single value, e.g. VPC id like so:

npx cdk deploy --parameters vpcId=vpc-12345

And then access it inside the stack like so:

const vpcId = new cdk.CfnParameter(this, "vpcId", {type: "String"});


const vpc = ec2.Vpc.fromVpcAttributes(this, "myVPC", {
      vpcId: vpcId.valueAsString,
      availabilityZones: [0, 1].map(i => cdk.Fn.select(i, cdk.Fn.getAzs('ap-southeast-2'))),
      publicSubnetIds: publicSubnetIds.valueAsList, // ["subn1", "subn2"]
      privateSubnetIds: privateSubnetIds.valueAsList
    });

But when I do:

npx cdk deploy --parameters publicSubnetIds=subnet0001,subnet0002

And then:

const publicSubnetIds = new cdk.CfnParameter(this, "publicSubnetIds", {type: "List<String>"})

I get a single entry in the result [ '#{Token[TOKEN.62]}' ] if I do publicSubnetIds.getValueAsList, not an array of two values as expected.

How do I pass multiple values in cli parameters, such as array of subnet Ids?

1 Answers

You are passing the values correctly. CDK shows a single entry but during deployment cloudformation will handle it.

I remember I faced some issue when we kept type as List<String> for subnetIds and securityGroupIds. You need to keep the types as mentioned below.

this.subnetIds = new CfnParameter(this.stack, 'SubnetIds', {
            type: 'List<AWS::EC2::Subnet::Id>',
        }).valueAsList;
        
this.securityGroupIds = new CfnParameter(this.stack, 'SecurityGroupIds', {
            type: 'List<AWS::EC2::SecurityGroup::Id>',
        }).valueAsList;
Related