Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(appconfig): allow multiple environment monitor roles to be created #27243

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/@aws-cdk/aws-appconfig-alpha/lib/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ export class Environment extends EnvironmentBase {
applicationId: this.applicationId,
name: this.name,
description: this.description,
monitors: this.monitors?.map((monitor) => {
monitors: this.monitors?.map((monitor, index) => {
return {
alarmArn: monitor.alarm.alarmArn,
alarmRoleArn: monitor.alarmRole?.roleArn || this.createAlarmRole(monitor.alarm.alarmArn).roleArn,
alarmRoleArn: monitor.alarmRole?.roleArn || this.createAlarmRole(monitor.alarm.alarmArn, index).roleArn,
};
}),
});
Expand All @@ -256,7 +256,7 @@ export class Environment extends EnvironmentBase {
this.application.addExistingEnvironment(this);
}

private createAlarmRole(alarmArn: string): iam.IRole {
private createAlarmRole(alarmArn: string, index: number): iam.IRole {
const policy = new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['cloudwatch:DescribeAlarms'],
Expand All @@ -265,7 +265,7 @@ export class Environment extends EnvironmentBase {
const document = new iam.PolicyDocument({
statements: [policy],
});
const role = new iam.Role(this, 'Role', {
const role = new iam.Role(this, `Role${index}`, {
roleName: PhysicalName.GENERATE_IF_NEEDED,
assumedBy: new iam.ServicePrincipal('appconfig.amazonaws.com'),
inlinePolicies: {
Expand Down
76 changes: 75 additions & 1 deletion packages/@aws-cdk/aws-appconfig-alpha/test/environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe('environment', () => {
},
AlarmRoleArn: {
'Fn::GetAtt': [
'MyEnvironmentRoleC08961D3',
'MyEnvironmentRole01C8C013F',
'Arn',
],
},
Expand Down Expand Up @@ -177,6 +177,80 @@ describe('environment', () => {
});
});

test('environment with monitors with two alarms', () => {
const stack = new cdk.Stack();
const app = new Application(stack, 'MyAppConfig');
new Environment(stack, 'MyEnvironment', {
name: 'TestEnv',
application: app,
monitors: [
{
alarm: new Alarm(stack, 'Alarm1', {
threshold: 5,
evaluationPeriods: 5,
metric: new Metric(
{
namespace: 'aws',
metricName: 'myMetric',
},
),
}),
},
{
alarm: new Alarm(stack, 'Alarm2', {
threshold: 5,
evaluationPeriods: 5,
metric: new Metric(
{
namespace: 'aws',
metricName: 'myMetric',
},
),
}),
},
],
});

Template.fromStack(stack).resourceCountIs('AWS::CloudWatch::Alarm', 2);
Template.fromStack(stack).resourceCountIs('AWS::IAM::Role', 2);
Template.fromStack(stack).hasResourceProperties('AWS::AppConfig::Environment', {
Name: 'TestEnv',
ApplicationId: {
Ref: 'MyAppConfigB4B63E75',
},
Monitors: [
{
AlarmArn: {
'Fn::GetAtt': [
'Alarm1F9009D71',
'Arn',
],
},
AlarmRoleArn: {
'Fn::GetAtt': [
'MyEnvironmentRole01C8C013F',
'Arn',
],
},
},
{
AlarmArn: {
'Fn::GetAtt': [
'Alarm2A7122E13',
'Arn',
],
},
AlarmRoleArn: {
'Fn::GetAtt': [
'MyEnvironmentRole135A2CEE4',
'Arn',
],
},
},
],
});
});

test('from environment arn', () => {
const stack = new cdk.Stack();
const env = Environment.fromEnvironmentArn(stack, 'MyEnvironment',
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"Threshold": 10
}
},
"MyEnvironmentRoleC08961D3": {
"MyEnvironmentRole01C8C013F": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
Expand Down Expand Up @@ -72,7 +72,7 @@
},
"AlarmRoleArn": {
"Fn::GetAtt": [
"MyEnvironmentRoleC08961D3",
"MyEnvironmentRole01C8C013F",
"Arn"
]
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.