Skip to content

Commit

Permalink
chore: update code
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkuan committed Jan 1, 2024
1 parent 07f3987 commit a347097
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 67 deletions.
18 changes: 4 additions & 14 deletions packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,16 @@ export class LambdaAction implements cloudwatch.IAlarmAction {
*/
bind(_scope: Construct, _alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig {
if (this.lambdaFunction.stack.account === Stack.of(_scope).account) {
new lambda.CfnPermission(_scope, 'AlarmPermission', {
new lambda.CfnPermission(_scope, `${this.lambdaFunction.node.id}AlarmPermission`, {
sourceAccount: Stack.of(_scope).account,
action: 'lambda:InvokeFunction',
sourceArn: _alarm.alarmArn,
principal: 'lambda.alarms.cloudwatch.amazonaws.com',
functionName: this.lambdaFunction.functionName,
});
}
if ((this.lambdaFunction as lambda.IAlias).aliasName) {
return {
alarmActionArn: `arn:${Stack.of(this.lambdaFunction.stack).partition}:lambda:${Stack.of(this.lambdaFunction.stack).region}:${Stack.of(this.lambdaFunction.stack).account}:function:${this.lambdaFunction.functionName}:${(this.lambdaFunction as lambda.IAlias).aliasName}`,
};
} else if ((this.lambdaFunction as lambda.IAlias).version) {
return {
alarmActionArn: `arn:${Stack.of(this.lambdaFunction.stack).partition}:lambda:${Stack.of(this.lambdaFunction.stack).region}:${Stack.of(this.lambdaFunction.stack).account}:function:${this.lambdaFunction.functionName}:${(this.lambdaFunction as lambda.IVersion).version}`,
};
} else {
return {
alarmActionArn: `arn:${Stack.of(this.lambdaFunction.stack).partition}:lambda:${Stack.of(this.lambdaFunction.stack).region}:${Stack.of(this.lambdaFunction.stack).account}:function:${this.lambdaFunction.functionName}`,
};
}
return {
alarmActionArn: this.lambdaFunction.functionArn,
};
}
}
58 changes: 5 additions & 53 deletions packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,9 @@ def handler(event, context):
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
AlarmActions: [
{
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':lambda:',
{
Ref: 'AWS::Region',
},
':',
{
Ref: 'AWS::AccountId',
},
':alarmLambda',
],
'Fn::GetAtt': [
'alarmLambda131DB691',
'Arn',
],
},
],
Expand Down Expand Up @@ -86,24 +72,7 @@ def handler(event, context):
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
AlarmActions: [
{
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':lambda:',
{
Ref: 'AWS::Region',
},
':',
{
Ref: 'AWS::AccountId',
},
':alarmLambda:aliasName',
],
],
Ref: 'alarmLambdaAliasaliasName41B27313',
},
],
});
Expand Down Expand Up @@ -136,24 +105,7 @@ def handler(event, context):
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
AlarmActions: [
{
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':lambda:',
{
Ref: 'AWS::Region',
},
':',
{
Ref: 'AWS::AccountId',
},
':alarmLambda:1',
],
],
Ref: 'alarmLambdaCurrentVersionBDCE825Cf5e98d107ecb420808f3d9421127310e',
},
],
});
Expand Down

0 comments on commit a347097

Please sign in to comment.