Skip to content

Commit

Permalink
fix(appconfig): allow multiple environment monitor roles to be created
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjane-dev committed Sep 21, 2023
1 parent c695b60 commit 1aea938
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 29 deletions.
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "32.0.0",
"version": "34.0.0",
"files": {
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
"source": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"version": "32.0.0",
"version": "34.0.0",
"files": {
"5a83c94c795349286c703ba61add79efe69c74c7adb8de98527fcf034b9683af": {
"610cbf4701a1d09b46d0f5dcddb700815ab9758d826bae0d0a0435cefc679345": {
"source": {
"path": "aws-appconfig-environment.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "5a83c94c795349286c703ba61add79efe69c74c7adb8de98527fcf034b9683af.json",
"objectKey": "610cbf4701a1d09b46d0f5dcddb700815ab9758d826bae0d0a0435cefc679345.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"Threshold": 10
}
},
"MyEnvironmentRoleC08961D3": {
"MyEnvironmentRole2EA31107E16C4": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
Expand Down Expand Up @@ -72,7 +72,7 @@
},
"AlarmRoleArn": {
"Fn::GetAtt": [
"MyEnvironmentRoleC08961D3",
"MyEnvironmentRole2EA31107E16C4",
"Arn"
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"32.0.0"}
{"version":"34.0.0"}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "32.0.0",
"version": "34.0.0",
"testCases": {
"appconfig-environment/DefaultTest": {
"stacks": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "32.0.0",
"version": "34.0.0",
"artifacts": {
"aws-appconfig-environment.assets": {
"type": "cdk:asset-manifest",
Expand All @@ -17,7 +17,7 @@
"validateOnSynth": false,
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/5a83c94c795349286c703ba61add79efe69c74c7adb8de98527fcf034b9683af.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/610cbf4701a1d09b46d0f5dcddb700815ab9758d826bae0d0a0435cefc679345.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down Expand Up @@ -45,10 +45,10 @@
"data": "MyAlarm696658B6"
}
],
"/aws-appconfig-environment/MyEnvironment/Role/Resource": [
"/aws-appconfig-environment/MyEnvironment/Role2EA31/Resource": [
{
"type": "aws:cdk:logicalId",
"data": "MyEnvironmentRoleC08961D3"
"data": "MyEnvironmentRole2EA31107E16C4"
}
],
"/aws-appconfig-environment/MyEnvironment/Resource": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}
},
"constructInfo": {
"fqn": "aws-cdk-lib.Resource",
"fqn": "@aws-cdk/aws-appconfig-alpha.Application",
"version": "0.0.0"
}
},
Expand Down Expand Up @@ -66,21 +66,21 @@
"id": "MyEnvironment",
"path": "aws-appconfig-environment/MyEnvironment",
"children": {
"Role": {
"id": "Role",
"path": "aws-appconfig-environment/MyEnvironment/Role",
"Role2EA31": {
"id": "Role2EA31",
"path": "aws-appconfig-environment/MyEnvironment/Role2EA31",
"children": {
"ImportRole": {
"id": "ImportRole",
"path": "aws-appconfig-environment/MyEnvironment/Role/ImportRole",
"ImportRole2EA31": {
"id": "ImportRole2EA31",
"path": "aws-appconfig-environment/MyEnvironment/Role2EA31/ImportRole2EA31",
"constructInfo": {
"fqn": "aws-cdk-lib.Resource",
"version": "0.0.0"
}
},
"Resource": {
"id": "Resource",
"path": "aws-appconfig-environment/MyEnvironment/Role/Resource",
"path": "aws-appconfig-environment/MyEnvironment/Role2EA31/Resource",
"attributes": {
"aws:cdk:cloudformation:type": "AWS::IAM::Role",
"aws:cdk:cloudformation:props": {
Expand Down Expand Up @@ -149,7 +149,7 @@
},
"alarmRoleArn": {
"Fn::GetAtt": [
"MyEnvironmentRoleC08961D3",
"MyEnvironmentRole2EA31107E16C4",
"Arn"
]
}
Expand All @@ -165,7 +165,7 @@
}
},
"constructInfo": {
"fqn": "aws-cdk-lib.Resource",
"fqn": "@aws-cdk/aws-appconfig-alpha.Environment",
"version": "0.0.0"
}
},
Expand Down Expand Up @@ -204,7 +204,7 @@
"path": "appconfig-environment/DefaultTest/Default",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.2.69"
"version": "10.2.70"
}
},
"DeployAssert": {
Expand Down Expand Up @@ -250,7 +250,7 @@
"path": "Tree",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.2.69"
"version": "10.2.70"
}
}
},
Expand Down

0 comments on commit 1aea938

Please sign in to comment.