Skip to content

Commit ad877ac

Browse files
committed
fix by the review
change test name fix trailing-spaces
1 parent 5e4ec23 commit ad877ac

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ export class LambdaAction implements cloudwatch.IAlarmAction {
2121
*
2222
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricAlarm.html
2323
*/
24-
bind(_scope: Construct, _alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig {
25-
const idPrefix = FeatureFlags.of(_scope).isEnabled(LAMBDA_PERMISSION_LOGICAL_ID_FOR_LAMBDA_ACTION) ? _alarm.node.id : '';
24+
bind(scope: Construct, alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig {
25+
const idPrefix = FeatureFlags.of(scope).isEnabled(LAMBDA_PERMISSION_LOGICAL_ID_FOR_LAMBDA_ACTION) ? alarm.node.id : '';
2626
this.lambdaFunction.addPermission(`${idPrefix}AlarmPermission`, {
27-
sourceAccount: Stack.of(_scope).account,
27+
sourceAccount: Stack.of(scope).account,
2828
action: 'lambda:InvokeFunction',
29-
sourceArn: _alarm.alarmArn,
29+
sourceArn: alarm.alarmArn,
3030
principal: new iam.ServicePrincipal('lambda.alarms.cloudwatch.amazonaws.com'),
3131
});
3232

packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,38 @@ def handler(event, context):
144144
// THEN
145145
Template.fromStack(stack).resourceCountIs('AWS::CloudWatch::Alarm', 2);
146146
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Permission', 2);
147+
});
148+
149+
test('throws when multiple alarms are created for the same lambda if feature flag is set to false', () => {
150+
// GIVEN
151+
const stack = new Stack();
152+
stack.node.setContext(LAMBDA_PERMISSION_LOGICAL_ID_FOR_LAMBDA_ACTION, false); // Default, but explicit just in case.
153+
const alarm1 = new cloudwatch.Alarm(stack, 'Alarm1', {
154+
metric: new cloudwatch.Metric({ namespace: 'AWS', metricName: 'Test' }),
155+
evaluationPeriods: 3,
156+
threshold: 100,
157+
});
158+
const alarm2 = new cloudwatch.Alarm(stack, 'Alarm2', {
159+
metric: new cloudwatch.Metric({ namespace: 'AWS', metricName: 'Test2' }),
160+
evaluationPeriods: 3,
161+
threshold: 100,
162+
});
163+
164+
// WHEN
165+
const alarmLambda = new lambda.Function(stack, 'alarmLambda', {
166+
runtime: lambda.Runtime.PYTHON_3_12,
167+
functionName: 'alarmLambda',
168+
code: lambda.Code.fromInline(`
169+
def handler(event, context):
170+
print('event:', event)
171+
print('.............................................')
172+
print('context:', context)`),
173+
handler: 'index.handler',
174+
});
175+
alarm1.addAlarmAction(new actions.LambdaAction(alarmLambda));
176+
177+
// THEN
178+
expect(() => {
179+
alarm2.addAlarmAction(new actions.LambdaAction(alarmLambda));
180+
}).toThrow(/There is already a Construct with name 'AlarmPermission' in Function \[alarmLambda\]/);
147181
});

0 commit comments

Comments
 (0)