Skip to content

Commit 0eb6c3b

Browse files
authored
fix(cloudwatch-actions): stack partition is hardcoded 'aws' in action arn (#20224)
This removes the hardcoded partition in the ARNs of Alarm Actions for EC2 and SSM. This ensures that these don't unnecessarily break in other non-standard partitions. This uses the ARN of the stack, as done for the region and account. This updates a regular expression in `@aws-cdk/aws-cloudwatch` as well to make sure that EC2 actions are still validated as-expected in GovCloud and other partitions that may support AlarmActions. Closes #19765 ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 5c0d824 commit 0eb6c3b

File tree

6 files changed

+35
-7
lines changed

6 files changed

+35
-7
lines changed

Diff for: packages/@aws-cdk/aws-cloudwatch-actions/lib/ec2.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class Ec2Action implements cloudwatch.IAlarmAction {
4141
* Returns an alarm action configuration to use an EC2 action as an alarm action
4242
*/
4343
bind(_scope: Construct, _alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig {
44-
return { alarmActionArn: `arn:aws:automate:${Stack.of(_scope).region}:ec2:${this.ec2Action}` };
44+
return { alarmActionArn: `arn:${Stack.of(_scope).partition}:automate:${Stack.of(_scope).region}:ec2:${this.ec2Action}` };
4545
}
4646
}
4747

Diff for: packages/@aws-cdk/aws-cloudwatch-actions/lib/ssm.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ export class SsmAction implements cloudwatch.IAlarmAction {
7070
*/
7171
bind(_scope: Construct, _alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig {
7272
if (this.category === undefined) {
73-
return { alarmActionArn: `arn:aws:ssm:${Stack.of(_scope).region}:${Stack.of(_scope).account}:opsitem:${this.severity}` };
73+
return { alarmActionArn: `arn:${Stack.of(_scope).partition}:ssm:${Stack.of(_scope).region}:${Stack.of(_scope).account}:opsitem:${this.severity}` };
7474
} else {
75-
return { alarmActionArn: `arn:aws:ssm:${Stack.of(_scope).region}:${Stack.of(_scope).account}:opsitem:${this.severity}#CATEGORY=${this.category}` };
75+
return { alarmActionArn: `arn:${Stack.of(_scope).partition}:ssm:${Stack.of(_scope).region}:${Stack.of(_scope).account}:opsitem:${this.severity}#CATEGORY=${this.category}` };
7676
}
7777
}
7878
}

Diff for: packages/@aws-cdk/aws-cloudwatch-actions/test/ec2.test.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ test('can use instance reboot as alarm action', () => {
2828
'Fn::Join': [
2929
'',
3030
[
31-
'arn:aws:automate:',
31+
'arn:',
32+
{
33+
Ref: 'AWS::Partition',
34+
},
35+
':automate:',
3236
{
3337
Ref: 'AWS::Region',
3438
},

Diff for: packages/@aws-cdk/aws-cloudwatch-actions/test/ssm.test.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ test('can use ssm with critical severity and performance category as alarm actio
2525
'Fn::Join': [
2626
'',
2727
[
28-
'arn:aws:ssm:',
28+
'arn:',
29+
{
30+
Ref: 'AWS::Partition',
31+
},
32+
':ssm:',
2933
{
3034
Ref: 'AWS::Region',
3135
},
@@ -64,7 +68,11 @@ test('can use ssm with meduim severity and no category as alarm action', () => {
6468
'Fn::Join': [
6569
'',
6670
[
67-
'arn:aws:ssm:',
71+
'arn:',
72+
{
73+
Ref: 'AWS::Partition',
74+
},
75+
':ssm:',
6876
{
6977
Ref: 'AWS::Region',
7078
},

Diff for: packages/@aws-cdk/aws-cloudwatch/lib/alarm.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export class Alarm extends AlarmBase {
245245
}
246246

247247
private validateActionArn(actionArn: string): string {
248-
const ec2ActionsRegexp: RegExp = /arn:aws:automate:[a-z|\d|-]+:ec2:[a-z]+/;
248+
const ec2ActionsRegexp: RegExp = /arn:aws[a-z0-9-]*:automate:[a-z|\d|-]+:ec2:[a-z]+/;
249249
if (ec2ActionsRegexp.test(actionArn)) {
250250
// Check per-instance metric
251251
const metricConfig = this.metric.toMetricConfig();

Diff for: packages/@aws-cdk/aws-cloudwatch/test/alarm.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ describe('Alarm', () => {
4949
}).toThrow(/EC2 alarm actions requires an EC2 Per-Instance Metric. \(.+ does not have an 'InstanceId' dimension\)/);
5050
});
5151

52+
test('non ec2 instance related alarm does not accept EC2 action in other partitions', () => {
53+
const stack = new Stack();
54+
const alarm = new Alarm(stack, 'Alarm', {
55+
metric: testMetric,
56+
threshold: 1000,
57+
evaluationPeriods: 2,
58+
});
59+
60+
expect(() => {
61+
alarm.addAlarmAction(new Ec2TestAlarmAction('arn:aws-us-gov:automate:us-east-1:ec2:reboot'));
62+
}).toThrow(/EC2 alarm actions requires an EC2 Per-Instance Metric. \(.+ does not have an 'InstanceId' dimension\)/);
63+
expect(() => {
64+
alarm.addAlarmAction(new Ec2TestAlarmAction('arn:aws-cn:automate:us-east-1:ec2:reboot'));
65+
}).toThrow(/EC2 alarm actions requires an EC2 Per-Instance Metric. \(.+ does not have an 'InstanceId' dimension\)/);
66+
});
67+
5268
test('can make simple alarm', () => {
5369
// GIVEN
5470
const stack = new Stack();

0 commit comments

Comments
 (0)