Skip to content

Commit 9312c97

Browse files
authored
fix(appconfig): allow multiple environment monitor roles to be created (#27243)
Currently, when adding multiple monitors under an environment, users are seeing the following error ``` Error: There is already a Construct with name 'Role' in Environment [MyEnvironment] ``` Before the logical id for any monitor role that is created was just `Role`. In this change, we use the monitor index in the logical id so there aren't any repeats in logical id's. This is also why there is a change in the integ test snapshot, since the logical id was refactored. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 3196733 commit 9312c97

9 files changed

+103
-29
lines changed

packages/@aws-cdk/aws-appconfig-alpha/lib/environment.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ export class Environment extends EnvironmentBase {
236236
applicationId: this.applicationId,
237237
name: this.name,
238238
description: this.description,
239-
monitors: this.monitors?.map((monitor) => {
239+
monitors: this.monitors?.map((monitor, index) => {
240240
return {
241241
alarmArn: monitor.alarm.alarmArn,
242-
alarmRoleArn: monitor.alarmRole?.roleArn || this.createAlarmRole(monitor.alarm.alarmArn).roleArn,
242+
alarmRoleArn: monitor.alarmRole?.roleArn || this.createAlarmRole(monitor.alarm.alarmArn, index).roleArn,
243243
};
244244
}),
245245
});
@@ -256,7 +256,7 @@ export class Environment extends EnvironmentBase {
256256
this.application.addExistingEnvironment(this);
257257
}
258258

259-
private createAlarmRole(alarmArn: string): iam.IRole {
259+
private createAlarmRole(alarmArn: string, index: number): iam.IRole {
260260
const policy = new iam.PolicyStatement({
261261
effect: iam.Effect.ALLOW,
262262
actions: ['cloudwatch:DescribeAlarms'],
@@ -265,7 +265,7 @@ export class Environment extends EnvironmentBase {
265265
const document = new iam.PolicyDocument({
266266
statements: [policy],
267267
});
268-
const role = new iam.Role(this, 'Role', {
268+
const role = new iam.Role(this, `Role${index}`, {
269269
roleName: PhysicalName.GENERATE_IF_NEEDED,
270270
assumedBy: new iam.ServicePrincipal('appconfig.amazonaws.com'),
271271
inlinePolicies: {

packages/@aws-cdk/aws-appconfig-alpha/test/environment.test.ts

+75-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ describe('environment', () => {
147147
},
148148
AlarmRoleArn: {
149149
'Fn::GetAtt': [
150-
'MyEnvironmentRoleC08961D3',
150+
'MyEnvironmentRole01C8C013F',
151151
'Arn',
152152
],
153153
},
@@ -177,6 +177,80 @@ describe('environment', () => {
177177
});
178178
});
179179

180+
test('environment with monitors with two alarms', () => {
181+
const stack = new cdk.Stack();
182+
const app = new Application(stack, 'MyAppConfig');
183+
new Environment(stack, 'MyEnvironment', {
184+
name: 'TestEnv',
185+
application: app,
186+
monitors: [
187+
{
188+
alarm: new Alarm(stack, 'Alarm1', {
189+
threshold: 5,
190+
evaluationPeriods: 5,
191+
metric: new Metric(
192+
{
193+
namespace: 'aws',
194+
metricName: 'myMetric',
195+
},
196+
),
197+
}),
198+
},
199+
{
200+
alarm: new Alarm(stack, 'Alarm2', {
201+
threshold: 5,
202+
evaluationPeriods: 5,
203+
metric: new Metric(
204+
{
205+
namespace: 'aws',
206+
metricName: 'myMetric',
207+
},
208+
),
209+
}),
210+
},
211+
],
212+
});
213+
214+
Template.fromStack(stack).resourceCountIs('AWS::CloudWatch::Alarm', 2);
215+
Template.fromStack(stack).resourceCountIs('AWS::IAM::Role', 2);
216+
Template.fromStack(stack).hasResourceProperties('AWS::AppConfig::Environment', {
217+
Name: 'TestEnv',
218+
ApplicationId: {
219+
Ref: 'MyAppConfigB4B63E75',
220+
},
221+
Monitors: [
222+
{
223+
AlarmArn: {
224+
'Fn::GetAtt': [
225+
'Alarm1F9009D71',
226+
'Arn',
227+
],
228+
},
229+
AlarmRoleArn: {
230+
'Fn::GetAtt': [
231+
'MyEnvironmentRole01C8C013F',
232+
'Arn',
233+
],
234+
},
235+
},
236+
{
237+
AlarmArn: {
238+
'Fn::GetAtt': [
239+
'Alarm2A7122E13',
240+
'Arn',
241+
],
242+
},
243+
AlarmRoleArn: {
244+
'Fn::GetAtt': [
245+
'MyEnvironmentRole135A2CEE4',
246+
'Arn',
247+
],
248+
},
249+
},
250+
],
251+
});
252+
});
253+
180254
test('from environment arn', () => {
181255
const stack = new cdk.Stack();
182256
const env = Environment.fromEnvironmentArn(stack, 'MyEnvironment',

packages/@aws-cdk/aws-appconfig-alpha/test/integ.environment.js.snapshot/appconfigenvironmentDefaultTestDeployAssert75BD28E7.assets.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-appconfig-alpha/test/integ.environment.js.snapshot/aws-appconfig-environment.assets.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-appconfig-alpha/test/integ.environment.js.snapshot/aws-appconfig-environment.template.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"Threshold": 10
1919
}
2020
},
21-
"MyEnvironmentRoleC08961D3": {
21+
"MyEnvironmentRole01C8C013F": {
2222
"Type": "AWS::IAM::Role",
2323
"Properties": {
2424
"AssumeRolePolicyDocument": {
@@ -72,7 +72,7 @@
7272
},
7373
"AlarmRoleArn": {
7474
"Fn::GetAtt": [
75-
"MyEnvironmentRoleC08961D3",
75+
"MyEnvironmentRole01C8C013F",
7676
"Arn"
7777
]
7878
}

packages/@aws-cdk/aws-appconfig-alpha/test/integ.environment.js.snapshot/cdk.out

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-appconfig-alpha/test/integ.environment.js.snapshot/integ.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-appconfig-alpha/test/integ.environment.js.snapshot/manifest.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-appconfig-alpha/test/integ.environment.js.snapshot/tree.json

+12-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)