-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Description
What is the problem?
If you have a stack where you need to look up an existing VPC (using VPC.fromLookup
) and add a VPC Interface Endpoint to it using a subnet selection, it will fail if the cdk.context.json
file does not exist or does not contain information about that VPC already.
Reproduction Steps
Assumptions:
- Referencing VPC & Subnets that already exist
Steps to Reproduce:
-
Populate Account, Subnet Ids, and VPC Id
-
Ensure
cdk.context.json
either doesn't exist, or at least does not contain information matching your VPC -
Run
cdk ls
-
Verify error occurs
Cannot create a VPC Endpoint with no subnets Subprocess exited with error 1
Workaround:
- Comment out VPC Interface Endpoint code
- Run
cdk ls
which should create/update thecdk.context.json
- Uncomment out VPC Interface Endpoint code
- Run
cdk ls
and it should work
Code:
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from '@aws-cdk/core';
import {Construct, Stack, StackProps} from "@aws-cdk/core";
import {InterfaceVpcEndpointAwsService, SubnetFilter, Vpc} from "@aws-cdk/aws-ec2";
const app = new cdk.App();
interface MyStackProps extends StackProps {
readonly subnetIds: string[];
readonly vpcId: string;
}
class MyStack extends Stack {
constructor(scope: Construct, id: string, props: MyStackProps) {
super(scope, id, props);
const vpc = Vpc.fromLookup(this, 'Vpc', {
vpcId: props.vpcId,
});
const secretsManagerEndpoint = vpc.addInterfaceEndpoint('SecretsManagerEndpoint', {
service: InterfaceVpcEndpointAwsService.SECRETS_MANAGER,
subnets: {
subnetFilters: [SubnetFilter.byIds(props.subnetIds)],
},
});
secretsManagerEndpoint.connections.allowDefaultPortFromAnyIpv4();
}
}
new MyStack(app, 'MyStack', {
env: {
account: 'xxx',
region: 'us-east-1',
},
subnetIds: [
'xxx',
'xxx'
],
vpcId: 'xxx'
});
What did you expect to happen?
I should be able to run CDK commands without an error occurring and without having to comment out portions of my stack in order to generate/update the cdk.context.json
file.
What actually happened?
All CDK commands fail with an error and the cdk.context.json
file is not generated/updated.
CDK CLI Version
1.132.0
Framework Version
No response
Node.js Version
v14.17.5
OS
macOS 11.5.1
Language
Typescript
Language Version
TypeScript (3.9.10)
Other information
The error that you will see after running any CDK command is:
Cannot create a VPC Endpoint with no subnets
Subprocess exited with error 1
Reference implementation: https://github.com/sdobberstein/cdk-vpc-issue