Skip to content

Commit cf2e3f6

Browse files
RomainMullerElad Ben-Israel
authored andcommitted
fix(cli): conversion of "tags" filter for EC2 DescribeVpcs call (#3393)
When in the `Filters` parameter of the `DescribeVpcs` call, the tags need to be encoded as `{ Name: "tag:<name>", Values: ["<value>"] }`, but the `tag:` prefix was not added by the preparation code. Fixes #3372
1 parent 1409a88 commit cf2e3f6

File tree

8 files changed

+78
-5
lines changed

8 files changed

+78
-5
lines changed

packages/@aws-cdk/aws-backup/.npmignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ dist
1616
.LAST_PACKAGE
1717
.jsii
1818

19-
*.tsbuildinfo
19+
*.tsbuildinfo
20+
21+
# Include .jsii
22+
!.jsii

packages/@aws-cdk/aws-ec2/lib/vpc.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ export class Vpc extends VpcBase {
685685
* Import an existing VPC from by querying the AWS environment this stack is deployed to.
686686
*/
687687
public static fromLookup(scope: Construct, id: string, options: VpcLookupOptions): IVpc {
688-
const filter: {[key: string]: string} = options.tags || {};
688+
const filter: {[key: string]: string} = makeTagFilter(options.tags);
689689

690690
// We give special treatment to some tags
691691
if (options.vpcId) { filter['vpc-id'] = options.vpcId; }
@@ -701,6 +701,17 @@ export class Vpc extends VpcBase {
701701
});
702702

703703
return this.fromVpcAttributes(scope, id, attributes);
704+
705+
/**
706+
* Prefixes all keys in the argument with `tag:`.`
707+
*/
708+
function makeTagFilter(tags: { [name: string]: string } | undefined): { [name: string]: string } {
709+
const result: { [name: string]: string } = {};
710+
for (const [name, value] of Object.entries(tags || {})) {
711+
result[`tag:${name}`] = value;
712+
}
713+
return result;
714+
}
704715
}
705716

706717
/**

packages/@aws-cdk/aws-medialive/.npmignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ dist
1616
.LAST_PACKAGE
1717
.jsii
1818

19-
*.tsbuildinfo
19+
*.tsbuildinfo
20+
21+
# Include .jsii
22+
!.jsii

packages/@aws-cdk/aws-securityhub/.npmignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ dist
1616
.LAST_PACKAGE
1717
.jsii
1818

19-
*.tsbuildinfo
19+
*.tsbuildinfo
20+
21+
# Include .jsii
22+
!.jsii

packages/aws-cdk/lib/context-providers/vpcs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class VpcNetworkContextProviderPlugin implements ContextProviderPlugin {
2222

2323
private async findVpc(ec2: AWS.EC2, args: cxapi.VpcContextQuery): Promise<string> {
2424
// Build request filter (map { Name -> Value } to list of [{ Name, Values }])
25-
const filters: AWS.EC2.Filter[] = Object.entries(args.filter).map(x => ({ Name: x[0], Values: [x[1]] }));
25+
const filters: AWS.EC2.Filter[] = Object.entries(args.filter).map(([tag, value]) => ({ Name: tag, Values: [value] }));
2626

2727
debug(`Listing VPCs in ${args.account}:${args.region}`);
2828
const response = await ec2.describeVpcs({ Filters: filters }).promise();

packages/aws-cdk/test/integ/cli/app/app.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path');
22
const cdk = require('@aws-cdk/core');
3+
const ec2 = require('@aws-cdk/aws-ec2');
34
const ssm = require('@aws-cdk/aws-ssm');
45
const iam = require('@aws-cdk/aws-iam');
56
const sns = require('@aws-cdk/aws-sns');
@@ -101,6 +102,28 @@ class DockerStack extends cdk.Stack {
101102
}
102103
}
103104

105+
const VPC_TAG_NAME = 'custom-tag';
106+
const VPC_TAG_VALUE = 'bazinga!';
107+
108+
class DefineVpcStack extends cdk.Stack {
109+
constructor(parent, id, props) {
110+
super(parent, id, props);
111+
112+
new ec2.Vpc(this, 'VPC', {
113+
maxAzs: 1,
114+
}).node.applyAspect(new cdk.Tag(VPC_TAG_NAME, VPC_TAG_VALUE));
115+
}
116+
}
117+
118+
class ImportVpcStack extends cdk.Stack {
119+
constructor(parent, id, props) {
120+
super(parent, id, props);
121+
122+
ec2.Vpc.fromLookup(this, 'DefaultVPC', { isDefault: true });
123+
ec2.Vpc.fromLookup(this, 'ByTag', { tags: { [VPC_TAG_NAME]: VPC_TAG_VALUE } });
124+
}
125+
}
126+
104127
const stackPrefix = process.env.STACK_NAME_PREFIX || 'cdk-toolkit-integration';
105128

106129
const app = new cdk.App();
@@ -123,4 +146,12 @@ new MissingSSMParameterStack(app, `${stackPrefix}-missing-ssm-parameter`, { env:
123146
new LambdaStack(app, `${stackPrefix}-lambda`);
124147
new DockerStack(app, `${stackPrefix}-docker`);
125148

149+
if (process.env.ENABLE_VPC_TESTING) { // Gating so we don't do context fetching unless that's what we are here for
150+
const env = { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION };
151+
if (process.env.ENABLE_VPC_TESTING === 'DEFINE')
152+
new DefineVpcStack(app, `${stackPrefix}-define-vpc`, { env });
153+
if (process.env.ENABLE_VPC_TESTING === 'IMPORT')
154+
new ImportVpcStack(app, `${stackPrefix}-import-vpc`, { env });
155+
}
156+
126157
app.synth();

packages/aws-cdk/test/integ/cli/common.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ function setup() {
8282
install_dep @aws-cdk/aws-lambda
8383
install_dep @aws-cdk/aws-ssm
8484
install_dep @aws-cdk/aws-ecr-assets
85+
install_dep @aws-cdk/aws-ec2
8586

8687
echo "| setup complete at: $PWD"
8788
echo "| 'cdk' is: $(which cdk)"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
scriptdir=$(cd $(dirname $0) && pwd)
4+
source ${scriptdir}/common.bash
5+
# ----------------------------------------------------------
6+
7+
setup
8+
9+
echo "Setting up: creating a VPC with known tags"
10+
ENABLE_VPC_TESTING="DEFINE" cdk deploy ${STACK_NAME_PREFIX}-define-vpc
11+
echo "Setup complete!"
12+
13+
# verify we can synth the importing stack now
14+
echo "Verifying we can now import that VPC"
15+
ENABLE_VPC_TESTING="IMPORT" cdk synth -v ${STACK_NAME_PREFIX}-import-vpc
16+
17+
# destroy
18+
echo "Cleaning up..."
19+
ENABLE_VPC_TESTING="DEFINE" cdk destroy -f ${STACK_NAME_PREFIX}-define-vpc
20+
21+
echo "✅ success"

0 commit comments

Comments
 (0)