Skip to content

Commit 857ab7d

Browse files
authored
fix(cli): fix stack monitoring when the stack events do not have phsical resource id set (#27692)
`PhysicalResourceId` for a `StackEvent` is [not required](https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_StackEvent.html) and can be empty. This causes error in stack deployment activty monitoring where we try to monitor nested stacks but empty `PhysicalResourceId` in the `StackEvent` causes it to fail with the error ``` [ValidationError]: 2 validation errors detected: Value '' at 'stackName' failed to satisfy constraint: Member must have length greater than or equal to 1; Value '' at 'stackName' failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z][-a-zA-Z0-9]*|arn:[-a-zA-Z0-9:/._+]* ``` This PR adds a check to avoid that. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 89f4f86 commit 857ab7d

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

packages/aws-cdk/lib/api/util/cloudformation/stack-activity-monitor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ export class StackActivityMonitor {
252252
});
253253

254254
if (event.ResourceType === 'AWS::CloudFormation::Stack' && !CFN_SUCCESS_STATUS.includes(event.ResourceStatus ?? '')) {
255-
// If the event is not for `this` stack, recursively call for events in the nested stack
256-
if (event.PhysicalResourceId !== stackToPollForEvents) {
255+
// If the event is not for `this` stack and has a physical resource Id, recursively call for events in the nested stack
256+
if (event.PhysicalResourceId && event.PhysicalResourceId !== stackToPollForEvents) {
257257
await this.readNewEvents(event.PhysicalResourceId);
258258
}
259259
}

packages/aws-cdk/test/util/stack-monitor.test.ts

+37
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,43 @@ describe('stack monitor, collecting errors from events', () => {
153153
expect(monitor.errors).toStrictEqual(['actual failure error message', 'nested stack failed']);
154154
});
155155

156+
test('does not consider events without physical resource id for monitoring nested stacks', async () => {
157+
const monitor = await testMonitorWithEventCalls([
158+
(request) => {
159+
expect(request.StackName).toStrictEqual('StackName');
160+
return {
161+
StackEvents: [
162+
addErrorToStackEvent(
163+
event(100), {
164+
logicalResourceId: 'nestedStackLogicalResourceId',
165+
physicalResourceId: '',
166+
resourceType: 'AWS::CloudFormation::Stack',
167+
resourceStatusReason: 'nested stack failed',
168+
},
169+
),
170+
],
171+
};
172+
},
173+
(request) => {
174+
// Note that the second call happened for the top level stack instead of a nested stack
175+
expect(request.StackName).toStrictEqual('StackName');
176+
return {
177+
StackEvents: [
178+
addErrorToStackEvent(
179+
event(101), {
180+
logicalResourceId: 'OtherResource',
181+
resourceType: 'Some::Other::Resource',
182+
resourceStatusReason: 'some failure',
183+
},
184+
),
185+
],
186+
};
187+
},
188+
]);
189+
190+
expect(monitor.errors).toStrictEqual(['nested stack failed', 'some failure']);
191+
});
192+
156193
test('does not check for nested stacks that have already completed successfully', async () => {
157194
const monitor = await testMonitorWithEventCalls([
158195
(request) => {

0 commit comments

Comments
 (0)