Skip to content

Commit

Permalink
fix by the review
Browse files Browse the repository at this point in the history
change test name

fix trailing-spaces
  • Loading branch information
go-to-k committed Jan 24, 2024
1 parent 5e4ec23 commit ad877ac
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ export class LambdaAction implements cloudwatch.IAlarmAction {
*
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricAlarm.html
*/
bind(_scope: Construct, _alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig {
const idPrefix = FeatureFlags.of(_scope).isEnabled(LAMBDA_PERMISSION_LOGICAL_ID_FOR_LAMBDA_ACTION) ? _alarm.node.id : '';
bind(scope: Construct, alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig {
const idPrefix = FeatureFlags.of(scope).isEnabled(LAMBDA_PERMISSION_LOGICAL_ID_FOR_LAMBDA_ACTION) ? alarm.node.id : '';
this.lambdaFunction.addPermission(`${idPrefix}AlarmPermission`, {
sourceAccount: Stack.of(_scope).account,
sourceAccount: Stack.of(scope).account,
action: 'lambda:InvokeFunction',
sourceArn: _alarm.alarmArn,
sourceArn: alarm.alarmArn,
principal: new iam.ServicePrincipal('lambda.alarms.cloudwatch.amazonaws.com'),
});

Expand Down
34 changes: 34 additions & 0 deletions packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,38 @@ def handler(event, context):
// THEN
Template.fromStack(stack).resourceCountIs('AWS::CloudWatch::Alarm', 2);
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Permission', 2);
});

test('throws when multiple alarms are created for the same lambda if feature flag is set to false', () => {
// GIVEN
const stack = new Stack();
stack.node.setContext(LAMBDA_PERMISSION_LOGICAL_ID_FOR_LAMBDA_ACTION, false); // Default, but explicit just in case.
const alarm1 = new cloudwatch.Alarm(stack, 'Alarm1', {
metric: new cloudwatch.Metric({ namespace: 'AWS', metricName: 'Test' }),
evaluationPeriods: 3,
threshold: 100,
});
const alarm2 = new cloudwatch.Alarm(stack, 'Alarm2', {
metric: new cloudwatch.Metric({ namespace: 'AWS', metricName: 'Test2' }),
evaluationPeriods: 3,
threshold: 100,
});

// WHEN
const alarmLambda = new lambda.Function(stack, 'alarmLambda', {
runtime: lambda.Runtime.PYTHON_3_12,
functionName: 'alarmLambda',
code: lambda.Code.fromInline(`
def handler(event, context):
print('event:', event)
print('.............................................')
print('context:', context)`),
handler: 'index.handler',
});
alarm1.addAlarmAction(new actions.LambdaAction(alarmLambda));

// THEN
expect(() => {
alarm2.addAlarmAction(new actions.LambdaAction(alarmLambda));
}).toThrow(/There is already a Construct with name 'AlarmPermission' in Function \[alarmLambda\]/);
});

0 comments on commit ad877ac

Please sign in to comment.