question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to use deploy-time parameters for privateSubnetIds for Vpc.fromVpcAttributes

See original GitHub issue

Unable to pass CommaDelimitedList CfnParameter to VPC.fromVpcAttributes privateSubnetIds

Reproduction Steps

When trying to use existing VPC for a lambda function

const vpcSubnetId = new CfnParameter(this, 'VPCSubnetId', {
            description: 'VPC Subnet Id',
            type: 'CommaDelimitedList',
});
const vpc = Vpc.fromVpcAttributes(this, 'GeoVPC', {
       vpcId: vpcId.valueAsString,
       availabilityZones: ['eu-west-2a', 'eu-west-2b', 'eu-west-2c'],
       privateSubnetIds: vpcSubnetId.valueAsList,
});

When runing npn run build npx synth for a stack with the above in it it will declare the following error:

Number of privateSubnetIds (1) must be a multiple of availability zones (3).

Environment

  • **CLI Version 😗*1.8.0
  • **Framework Version:**1.8.0
  • **OS 😗*MacOs 10.14.6
  • **Language 😗*typescript

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
wchawscommented, Jan 31, 2021

Here is another nice workaround

const vpcIdParam = new CfnParameter(this, 'YourVpcId', { type: 'AWS::EC2::VPC::Id' });
const pubSubnetsParam = new CfnParameter(this, 'PubSubnets', { type: 'List<AWS::EC2::Subnet::Id>' });
const privSubnetsParam = new CfnParameter(this, 'PrivSubnets', { type: 'List<AWS::EC2::Subnet::Id>' });

const azs = ['eu-west-2a', 'eu-west-2b', 'eu-west-2c'];

const vpc = ec2.Vpc.fromVpcAttributes(this, 'VpcAttr', {
  vpcId: vpcIdParam.valueAsString,
  vpcCidrBlock: Aws.NO_VALUE,
  availabilityZones: azs,
  publicSubnetIds: azs.map((_, index) => Fn.select(index, pubSubnetsParam.valueAsList)),
  privateSubnetIds: azs.map((_, index) => Fn.select(index, privSubnetsParam.valueAsList)),
});
2reactions
eruvanoscommented, Jan 20, 2021

Hi,

we faced this issue multiple times, as a workaround we wrap the subnetIds with select statements:


const vpcSubnetIdsRef = Token.asList(Fn.Split(",", Fn.importValue('VPCSubnetId')))
// or
// const vpcSubnetIdsRef = new CfnParameter(this, 'VPCSubnetId', {
//            description: 'VPC Subnet Id',
//            type: 'CommaDelimitedList',
// });

const azs = ['eu-west-2a', 'eu-west-2b', 'eu-west-2c']

const subnetIds = [];
azs.forEach((_, index) => {
        subnetIds.push(Fn.select(index, vpcSubnetIdsRef))
});

const vpc = Vpc.fromVpcAttributes(this, 'GeoVPC', {
       vpcId: vpcId.valueAsString,
       availabilityZones: azs,
       privateSubnetIds: subnetIds,
});

This will create a list with three tokens, which will be resolved in cfn later.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vpc — AWS Cloud Development Kit 1.184.0 documentation
Import a VPC by supplying all attributes directly. NOTE: using fromVpcAttributes() with deploy-time parameters (like a Fn.importValue() or CfnParameter ...
Read more >
awslabs/aws-cdk - Gitter
Hi all,. I am trying to import an existing VPC created by a different stack into my current stack. From what I have...
Read more >
Importing VPC IDs into a stack with CDK - DEV Community ‍ ‍
When we want to import a VPC ID from another stack using CDK, ... NOTE: using `fromVpcAttributes()` with deploy-time parameters (like a ...
Read more >
AWS CDK, CfnParameter valueAsList: how to pass lists in cmd ...
You are passing the values correctly. CDK shows a single entry but during deployment cloudformation will handle it.
Read more >
Hey CDK, how can I reference existing resources?
If the needed values are available as CloudFormation exports, it's easy to build the VPC Construct: const vpc = Vpc.fromVpcAttributes(this ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found