Skip to content

Commit 0fb7ea3

Browse files
authored
fix(cli): Genertate CDK Metadata resource for region-independent stacks (#3149)
Fixes a bug that caused the CDK Metadata resource to not be emitted if a stack was not bound to a particular supported region. The fixed behavior will generate the resource for supported regions as well as for region independent stacks. Fixes #3142
1 parent 65a3a53 commit 0fb7ea3

File tree

3 files changed

+73
-28
lines changed

3 files changed

+73
-28
lines changed

packages/aws-cdk/lib/api/cxapp/stacks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export class AppStacks {
223223
if (!stack.template.Resources) {
224224
stack.template.Resources = {};
225225
}
226-
const resourcePresent = stack.environment.region === 'default-region'
226+
const resourcePresent = stack.environment.region === cxapi.UNKNOWN_REGION
227227
|| regionInfo.Fact.find(stack.environment.region, regionInfo.FactName.CDK_METADATA_RESOURCE_AVAILABLE) === 'YES';
228228
if (resourcePresent) {
229229
if (!stack.template.Resources.CDKMetadata) {

packages/aws-cdk/test/api/test.stacks.ts

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
11
import cxapi = require('@aws-cdk/cx-api');
2-
import { Test } from 'nodeunit';
2+
import { Test, testCase } from 'nodeunit';
33
import { SDK } from '../../lib';
44
import { AppStacks, DefaultSelection } from '../../lib/api/cxapp/stacks';
55
import { Configuration } from '../../lib/settings';
66
import { testAssembly } from '../util';
77

8-
const FIXED_RESULT = testAssembly({
9-
stackName: 'withouterrors',
10-
template: { resource: 'noerrorresource' },
11-
},
12-
{
13-
stackName: 'witherrors',
14-
template: { resource: 'errorresource' },
15-
metadata: {
16-
'/resource': [
17-
{
18-
type: cxapi.ERROR_METADATA_KEY,
19-
data: 'this is an error'
20-
}
21-
]
22-
},
23-
});
24-
25-
export = {
8+
export = testCase({
269
async 'do not throw when selecting stack without errors'(test: Test) {
2710
// GIVEN
2811
const stacks = testStacks();
@@ -95,14 +78,75 @@ export = {
9578
// THEN
9679
test.ok(thrown && thrown.includes('Since this app includes more than a single stack, specify which stacks to use (wildcards are supported)'));
9780
test.done();
98-
}
81+
},
82+
83+
'AWS::CDK::Metadata': {
84+
async 'is generated for relocatable stacks'(test: Test) {
85+
const stacks = testStacks({ env: `aws://${cxapi.UNKNOWN_ACCOUNT}/${cxapi.UNKNOWN_REGION}`, versionReporting: true });
86+
87+
const result = await stacks.synthesizeStack('withouterrors');
88+
const metadata = result.template.Resources && result.template.Resources.CDKMetadata;
89+
test.deepEqual(metadata, {
90+
Type: 'AWS::CDK::Metadata',
91+
Properties: {
92+
Modules: `${require('../../package.json').name}=${require('../../package.json').version}`
93+
}
94+
});
95+
96+
test.done();
97+
},
98+
99+
async 'is generated for stacks in supported regions'(test: Test) {
100+
const stacks = testStacks({ env: 'aws://012345678912/us-east-1', versionReporting: true });
101+
102+
const result = await stacks.synthesizeStack('withouterrors');
103+
const metadata = result.template.Resources && result.template.Resources.CDKMetadata;
104+
test.deepEqual(metadata, {
105+
Type: 'AWS::CDK::Metadata',
106+
Properties: {
107+
Modules: `${require('../../package.json').name}=${require('../../package.json').version}`
108+
}
109+
});
110+
111+
test.done();
112+
},
113+
114+
async 'is not generated for stacks in unsupported regions'(test: Test) {
115+
const stacks = testStacks({ env: 'aws://012345678912/bermuda-triangle-1337', versionReporting: true });
116+
117+
const result = await stacks.synthesizeStack('withouterrors');
118+
const metadata = result.template.Resources && result.template.Resources.CDKMetadata;
119+
test.equal(metadata, undefined);
120+
121+
test.done();
122+
}
123+
},
124+
});
99125

100-
};
126+
function testStacks({ env, versionReporting = true }: { env?: string, versionReporting?: boolean } = {}) {
127+
const configuration = new Configuration();
128+
configuration.settings.set(['versionReporting'], versionReporting);
101129

102-
function testStacks() {
103130
return new AppStacks({
104-
configuration: new Configuration(),
131+
configuration,
105132
aws: new SDK(),
106-
synthesizer: async () => FIXED_RESULT,
133+
synthesizer: async () => testAssembly({
134+
stackName: 'withouterrors',
135+
env,
136+
template: { resource: 'noerrorresource' },
137+
},
138+
{
139+
stackName: 'witherrors',
140+
env,
141+
template: { resource: 'errorresource' },
142+
metadata: {
143+
'/resource': [
144+
{
145+
type: cxapi.ERROR_METADATA_KEY,
146+
data: 'this is an error'
147+
}
148+
]
149+
},
150+
}),
107151
});
108-
}
152+
}

packages/aws-cdk/test/util.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path = require('path');
55
export interface TestStackArtifact {
66
stackName: string;
77
template: any;
8+
env?: string,
89
depends?: string[];
910
metadata?: cxapi.StackMetadata;
1011
assets?: cxapi.AssetMetadataEntry[];
@@ -27,7 +28,7 @@ export function testAssembly(...stacks: TestStackArtifact[]): cxapi.CloudAssembl
2728

2829
builder.addArtifact(stack.stackName, {
2930
type: cxapi.ArtifactType.AWS_CLOUDFORMATION_STACK,
30-
environment: 'aws://12345/here',
31+
environment: stack.env || 'aws://12345/here',
3132

3233
dependencies: stack.depends,
3334
metadata,
@@ -43,4 +44,4 @@ export function testAssembly(...stacks: TestStackArtifact[]): cxapi.CloudAssembl
4344
export function testStack(stack: TestStackArtifact) {
4445
const assembly = testAssembly(stack);
4546
return assembly.getStack(stack.stackName);
46-
}
47+
}

0 commit comments

Comments
 (0)