From 7429bcafed82b48e12ff3030b7c21bd43f47532d Mon Sep 17 00:00:00 2001 From: neilkuan Date: Sun, 24 Dec 2023 11:46:03 +0000 Subject: [PATCH 01/22] feat: cloudwatch alarm lambda action --- .../aws-cloudwatch-actions/lib/lambda.ts | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts new file mode 100644 index 0000000000000..a7c4eb7e996e8 --- /dev/null +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts @@ -0,0 +1,47 @@ +import { Construct } from 'constructs'; +import * as cloudwatch from '../../aws-cloudwatch'; +import * as lambda from '../../aws-lambda'; +import { Stack } from '../../core'; + +/** + * Use an Lambda action as an Alarm action + */ +export class LambdaAction implements cloudwatch.IAlarmAction { + private lambdaFunction: lambda.IAlias | lambda.IVersion + constructor( + lambdaFunction: lambda.IAlias | lambda.IVersion, + ) { + this.lambdaFunction = lambdaFunction; + } + + /** + * Returns an alarm action configuration to use an Lambda action as an alarm action + * + * @see: https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricAlarm.html + * + */ + bind(_scope: Construct, _alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig { + if (this.lambdaFunction.stack.account === Stack.of(_scope).account) { + new lambda.CfnPermission(_scope, '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.IAlias).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}`, + }; + } + } +} From 07f3987b30aa8c8759b13f46f592b7a7e07a8909 Mon Sep 17 00:00:00 2001 From: neilkuan Date: Sun, 24 Dec 2023 14:28:52 +0000 Subject: [PATCH 02/22] chore: add testing --- .../aws-cloudwatch-actions/README.md | 11 ++ .../aws-cloudwatch-actions/lib/index.ts | 1 + .../aws-cloudwatch-actions/lib/lambda.ts | 6 +- .../test/lambda.test.ts | 161 ++++++++++++++++++ 4 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md b/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md index 5da3f83ebcab3..e2acb85953368 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md @@ -40,4 +40,15 @@ alarm.addAlarmAction( ); ``` +## Lambda Action Example + +```ts +declare const alarm: cloudwatch.Alarm; +declare const lambda: lambda.Function; + +alarm.addAlarmAction( + new actions.LambdaAction(lambda) +); +``` + See `aws-cdk-lib/aws-cloudwatch` for more information. diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/index.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/index.ts index 191da008b23a9..32e159bb4e918 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/index.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/index.ts @@ -3,3 +3,4 @@ export * from './autoscaling'; export * from './sns'; export * from './ec2'; export * from './ssm'; +export * from './lambda'; \ No newline at end of file diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts index a7c4eb7e996e8..83f43b826b854 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts @@ -7,9 +7,9 @@ import { Stack } from '../../core'; * Use an Lambda action as an Alarm action */ export class LambdaAction implements cloudwatch.IAlarmAction { - private lambdaFunction: lambda.IAlias | lambda.IVersion + private lambdaFunction: lambda.IAlias | lambda.IVersion | lambda.IFunction constructor( - lambdaFunction: lambda.IAlias | lambda.IVersion, + lambdaFunction: lambda.IAlias | lambda.IVersion | lambda.IFunction, ) { this.lambdaFunction = lambdaFunction; } @@ -36,7 +36,7 @@ export class LambdaAction implements cloudwatch.IAlarmAction { }; } 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.IAlias).version}`, + 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 { diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts new file mode 100644 index 0000000000000..15bfa6fe41dd8 --- /dev/null +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts @@ -0,0 +1,161 @@ +import { Template } from '../../assertions'; +import * as cloudwatch from '../../aws-cloudwatch'; +import * as lambda from '../../aws-lambda'; +import { Stack } from '../../core'; +import * as actions from '../lib/index'; + +test('can use lambda as alarm action', () => { + // GIVEN + const stack = new Stack(); + const alarm = new cloudwatch.Alarm(stack, 'Alarm', { + metric: new cloudwatch.Metric({ + namespace: 'AWS', + metricName: 'Test', + }), + 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', + }); + alarm.addAlarmAction(new actions.LambdaAction(alarmLambda)); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', { + AlarmActions: [ + { + 'Fn::Join': [ + '', + [ + 'arn:', + { + Ref: 'AWS::Partition', + }, + ':lambda:', + { + Ref: 'AWS::Region', + }, + ':', + { + Ref: 'AWS::AccountId', + }, + ':alarmLambda', + ], + ], + }, + ], + }); +}); + +test('can use lambda alias as alarm action', () => { + // GIVEN + const stack = new Stack(); + const alarm = new cloudwatch.Alarm(stack, 'Alarm', { + metric: new cloudwatch.Metric({ + namespace: 'AWS', + metricName: 'Test', + }), + 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', + }); + const aliasName = alarmLambda.addAlias('aliasName'); + alarm.addAlarmAction(new actions.LambdaAction(aliasName)); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', { + AlarmActions: [ + { + 'Fn::Join': [ + '', + [ + 'arn:', + { + Ref: 'AWS::Partition', + }, + ':lambda:', + { + Ref: 'AWS::Region', + }, + ':', + { + Ref: 'AWS::AccountId', + }, + ':alarmLambda:aliasName', + ], + ], + }, + ], + }); +}); + +test('can use lambda version as alarm action', () => { + // GIVEN + const stack = new Stack(); + const alarm = new cloudwatch.Alarm(stack, 'Alarm', { + metric: new cloudwatch.Metric({ namespace: 'AWS', metricName: 'Test' }), + 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', + }); + const version = alarmLambda.currentVersion; + alarm.addAlarmAction(new actions.LambdaAction(version)); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', { + AlarmActions: [ + { + 'Fn::Join': [ + '', + [ + 'arn:', + { + Ref: 'AWS::Partition', + }, + ':lambda:', + { + Ref: 'AWS::Region', + }, + ':', + { + Ref: 'AWS::AccountId', + }, + ':alarmLambda:1', + ], + ], + }, + ], + }); +}); + From a347097b6076fbaeff3c3ea08930faa55bac6f2e Mon Sep 17 00:00:00 2001 From: neilkuan Date: Mon, 1 Jan 2024 04:36:33 +0000 Subject: [PATCH 03/22] chore: update code --- .../aws-cloudwatch-actions/lib/lambda.ts | 18 ++---- .../test/lambda.test.ts | 58 ++----------------- 2 files changed, 9 insertions(+), 67 deletions(-) diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts index 83f43b826b854..26079c2c818b2 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts @@ -22,7 +22,7 @@ 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, @@ -30,18 +30,8 @@ export class LambdaAction implements cloudwatch.IAlarmAction { 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, + }; } } diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts index 15bfa6fe41dd8..03e71b40b0cbb 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts @@ -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', ], }, ], @@ -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', }, ], }); @@ -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', }, ], }); From 0fefaed4a74e4dae316b3e0a276aeadabba6d057 Mon Sep 17 00:00:00 2001 From: neilkuan Date: Mon, 1 Jan 2024 05:21:39 +0000 Subject: [PATCH 04/22] chore: add integ testing --- ...efaultTestDeployAssertF9F882E5.assets.json | 19 + ...aultTestDeployAssertF9F882E5.template.json | 36 ++ ...larmActionIntegrationTestStack.assets.json | 19 + ...rmActionIntegrationTestStack.template.json | 288 ++++++++++ .../cdk.out | 1 + .../integ.json | 12 + .../manifest.json | 167 ++++++ .../tree.json | 519 ++++++++++++++++++ .../test/integ.lambda-alarm-action.ts | 55 ++ 9 files changed, 1116 insertions(+) create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/cdk.out create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/integ.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets.json new file mode 100644 index 0000000000000..2372b822099ba --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json new file mode 100644 index 0000000000000..2428455f27dd1 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.0", + "files": { + "3ecf35f5c1e42717d8d3492d07d7d8fbb935ba2e7961453e11b81fc5c0469ae2": { + "source": { + "path": "LambdaAlarmActionIntegrationTestStack.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "3ecf35f5c1e42717d8d3492d07d7d8fbb935ba2e7961453e11b81fc5c0469ae2.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json new file mode 100644 index 0000000000000..427a04557a8da --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json @@ -0,0 +1,288 @@ +{ + "Resources": { + "inAlarmLambdaServiceRole970DE64C": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "inAlarmLambda0920D101": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "ZipFile": "\ndef handler(event, context):\n make this fn fail\n prinr22(''hello)" + }, + "FunctionName": "inAlarmLambda", + "Handler": "index.handler", + "Role": { + "Fn::GetAtt": [ + "inAlarmLambdaServiceRole970DE64C", + "Arn" + ] + }, + "Runtime": "python3.12" + }, + "DependsOn": [ + "inAlarmLambdaServiceRole970DE64C" + ] + }, + "Alarm7103F465": { + "Type": "AWS::CloudWatch::Alarm", + "Properties": { + "ActionsEnabled": true, + "AlarmActions": [ + { + "Ref": "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a" + }, + { + "Ref": "alarmLambdaAliasaliasName41B27313" + }, + { + "Fn::GetAtt": [ + "alarmLambda131DB691", + "Arn" + ] + } + ], + "ComparisonOperator": "GreaterThanThreshold", + "Dimensions": [ + { + "Name": "FunctionName", + "Value": { + "Ref": "inAlarmLambda0920D101" + } + } + ], + "EvaluationPeriods": 1, + "MetricName": "Errors", + "Namespace": "AWS/Lambda", + "Period": 60, + "Statistic": "Sum", + "Threshold": 1, + "TreatMissingData": "notBreaching" + } + }, + "AlarmCurrentVersionAlarmPermission22DB4772": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Fn::Join": [ + "", + [ + { + "Ref": "alarmLambda131DB691" + }, + ":", + { + "Fn::GetAtt": [ + "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a", + "Version" + ] + } + ] + ] + }, + "Principal": "lambda.alarms.cloudwatch.amazonaws.com", + "SourceAccount": { + "Ref": "AWS::AccountId" + }, + "SourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "AlarmAliasaliasNameAlarmPermission3887D390": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Fn::Join": [ + "", + [ + { + "Fn::Select": [ + 6, + { + "Fn::Split": [ + ":", + { + "Ref": "alarmLambdaAliasaliasName41B27313" + } + ] + } + ] + }, + ":aliasName" + ] + ] + }, + "Principal": "lambda.alarms.cloudwatch.amazonaws.com", + "SourceAccount": { + "Ref": "AWS::AccountId" + }, + "SourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "AlarmalarmLambdaAlarmPermission5CAF3DE7": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Ref": "alarmLambda131DB691" + }, + "Principal": "lambda.alarms.cloudwatch.amazonaws.com", + "SourceAccount": { + "Ref": "AWS::AccountId" + }, + "SourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "alarmLambdaServiceRoleCDAABB9D": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "alarmLambda131DB691": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "ZipFile": "\ndef handler(event, context):\n print('event:', event)\n print('.............................................')\n print('context:', context)" + }, + "FunctionName": "alarmLambda", + "Handler": "index.handler", + "Role": { + "Fn::GetAtt": [ + "alarmLambdaServiceRoleCDAABB9D", + "Arn" + ] + }, + "Runtime": "python3.12" + }, + "DependsOn": [ + "alarmLambdaServiceRoleCDAABB9D" + ] + }, + "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a": { + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "alarmLambda131DB691" + } + } + }, + "alarmLambdaAliasaliasName41B27313": { + "Type": "AWS::Lambda::Alias", + "Properties": { + "FunctionName": { + "Ref": "alarmLambda131DB691" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a", + "Version" + ] + }, + "Name": "aliasName" + } + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/cdk.out new file mode 100644 index 0000000000000..1f0068d32659a --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"36.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/integ.json new file mode 100644 index 0000000000000..0d675b8e2e251 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "36.0.0", + "testCases": { + "LambdaAlarmActionIntegrationTest/DefaultTest": { + "stacks": [ + "LambdaAlarmActionIntegrationTestStack" + ], + "assertionStack": "LambdaAlarmActionIntegrationTest/DefaultTest/DeployAssert", + "assertionStackName": "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json new file mode 100644 index 0000000000000..fd185515bc5c1 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json @@ -0,0 +1,167 @@ +{ + "version": "36.0.0", + "artifacts": { + "LambdaAlarmActionIntegrationTestStack.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "LambdaAlarmActionIntegrationTestStack.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "LambdaAlarmActionIntegrationTestStack": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "LambdaAlarmActionIntegrationTestStack.template.json", + "terminationProtection": false, + "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}/3ecf35f5c1e42717d8d3492d07d7d8fbb935ba2e7961453e11b81fc5c0469ae2.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "LambdaAlarmActionIntegrationTestStack.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "LambdaAlarmActionIntegrationTestStack.assets" + ], + "metadata": { + "/LambdaAlarmActionIntegrationTestStack/inAlarmLambda/ServiceRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "inAlarmLambdaServiceRole970DE64C" + } + ], + "/LambdaAlarmActionIntegrationTestStack/inAlarmLambda/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "inAlarmLambda0920D101" + } + ], + "/LambdaAlarmActionIntegrationTestStack/Alarm/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "Alarm7103F465" + } + ], + "/LambdaAlarmActionIntegrationTestStack/Alarm/CurrentVersionAlarmPermission": [ + { + "type": "aws:cdk:logicalId", + "data": "AlarmCurrentVersionAlarmPermission22DB4772" + } + ], + "/LambdaAlarmActionIntegrationTestStack/Alarm/AliasaliasNameAlarmPermission": [ + { + "type": "aws:cdk:logicalId", + "data": "AlarmAliasaliasNameAlarmPermission3887D390" + } + ], + "/LambdaAlarmActionIntegrationTestStack/Alarm/alarmLambdaAlarmPermission": [ + { + "type": "aws:cdk:logicalId", + "data": "AlarmalarmLambdaAlarmPermission5CAF3DE7" + } + ], + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/ServiceRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "alarmLambdaServiceRoleCDAABB9D" + } + ], + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "alarmLambda131DB691" + } + ], + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a" + } + ], + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "alarmLambdaAliasaliasName41B27313" + } + ], + "/LambdaAlarmActionIntegrationTestStack/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/LambdaAlarmActionIntegrationTestStack/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "LambdaAlarmActionIntegrationTestStack" + }, + "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.template.json", + "terminationProtection": false, + "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}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets" + ], + "metadata": { + "/LambdaAlarmActionIntegrationTest/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/LambdaAlarmActionIntegrationTest/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "LambdaAlarmActionIntegrationTest/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json new file mode 100644 index 0000000000000..a5b31290cea22 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json @@ -0,0 +1,519 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "LambdaAlarmActionIntegrationTestStack": { + "id": "LambdaAlarmActionIntegrationTestStack", + "path": "LambdaAlarmActionIntegrationTestStack", + "children": { + "inAlarmLambda": { + "id": "inAlarmLambda", + "path": "LambdaAlarmActionIntegrationTestStack/inAlarmLambda", + "children": { + "ServiceRole": { + "id": "ServiceRole", + "path": "LambdaAlarmActionIntegrationTestStack/inAlarmLambda/ServiceRole", + "children": { + "ImportServiceRole": { + "id": "ImportServiceRole", + "path": "LambdaAlarmActionIntegrationTestStack/inAlarmLambda/ServiceRole/ImportServiceRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "LambdaAlarmActionIntegrationTestStack/inAlarmLambda/ServiceRole/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "assumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "managedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnRole", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "LambdaAlarmActionIntegrationTestStack/inAlarmLambda/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Function", + "aws:cdk:cloudformation:props": { + "code": { + "zipFile": "\ndef handler(event, context):\n make this fn fail\n prinr22(''hello)" + }, + "functionName": "inAlarmLambda", + "handler": "index.handler", + "role": { + "Fn::GetAtt": [ + "inAlarmLambdaServiceRole970DE64C", + "Arn" + ] + }, + "runtime": "python3.12" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnFunction", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.FunctionBase", + "version": "0.0.0" + } + }, + "Alarm": { + "id": "Alarm", + "path": "LambdaAlarmActionIntegrationTestStack/Alarm", + "children": { + "Resource": { + "id": "Resource", + "path": "LambdaAlarmActionIntegrationTestStack/Alarm/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::CloudWatch::Alarm", + "aws:cdk:cloudformation:props": { + "actionsEnabled": true, + "alarmActions": [ + { + "Ref": "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a" + }, + { + "Ref": "alarmLambdaAliasaliasName41B27313" + }, + { + "Fn::GetAtt": [ + "alarmLambda131DB691", + "Arn" + ] + } + ], + "comparisonOperator": "GreaterThanThreshold", + "dimensions": [ + { + "name": "FunctionName", + "value": { + "Ref": "inAlarmLambda0920D101" + } + } + ], + "evaluationPeriods": 1, + "metricName": "Errors", + "namespace": "AWS/Lambda", + "period": 60, + "statistic": "Sum", + "threshold": 1, + "treatMissingData": "notBreaching" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudwatch.CfnAlarm", + "version": "0.0.0" + } + }, + "CurrentVersionAlarmPermission": { + "id": "CurrentVersionAlarmPermission", + "path": "LambdaAlarmActionIntegrationTestStack/Alarm/CurrentVersionAlarmPermission", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Fn::Join": [ + "", + [ + { + "Ref": "alarmLambda131DB691" + }, + ":", + { + "Fn::GetAtt": [ + "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a", + "Version" + ] + } + ] + ] + }, + "principal": "lambda.alarms.cloudwatch.amazonaws.com", + "sourceAccount": { + "Ref": "AWS::AccountId" + }, + "sourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } + }, + "AliasaliasNameAlarmPermission": { + "id": "AliasaliasNameAlarmPermission", + "path": "LambdaAlarmActionIntegrationTestStack/Alarm/AliasaliasNameAlarmPermission", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Fn::Join": [ + "", + [ + { + "Fn::Select": [ + 6, + { + "Fn::Split": [ + ":", + { + "Ref": "alarmLambdaAliasaliasName41B27313" + } + ] + } + ] + }, + ":aliasName" + ] + ] + }, + "principal": "lambda.alarms.cloudwatch.amazonaws.com", + "sourceAccount": { + "Ref": "AWS::AccountId" + }, + "sourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } + }, + "alarmLambdaAlarmPermission": { + "id": "alarmLambdaAlarmPermission", + "path": "LambdaAlarmActionIntegrationTestStack/Alarm/alarmLambdaAlarmPermission", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Ref": "alarmLambda131DB691" + }, + "principal": "lambda.alarms.cloudwatch.amazonaws.com", + "sourceAccount": { + "Ref": "AWS::AccountId" + }, + "sourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudwatch.Alarm", + "version": "0.0.0" + } + }, + "alarmLambda": { + "id": "alarmLambda", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda", + "children": { + "ServiceRole": { + "id": "ServiceRole", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/ServiceRole", + "children": { + "ImportServiceRole": { + "id": "ImportServiceRole", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/ServiceRole/ImportServiceRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/ServiceRole/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "assumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "managedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnRole", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Function", + "aws:cdk:cloudformation:props": { + "code": { + "zipFile": "\ndef handler(event, context):\n print('event:', event)\n print('.............................................')\n print('context:', context)" + }, + "functionName": "alarmLambda", + "handler": "index.handler", + "role": { + "Fn::GetAtt": [ + "alarmLambdaServiceRoleCDAABB9D", + "Arn" + ] + }, + "runtime": "python3.12" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnFunction", + "version": "0.0.0" + } + }, + "CurrentVersion": { + "id": "CurrentVersion", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion", + "children": { + "Resource": { + "id": "Resource", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Version", + "aws:cdk:cloudformation:props": { + "functionName": { + "Ref": "alarmLambda131DB691" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnVersion", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.Version", + "version": "0.0.0" + } + }, + "AliasaliasName": { + "id": "AliasaliasName", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName", + "children": { + "Resource": { + "id": "Resource", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Alias", + "aws:cdk:cloudformation:props": { + "functionName": { + "Ref": "alarmLambda131DB691" + }, + "functionVersion": { + "Fn::GetAtt": [ + "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a", + "Version" + ] + }, + "name": "aliasName" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnAlias", + "version": "0.0.0" + } + }, + "ScalingRole": { + "id": "ScalingRole", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/ScalingRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.Alias", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.FunctionBase", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "LambdaAlarmActionIntegrationTestStack/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "LambdaAlarmActionIntegrationTestStack/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "LambdaAlarmActionIntegrationTest": { + "id": "LambdaAlarmActionIntegrationTest", + "path": "LambdaAlarmActionIntegrationTest", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "LambdaAlarmActionIntegrationTest/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "LambdaAlarmActionIntegrationTest/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "LambdaAlarmActionIntegrationTest/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "LambdaAlarmActionIntegrationTest/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "LambdaAlarmActionIntegrationTest/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", + "version": "0.0.0" + } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts new file mode 100644 index 0000000000000..6755a13eb1289 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts @@ -0,0 +1,55 @@ +import { App, Stack, StackProps, Duration } from 'aws-cdk-lib'; +import * as integ from '@aws-cdk/integ-tests-alpha'; +import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch'; +import * as lambda from 'aws-cdk-lib/aws-lambda'; +import * as cloudwatchActions from 'aws-cdk-lib/aws-cloudwatch-actions'; + +class LambdaAlarmActionIntegrationTestStack extends Stack { + + constructor(scope: App, id: string, props?: StackProps) { + super(scope, id, props); + const inAlarmLambda = new lambda.Function(this, 'inAlarmLambda', { + runtime: lambda.Runtime.PYTHON_3_12, + functionName: 'inAlarmLambda', + code: lambda.Code.fromInline(` +def handler(event, context): + make this fn fail + prinr22(''hello)`), + handler: 'index.handler', + }); + const alarm = new cloudwatch.Alarm(this, 'Alarm', { + comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD, + threshold: 1, + evaluationPeriods: 1, + metric: inAlarmLambda.metricErrors({ period: Duration.minutes(1) }), + actionsEnabled: true, + treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING, + }); + + const alarmLambda = new lambda.Function(this, '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', + }); + const version = alarmLambda.currentVersion; + const aliasName = alarmLambda.addAlias('aliasName'); + alarm.addAlarmAction(new cloudwatchActions.LambdaAction(version)); + alarm.addAlarmAction(new cloudwatchActions.LambdaAction(aliasName)); + alarm.addAlarmAction(new cloudwatchActions.LambdaAction(alarmLambda)); + } +} + +const app = new App(); + +const stack = new LambdaAlarmActionIntegrationTestStack(app, 'LambdaAlarmActionIntegrationTestStack'); + +new integ.IntegTest(app, 'LambdaAlarmActionIntegrationTest', { + testCases: [stack], +}); + +app.synth(); From b8958339e6f3de1b7812aed6d1d244c88fa44e50 Mon Sep 17 00:00:00 2001 From: neilkuan Date: Sun, 24 Dec 2023 11:46:03 +0000 Subject: [PATCH 05/22] feat: cloudwatch alarm lambda action --- .../aws-cloudwatch-actions/lib/lambda.ts | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts new file mode 100644 index 0000000000000..a7c4eb7e996e8 --- /dev/null +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts @@ -0,0 +1,47 @@ +import { Construct } from 'constructs'; +import * as cloudwatch from '../../aws-cloudwatch'; +import * as lambda from '../../aws-lambda'; +import { Stack } from '../../core'; + +/** + * Use an Lambda action as an Alarm action + */ +export class LambdaAction implements cloudwatch.IAlarmAction { + private lambdaFunction: lambda.IAlias | lambda.IVersion + constructor( + lambdaFunction: lambda.IAlias | lambda.IVersion, + ) { + this.lambdaFunction = lambdaFunction; + } + + /** + * Returns an alarm action configuration to use an Lambda action as an alarm action + * + * @see: https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricAlarm.html + * + */ + bind(_scope: Construct, _alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig { + if (this.lambdaFunction.stack.account === Stack.of(_scope).account) { + new lambda.CfnPermission(_scope, '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.IAlias).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}`, + }; + } + } +} From 6cbfe8bfffec1d0406b8d0577f492f3df7d32982 Mon Sep 17 00:00:00 2001 From: neilkuan Date: Sun, 24 Dec 2023 14:28:52 +0000 Subject: [PATCH 06/22] chore: add testing --- .../aws-cloudwatch-actions/README.md | 11 ++ .../aws-cloudwatch-actions/lib/index.ts | 1 + .../aws-cloudwatch-actions/lib/lambda.ts | 6 +- .../test/lambda.test.ts | 161 ++++++++++++++++++ 4 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md b/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md index 5da3f83ebcab3..e2acb85953368 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md @@ -40,4 +40,15 @@ alarm.addAlarmAction( ); ``` +## Lambda Action Example + +```ts +declare const alarm: cloudwatch.Alarm; +declare const lambda: lambda.Function; + +alarm.addAlarmAction( + new actions.LambdaAction(lambda) +); +``` + See `aws-cdk-lib/aws-cloudwatch` for more information. diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/index.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/index.ts index 191da008b23a9..32e159bb4e918 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/index.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/index.ts @@ -3,3 +3,4 @@ export * from './autoscaling'; export * from './sns'; export * from './ec2'; export * from './ssm'; +export * from './lambda'; \ No newline at end of file diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts index a7c4eb7e996e8..83f43b826b854 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts @@ -7,9 +7,9 @@ import { Stack } from '../../core'; * Use an Lambda action as an Alarm action */ export class LambdaAction implements cloudwatch.IAlarmAction { - private lambdaFunction: lambda.IAlias | lambda.IVersion + private lambdaFunction: lambda.IAlias | lambda.IVersion | lambda.IFunction constructor( - lambdaFunction: lambda.IAlias | lambda.IVersion, + lambdaFunction: lambda.IAlias | lambda.IVersion | lambda.IFunction, ) { this.lambdaFunction = lambdaFunction; } @@ -36,7 +36,7 @@ export class LambdaAction implements cloudwatch.IAlarmAction { }; } 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.IAlias).version}`, + 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 { diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts new file mode 100644 index 0000000000000..15bfa6fe41dd8 --- /dev/null +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts @@ -0,0 +1,161 @@ +import { Template } from '../../assertions'; +import * as cloudwatch from '../../aws-cloudwatch'; +import * as lambda from '../../aws-lambda'; +import { Stack } from '../../core'; +import * as actions from '../lib/index'; + +test('can use lambda as alarm action', () => { + // GIVEN + const stack = new Stack(); + const alarm = new cloudwatch.Alarm(stack, 'Alarm', { + metric: new cloudwatch.Metric({ + namespace: 'AWS', + metricName: 'Test', + }), + 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', + }); + alarm.addAlarmAction(new actions.LambdaAction(alarmLambda)); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', { + AlarmActions: [ + { + 'Fn::Join': [ + '', + [ + 'arn:', + { + Ref: 'AWS::Partition', + }, + ':lambda:', + { + Ref: 'AWS::Region', + }, + ':', + { + Ref: 'AWS::AccountId', + }, + ':alarmLambda', + ], + ], + }, + ], + }); +}); + +test('can use lambda alias as alarm action', () => { + // GIVEN + const stack = new Stack(); + const alarm = new cloudwatch.Alarm(stack, 'Alarm', { + metric: new cloudwatch.Metric({ + namespace: 'AWS', + metricName: 'Test', + }), + 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', + }); + const aliasName = alarmLambda.addAlias('aliasName'); + alarm.addAlarmAction(new actions.LambdaAction(aliasName)); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', { + AlarmActions: [ + { + 'Fn::Join': [ + '', + [ + 'arn:', + { + Ref: 'AWS::Partition', + }, + ':lambda:', + { + Ref: 'AWS::Region', + }, + ':', + { + Ref: 'AWS::AccountId', + }, + ':alarmLambda:aliasName', + ], + ], + }, + ], + }); +}); + +test('can use lambda version as alarm action', () => { + // GIVEN + const stack = new Stack(); + const alarm = new cloudwatch.Alarm(stack, 'Alarm', { + metric: new cloudwatch.Metric({ namespace: 'AWS', metricName: 'Test' }), + 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', + }); + const version = alarmLambda.currentVersion; + alarm.addAlarmAction(new actions.LambdaAction(version)); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', { + AlarmActions: [ + { + 'Fn::Join': [ + '', + [ + 'arn:', + { + Ref: 'AWS::Partition', + }, + ':lambda:', + { + Ref: 'AWS::Region', + }, + ':', + { + Ref: 'AWS::AccountId', + }, + ':alarmLambda:1', + ], + ], + }, + ], + }); +}); + From 375691357ae2b6132d5f9b15a6a77bccf307c226 Mon Sep 17 00:00:00 2001 From: neilkuan Date: Mon, 1 Jan 2024 04:36:33 +0000 Subject: [PATCH 07/22] chore: update code --- .../aws-cloudwatch-actions/lib/lambda.ts | 18 ++---- .../test/lambda.test.ts | 58 ++----------------- 2 files changed, 9 insertions(+), 67 deletions(-) diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts index 83f43b826b854..26079c2c818b2 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts @@ -22,7 +22,7 @@ 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, @@ -30,18 +30,8 @@ export class LambdaAction implements cloudwatch.IAlarmAction { 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, + }; } } diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts index 15bfa6fe41dd8..03e71b40b0cbb 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/test/lambda.test.ts @@ -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', ], }, ], @@ -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', }, ], }); @@ -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', }, ], }); From d2e6e06516364cbbf4081511dd537587724ab0ef Mon Sep 17 00:00:00 2001 From: neilkuan Date: Mon, 1 Jan 2024 05:21:39 +0000 Subject: [PATCH 08/22] chore: add integ testing --- ...efaultTestDeployAssertF9F882E5.assets.json | 19 + ...aultTestDeployAssertF9F882E5.template.json | 36 ++ ...larmActionIntegrationTestStack.assets.json | 19 + ...rmActionIntegrationTestStack.template.json | 288 ++++++++++ .../cdk.out | 1 + .../integ.json | 12 + .../manifest.json | 167 ++++++ .../tree.json | 519 ++++++++++++++++++ .../test/integ.lambda-alarm-action.ts | 55 ++ 9 files changed, 1116 insertions(+) create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/cdk.out create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/integ.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets.json new file mode 100644 index 0000000000000..2372b822099ba --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json new file mode 100644 index 0000000000000..2428455f27dd1 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.0", + "files": { + "3ecf35f5c1e42717d8d3492d07d7d8fbb935ba2e7961453e11b81fc5c0469ae2": { + "source": { + "path": "LambdaAlarmActionIntegrationTestStack.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "3ecf35f5c1e42717d8d3492d07d7d8fbb935ba2e7961453e11b81fc5c0469ae2.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json new file mode 100644 index 0000000000000..427a04557a8da --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json @@ -0,0 +1,288 @@ +{ + "Resources": { + "inAlarmLambdaServiceRole970DE64C": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "inAlarmLambda0920D101": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "ZipFile": "\ndef handler(event, context):\n make this fn fail\n prinr22(''hello)" + }, + "FunctionName": "inAlarmLambda", + "Handler": "index.handler", + "Role": { + "Fn::GetAtt": [ + "inAlarmLambdaServiceRole970DE64C", + "Arn" + ] + }, + "Runtime": "python3.12" + }, + "DependsOn": [ + "inAlarmLambdaServiceRole970DE64C" + ] + }, + "Alarm7103F465": { + "Type": "AWS::CloudWatch::Alarm", + "Properties": { + "ActionsEnabled": true, + "AlarmActions": [ + { + "Ref": "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a" + }, + { + "Ref": "alarmLambdaAliasaliasName41B27313" + }, + { + "Fn::GetAtt": [ + "alarmLambda131DB691", + "Arn" + ] + } + ], + "ComparisonOperator": "GreaterThanThreshold", + "Dimensions": [ + { + "Name": "FunctionName", + "Value": { + "Ref": "inAlarmLambda0920D101" + } + } + ], + "EvaluationPeriods": 1, + "MetricName": "Errors", + "Namespace": "AWS/Lambda", + "Period": 60, + "Statistic": "Sum", + "Threshold": 1, + "TreatMissingData": "notBreaching" + } + }, + "AlarmCurrentVersionAlarmPermission22DB4772": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Fn::Join": [ + "", + [ + { + "Ref": "alarmLambda131DB691" + }, + ":", + { + "Fn::GetAtt": [ + "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a", + "Version" + ] + } + ] + ] + }, + "Principal": "lambda.alarms.cloudwatch.amazonaws.com", + "SourceAccount": { + "Ref": "AWS::AccountId" + }, + "SourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "AlarmAliasaliasNameAlarmPermission3887D390": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Fn::Join": [ + "", + [ + { + "Fn::Select": [ + 6, + { + "Fn::Split": [ + ":", + { + "Ref": "alarmLambdaAliasaliasName41B27313" + } + ] + } + ] + }, + ":aliasName" + ] + ] + }, + "Principal": "lambda.alarms.cloudwatch.amazonaws.com", + "SourceAccount": { + "Ref": "AWS::AccountId" + }, + "SourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "AlarmalarmLambdaAlarmPermission5CAF3DE7": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Ref": "alarmLambda131DB691" + }, + "Principal": "lambda.alarms.cloudwatch.amazonaws.com", + "SourceAccount": { + "Ref": "AWS::AccountId" + }, + "SourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "alarmLambdaServiceRoleCDAABB9D": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "alarmLambda131DB691": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "ZipFile": "\ndef handler(event, context):\n print('event:', event)\n print('.............................................')\n print('context:', context)" + }, + "FunctionName": "alarmLambda", + "Handler": "index.handler", + "Role": { + "Fn::GetAtt": [ + "alarmLambdaServiceRoleCDAABB9D", + "Arn" + ] + }, + "Runtime": "python3.12" + }, + "DependsOn": [ + "alarmLambdaServiceRoleCDAABB9D" + ] + }, + "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a": { + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "alarmLambda131DB691" + } + } + }, + "alarmLambdaAliasaliasName41B27313": { + "Type": "AWS::Lambda::Alias", + "Properties": { + "FunctionName": { + "Ref": "alarmLambda131DB691" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a", + "Version" + ] + }, + "Name": "aliasName" + } + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/cdk.out new file mode 100644 index 0000000000000..1f0068d32659a --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"36.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/integ.json new file mode 100644 index 0000000000000..0d675b8e2e251 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "36.0.0", + "testCases": { + "LambdaAlarmActionIntegrationTest/DefaultTest": { + "stacks": [ + "LambdaAlarmActionIntegrationTestStack" + ], + "assertionStack": "LambdaAlarmActionIntegrationTest/DefaultTest/DeployAssert", + "assertionStackName": "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json new file mode 100644 index 0000000000000..fd185515bc5c1 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json @@ -0,0 +1,167 @@ +{ + "version": "36.0.0", + "artifacts": { + "LambdaAlarmActionIntegrationTestStack.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "LambdaAlarmActionIntegrationTestStack.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "LambdaAlarmActionIntegrationTestStack": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "LambdaAlarmActionIntegrationTestStack.template.json", + "terminationProtection": false, + "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}/3ecf35f5c1e42717d8d3492d07d7d8fbb935ba2e7961453e11b81fc5c0469ae2.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "LambdaAlarmActionIntegrationTestStack.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "LambdaAlarmActionIntegrationTestStack.assets" + ], + "metadata": { + "/LambdaAlarmActionIntegrationTestStack/inAlarmLambda/ServiceRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "inAlarmLambdaServiceRole970DE64C" + } + ], + "/LambdaAlarmActionIntegrationTestStack/inAlarmLambda/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "inAlarmLambda0920D101" + } + ], + "/LambdaAlarmActionIntegrationTestStack/Alarm/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "Alarm7103F465" + } + ], + "/LambdaAlarmActionIntegrationTestStack/Alarm/CurrentVersionAlarmPermission": [ + { + "type": "aws:cdk:logicalId", + "data": "AlarmCurrentVersionAlarmPermission22DB4772" + } + ], + "/LambdaAlarmActionIntegrationTestStack/Alarm/AliasaliasNameAlarmPermission": [ + { + "type": "aws:cdk:logicalId", + "data": "AlarmAliasaliasNameAlarmPermission3887D390" + } + ], + "/LambdaAlarmActionIntegrationTestStack/Alarm/alarmLambdaAlarmPermission": [ + { + "type": "aws:cdk:logicalId", + "data": "AlarmalarmLambdaAlarmPermission5CAF3DE7" + } + ], + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/ServiceRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "alarmLambdaServiceRoleCDAABB9D" + } + ], + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "alarmLambda131DB691" + } + ], + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a" + } + ], + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "alarmLambdaAliasaliasName41B27313" + } + ], + "/LambdaAlarmActionIntegrationTestStack/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/LambdaAlarmActionIntegrationTestStack/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "LambdaAlarmActionIntegrationTestStack" + }, + "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.template.json", + "terminationProtection": false, + "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}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "LambdaAlarmActionIntegrationTestDefaultTestDeployAssertF9F882E5.assets" + ], + "metadata": { + "/LambdaAlarmActionIntegrationTest/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/LambdaAlarmActionIntegrationTest/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "LambdaAlarmActionIntegrationTest/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json new file mode 100644 index 0000000000000..a5b31290cea22 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json @@ -0,0 +1,519 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "LambdaAlarmActionIntegrationTestStack": { + "id": "LambdaAlarmActionIntegrationTestStack", + "path": "LambdaAlarmActionIntegrationTestStack", + "children": { + "inAlarmLambda": { + "id": "inAlarmLambda", + "path": "LambdaAlarmActionIntegrationTestStack/inAlarmLambda", + "children": { + "ServiceRole": { + "id": "ServiceRole", + "path": "LambdaAlarmActionIntegrationTestStack/inAlarmLambda/ServiceRole", + "children": { + "ImportServiceRole": { + "id": "ImportServiceRole", + "path": "LambdaAlarmActionIntegrationTestStack/inAlarmLambda/ServiceRole/ImportServiceRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "LambdaAlarmActionIntegrationTestStack/inAlarmLambda/ServiceRole/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "assumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "managedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnRole", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "LambdaAlarmActionIntegrationTestStack/inAlarmLambda/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Function", + "aws:cdk:cloudformation:props": { + "code": { + "zipFile": "\ndef handler(event, context):\n make this fn fail\n prinr22(''hello)" + }, + "functionName": "inAlarmLambda", + "handler": "index.handler", + "role": { + "Fn::GetAtt": [ + "inAlarmLambdaServiceRole970DE64C", + "Arn" + ] + }, + "runtime": "python3.12" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnFunction", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.FunctionBase", + "version": "0.0.0" + } + }, + "Alarm": { + "id": "Alarm", + "path": "LambdaAlarmActionIntegrationTestStack/Alarm", + "children": { + "Resource": { + "id": "Resource", + "path": "LambdaAlarmActionIntegrationTestStack/Alarm/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::CloudWatch::Alarm", + "aws:cdk:cloudformation:props": { + "actionsEnabled": true, + "alarmActions": [ + { + "Ref": "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a" + }, + { + "Ref": "alarmLambdaAliasaliasName41B27313" + }, + { + "Fn::GetAtt": [ + "alarmLambda131DB691", + "Arn" + ] + } + ], + "comparisonOperator": "GreaterThanThreshold", + "dimensions": [ + { + "name": "FunctionName", + "value": { + "Ref": "inAlarmLambda0920D101" + } + } + ], + "evaluationPeriods": 1, + "metricName": "Errors", + "namespace": "AWS/Lambda", + "period": 60, + "statistic": "Sum", + "threshold": 1, + "treatMissingData": "notBreaching" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudwatch.CfnAlarm", + "version": "0.0.0" + } + }, + "CurrentVersionAlarmPermission": { + "id": "CurrentVersionAlarmPermission", + "path": "LambdaAlarmActionIntegrationTestStack/Alarm/CurrentVersionAlarmPermission", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Fn::Join": [ + "", + [ + { + "Ref": "alarmLambda131DB691" + }, + ":", + { + "Fn::GetAtt": [ + "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a", + "Version" + ] + } + ] + ] + }, + "principal": "lambda.alarms.cloudwatch.amazonaws.com", + "sourceAccount": { + "Ref": "AWS::AccountId" + }, + "sourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } + }, + "AliasaliasNameAlarmPermission": { + "id": "AliasaliasNameAlarmPermission", + "path": "LambdaAlarmActionIntegrationTestStack/Alarm/AliasaliasNameAlarmPermission", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Fn::Join": [ + "", + [ + { + "Fn::Select": [ + 6, + { + "Fn::Split": [ + ":", + { + "Ref": "alarmLambdaAliasaliasName41B27313" + } + ] + } + ] + }, + ":aliasName" + ] + ] + }, + "principal": "lambda.alarms.cloudwatch.amazonaws.com", + "sourceAccount": { + "Ref": "AWS::AccountId" + }, + "sourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } + }, + "alarmLambdaAlarmPermission": { + "id": "alarmLambdaAlarmPermission", + "path": "LambdaAlarmActionIntegrationTestStack/Alarm/alarmLambdaAlarmPermission", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Ref": "alarmLambda131DB691" + }, + "principal": "lambda.alarms.cloudwatch.amazonaws.com", + "sourceAccount": { + "Ref": "AWS::AccountId" + }, + "sourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudwatch.Alarm", + "version": "0.0.0" + } + }, + "alarmLambda": { + "id": "alarmLambda", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda", + "children": { + "ServiceRole": { + "id": "ServiceRole", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/ServiceRole", + "children": { + "ImportServiceRole": { + "id": "ImportServiceRole", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/ServiceRole/ImportServiceRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/ServiceRole/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "assumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "managedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnRole", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Function", + "aws:cdk:cloudformation:props": { + "code": { + "zipFile": "\ndef handler(event, context):\n print('event:', event)\n print('.............................................')\n print('context:', context)" + }, + "functionName": "alarmLambda", + "handler": "index.handler", + "role": { + "Fn::GetAtt": [ + "alarmLambdaServiceRoleCDAABB9D", + "Arn" + ] + }, + "runtime": "python3.12" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnFunction", + "version": "0.0.0" + } + }, + "CurrentVersion": { + "id": "CurrentVersion", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion", + "children": { + "Resource": { + "id": "Resource", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Version", + "aws:cdk:cloudformation:props": { + "functionName": { + "Ref": "alarmLambda131DB691" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnVersion", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.Version", + "version": "0.0.0" + } + }, + "AliasaliasName": { + "id": "AliasaliasName", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName", + "children": { + "Resource": { + "id": "Resource", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Alias", + "aws:cdk:cloudformation:props": { + "functionName": { + "Ref": "alarmLambda131DB691" + }, + "functionVersion": { + "Fn::GetAtt": [ + "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a", + "Version" + ] + }, + "name": "aliasName" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnAlias", + "version": "0.0.0" + } + }, + "ScalingRole": { + "id": "ScalingRole", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/ScalingRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.Alias", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.FunctionBase", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "LambdaAlarmActionIntegrationTestStack/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "LambdaAlarmActionIntegrationTestStack/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "LambdaAlarmActionIntegrationTest": { + "id": "LambdaAlarmActionIntegrationTest", + "path": "LambdaAlarmActionIntegrationTest", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "LambdaAlarmActionIntegrationTest/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "LambdaAlarmActionIntegrationTest/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "LambdaAlarmActionIntegrationTest/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "LambdaAlarmActionIntegrationTest/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "LambdaAlarmActionIntegrationTest/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", + "version": "0.0.0" + } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts new file mode 100644 index 0000000000000..6755a13eb1289 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts @@ -0,0 +1,55 @@ +import { App, Stack, StackProps, Duration } from 'aws-cdk-lib'; +import * as integ from '@aws-cdk/integ-tests-alpha'; +import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch'; +import * as lambda from 'aws-cdk-lib/aws-lambda'; +import * as cloudwatchActions from 'aws-cdk-lib/aws-cloudwatch-actions'; + +class LambdaAlarmActionIntegrationTestStack extends Stack { + + constructor(scope: App, id: string, props?: StackProps) { + super(scope, id, props); + const inAlarmLambda = new lambda.Function(this, 'inAlarmLambda', { + runtime: lambda.Runtime.PYTHON_3_12, + functionName: 'inAlarmLambda', + code: lambda.Code.fromInline(` +def handler(event, context): + make this fn fail + prinr22(''hello)`), + handler: 'index.handler', + }); + const alarm = new cloudwatch.Alarm(this, 'Alarm', { + comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD, + threshold: 1, + evaluationPeriods: 1, + metric: inAlarmLambda.metricErrors({ period: Duration.minutes(1) }), + actionsEnabled: true, + treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING, + }); + + const alarmLambda = new lambda.Function(this, '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', + }); + const version = alarmLambda.currentVersion; + const aliasName = alarmLambda.addAlias('aliasName'); + alarm.addAlarmAction(new cloudwatchActions.LambdaAction(version)); + alarm.addAlarmAction(new cloudwatchActions.LambdaAction(aliasName)); + alarm.addAlarmAction(new cloudwatchActions.LambdaAction(alarmLambda)); + } +} + +const app = new App(); + +const stack = new LambdaAlarmActionIntegrationTestStack(app, 'LambdaAlarmActionIntegrationTestStack'); + +new integ.IntegTest(app, 'LambdaAlarmActionIntegrationTest', { + testCases: [stack], +}); + +app.synth(); From ad2939b6a782f0f38262076ff5cdaadb5acaa2f1 Mon Sep 17 00:00:00 2001 From: neilkuan Date: Wed, 3 Jan 2024 03:27:38 +0000 Subject: [PATCH 09/22] chore: update readme --- packages/aws-cdk-lib/aws-cloudwatch-actions/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md b/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md index e2acb85953368..5b8125b844ffc 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md @@ -44,10 +44,10 @@ alarm.addAlarmAction( ```ts declare const alarm: cloudwatch.Alarm; -declare const lambda: lambda.Function; +declare const fn: lambda.Function; alarm.addAlarmAction( - new actions.LambdaAction(lambda) + new actions.LambdaAction(fn) ); ``` From b8c33eb56d4b676cbc36db249637c773a8aaa7cf Mon Sep 17 00:00:00 2001 From: neilkuan Date: Wed, 3 Jan 2024 04:00:24 +0000 Subject: [PATCH 10/22] chore: update readme --- packages/aws-cdk-lib/aws-cloudwatch-actions/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md b/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md index 5b8125b844ffc..5499de4410264 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md @@ -43,6 +43,7 @@ alarm.addAlarmAction( ## Lambda Action Example ```ts +import * as lambda from 'aws-cdk-lib/aws-lambda' declare const alarm: cloudwatch.Alarm; declare const fn: lambda.Function; From 3a3e5b1702cc1d39b4049764db5b94aa14313163 Mon Sep 17 00:00:00 2001 From: neilkuan Date: Thu, 4 Jan 2024 00:39:44 +0000 Subject: [PATCH 11/22] chore: update code from pr review --- .../test/integ.lambda-alarm-action.ts | 2 +- .../aws-cloudwatch-actions/lib/lambda.ts | 20 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts index 6755a13eb1289..ebbf10d2eb618 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts @@ -13,7 +13,7 @@ class LambdaAlarmActionIntegrationTestStack extends Stack { functionName: 'inAlarmLambda', code: lambda.Code.fromInline(` def handler(event, context): - make this fn fail + # make this fn fail prinr22(''hello)`), handler: 'index.handler', }); diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts index 26079c2c818b2..4b7bb14add9f5 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts @@ -1,5 +1,6 @@ import { Construct } from 'constructs'; import * as cloudwatch from '../../aws-cloudwatch'; +import * as iam from '../../aws-iam'; import * as lambda from '../../aws-lambda'; import { Stack } from '../../core'; @@ -17,19 +18,18 @@ export class LambdaAction implements cloudwatch.IAlarmAction { /** * Returns an alarm action configuration to use an Lambda action as an alarm action * - * @see: https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricAlarm.html + * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricAlarm.html * */ bind(_scope: Construct, _alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig { - if (this.lambdaFunction.stack.account === Stack.of(_scope).account) { - 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, - }); - } + + this.lambdaFunction.addPermission(`${this.lambdaFunction.node.id}AlarmPermission`, { + sourceAccount: Stack.of(_scope).account, + action: 'lambda:InvokeFunction', + sourceArn: _alarm.alarmArn, + principal: new iam.ServicePrincipal('lambda.alarms.cloudwatch.amazonaws.com'), + }); + return { alarmActionArn: this.lambdaFunction.functionArn, }; From 6e5d592892eb52a0218588577a447a5b89de9a25 Mon Sep 17 00:00:00 2001 From: neilkuan Date: Thu, 4 Jan 2024 01:02:12 +0000 Subject: [PATCH 12/22] chore: update snapshot --- .../LambdaAlarmActionIntegrationTestStack.assets.json | 4 ++-- ...LambdaAlarmActionIntegrationTestStack.template.json | 2 +- .../manifest.json | 2 +- .../integ.lambda-alarm-action.js.snapshot/tree.json | 10 +++++----- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json index 2428455f27dd1..8972191305638 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json @@ -1,7 +1,7 @@ { "version": "36.0.0", "files": { - "3ecf35f5c1e42717d8d3492d07d7d8fbb935ba2e7961453e11b81fc5c0469ae2": { + "8756f063d4272071b5a0e679dcc6ce13f3735d993730d1cff013753d30c02715": { "source": { "path": "LambdaAlarmActionIntegrationTestStack.template.json", "packaging": "file" @@ -9,7 +9,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "3ecf35f5c1e42717d8d3492d07d7d8fbb935ba2e7961453e11b81fc5c0469ae2.json", + "objectKey": "8756f063d4272071b5a0e679dcc6ce13f3735d993730d1cff013753d30c02715.json", "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" } } diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json index 427a04557a8da..9777c21905b79 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json @@ -35,7 +35,7 @@ "Type": "AWS::Lambda::Function", "Properties": { "Code": { - "ZipFile": "\ndef handler(event, context):\n make this fn fail\n prinr22(''hello)" + "ZipFile": "\ndef handler(event, context):\n # make this fn fail\n prinr22(''hello)" }, "FunctionName": "inAlarmLambda", "Handler": "index.handler", diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json index fd185515bc5c1..6ae79b5dc74f9 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json @@ -18,7 +18,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}/3ecf35f5c1e42717d8d3492d07d7d8fbb935ba2e7961453e11b81fc5c0469ae2.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/8756f063d4272071b5a0e679dcc6ce13f3735d993730d1cff013753d30c02715.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json index a5b31290cea22..1b0c4aee38bf5 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json @@ -65,7 +65,7 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.Resource", + "fqn": "aws-cdk-lib.aws_iam.Role", "version": "0.0.0" } }, @@ -76,7 +76,7 @@ "aws:cdk:cloudformation:type": "AWS::Lambda::Function", "aws:cdk:cloudformation:props": { "code": { - "zipFile": "\ndef handler(event, context):\n make this fn fail\n prinr22(''hello)" + "zipFile": "\ndef handler(event, context):\n # make this fn fail\n prinr22(''hello)" }, "functionName": "inAlarmLambda", "handler": "index.handler", @@ -96,7 +96,7 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.FunctionBase", + "fqn": "aws-cdk-lib.aws_lambda.Function", "version": "0.0.0" } }, @@ -324,7 +324,7 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.Resource", + "fqn": "aws-cdk-lib.aws_iam.Role", "version": "0.0.0" } }, @@ -422,7 +422,7 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.FunctionBase", + "fqn": "aws-cdk-lib.aws_lambda.Function", "version": "0.0.0" } }, From 17ec0a23597e370fa8cb5d6db0c76ec9d9a4ac30 Mon Sep 17 00:00:00 2001 From: neilkuan Date: Thu, 4 Jan 2024 01:39:25 +0000 Subject: [PATCH 13/22] chore: update integ testing snapshot --- ...larmActionIntegrationTestStack.assets.json | 4 ++-- ...rmActionIntegrationTestStack.template.json | 16 +++++++------- .../manifest.json | 18 ++++++++++++--- .../tree.json | 14 ++++++------ .../test/integ.lambda-alarm-action.ts | 22 +++++++++---------- 5 files changed, 43 insertions(+), 31 deletions(-) diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json index 8972191305638..5698e0c924a0b 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json @@ -1,7 +1,7 @@ { "version": "36.0.0", "files": { - "8756f063d4272071b5a0e679dcc6ce13f3735d993730d1cff013753d30c02715": { + "88b21afc833974331e24a331feac58d1ab1f74688025e5e90d7b77bdf6d33820": { "source": { "path": "LambdaAlarmActionIntegrationTestStack.template.json", "packaging": "file" @@ -9,7 +9,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "8756f063d4272071b5a0e679dcc6ce13f3735d993730d1cff013753d30c02715.json", + "objectKey": "88b21afc833974331e24a331feac58d1ab1f74688025e5e90d7b77bdf6d33820.json", "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" } } diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json index 9777c21905b79..23c72da8a9e9c 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json @@ -35,7 +35,7 @@ "Type": "AWS::Lambda::Function", "Properties": { "Code": { - "ZipFile": "\ndef handler(event, context):\n # make this fn fail\n prinr22(''hello)" + "ZipFile": "exports.handler = function handler(event, _context, callback) {\n console.log(JSON.stringify(event, undefined, 2));\n return callback();\n}" }, "FunctionName": "inAlarmLambda", "Handler": "index.handler", @@ -45,7 +45,7 @@ "Arn" ] }, - "Runtime": "python3.12" + "Runtime": "nodejs18.x" }, "DependsOn": [ "inAlarmLambdaServiceRole970DE64C" @@ -57,7 +57,7 @@ "ActionsEnabled": true, "AlarmActions": [ { - "Ref": "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a" + "Ref": "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b" }, { "Ref": "alarmLambdaAliasaliasName41B27313" @@ -101,7 +101,7 @@ ":", { "Fn::GetAtt": [ - "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a", + "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b", "Version" ] } @@ -211,7 +211,7 @@ "Type": "AWS::Lambda::Function", "Properties": { "Code": { - "ZipFile": "\ndef handler(event, context):\n print('event:', event)\n print('.............................................')\n print('context:', context)" + "ZipFile": "exports.handler = function handler(event, _context, callback) {\n console.log(JSON.stringify(event, undefined, 2));\n return callback();\n}" }, "FunctionName": "alarmLambda", "Handler": "index.handler", @@ -221,13 +221,13 @@ "Arn" ] }, - "Runtime": "python3.12" + "Runtime": "nodejs18.x" }, "DependsOn": [ "alarmLambdaServiceRoleCDAABB9D" ] }, - "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a": { + "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b": { "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { @@ -243,7 +243,7 @@ }, "FunctionVersion": { "Fn::GetAtt": [ - "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a", + "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b", "Version" ] }, diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json index 6ae79b5dc74f9..9db8abcb451e9 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json @@ -18,7 +18,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}/8756f063d4272071b5a0e679dcc6ce13f3735d993730d1cff013753d30c02715.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/88b21afc833974331e24a331feac58d1ab1f74688025e5e90d7b77bdf6d33820.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ @@ -55,7 +55,10 @@ "/LambdaAlarmActionIntegrationTestStack/Alarm/CurrentVersionAlarmPermission": [ { "type": "aws:cdk:logicalId", - "data": "AlarmCurrentVersionAlarmPermission22DB4772" + "data": "AlarmCurrentVersionAlarmPermission22DB4772", + "trace": [ + "!!DESTRUCTIVE_CHANGES: WILL_REPLACE" + ] } ], "/LambdaAlarmActionIntegrationTestStack/Alarm/AliasaliasNameAlarmPermission": [ @@ -85,7 +88,7 @@ "/LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/Resource": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a" + "data": "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b" } ], "/LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/Resource": [ @@ -105,6 +108,15 @@ "type": "aws:cdk:logicalId", "data": "CheckBootstrapVersion" } + ], + "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a": [ + { + "type": "aws:cdk:logicalId", + "data": "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a", + "trace": [ + "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" + ] + } ] }, "displayName": "LambdaAlarmActionIntegrationTestStack" diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json index 1b0c4aee38bf5..5dd118e34d395 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json @@ -76,7 +76,7 @@ "aws:cdk:cloudformation:type": "AWS::Lambda::Function", "aws:cdk:cloudformation:props": { "code": { - "zipFile": "\ndef handler(event, context):\n # make this fn fail\n prinr22(''hello)" + "zipFile": "exports.handler = function handler(event, _context, callback) {\n console.log(JSON.stringify(event, undefined, 2));\n return callback();\n}" }, "functionName": "inAlarmLambda", "handler": "index.handler", @@ -86,7 +86,7 @@ "Arn" ] }, - "runtime": "python3.12" + "runtime": "nodejs18.x" } }, "constructInfo": { @@ -113,7 +113,7 @@ "actionsEnabled": true, "alarmActions": [ { - "Ref": "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a" + "Ref": "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b" }, { "Ref": "alarmLambdaAliasaliasName41B27313" @@ -165,7 +165,7 @@ ":", { "Fn::GetAtt": [ - "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a", + "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b", "Version" ] } @@ -335,7 +335,7 @@ "aws:cdk:cloudformation:type": "AWS::Lambda::Function", "aws:cdk:cloudformation:props": { "code": { - "zipFile": "\ndef handler(event, context):\n print('event:', event)\n print('.............................................')\n print('context:', context)" + "zipFile": "exports.handler = function handler(event, _context, callback) {\n console.log(JSON.stringify(event, undefined, 2));\n return callback();\n}" }, "functionName": "alarmLambda", "handler": "index.handler", @@ -345,7 +345,7 @@ "Arn" ] }, - "runtime": "python3.12" + "runtime": "nodejs18.x" } }, "constructInfo": { @@ -394,7 +394,7 @@ }, "functionVersion": { "Fn::GetAtt": [ - "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a", + "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b", "Version" ] }, diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts index ebbf10d2eb618..25fa92a34464d 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.ts @@ -2,6 +2,7 @@ import { App, Stack, StackProps, Duration } from 'aws-cdk-lib'; import * as integ from '@aws-cdk/integ-tests-alpha'; import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch'; import * as lambda from 'aws-cdk-lib/aws-lambda'; +import { STANDARD_NODEJS_RUNTIME } from '../../config'; import * as cloudwatchActions from 'aws-cdk-lib/aws-cloudwatch-actions'; class LambdaAlarmActionIntegrationTestStack extends Stack { @@ -9,13 +10,10 @@ class LambdaAlarmActionIntegrationTestStack extends Stack { constructor(scope: App, id: string, props?: StackProps) { super(scope, id, props); const inAlarmLambda = new lambda.Function(this, 'inAlarmLambda', { - runtime: lambda.Runtime.PYTHON_3_12, functionName: 'inAlarmLambda', - code: lambda.Code.fromInline(` -def handler(event, context): - # make this fn fail - prinr22(''hello)`), + runtime: STANDARD_NODEJS_RUNTIME, handler: 'index.handler', + code: lambda.Code.fromInline(`exports.handler = ${handler.toString()}`), }); const alarm = new cloudwatch.Alarm(this, 'Alarm', { comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD, @@ -27,14 +25,10 @@ def handler(event, context): }); const alarmLambda = new lambda.Function(this, 'alarmLambda', { - runtime: lambda.Runtime.PYTHON_3_12, functionName: 'alarmLambda', - code: lambda.Code.fromInline(` -def handler(event, context): - print('event:', event) - print('.............................................') - print('context:', context)`), + runtime: STANDARD_NODEJS_RUNTIME, handler: 'index.handler', + code: lambda.Code.fromInline(`exports.handler = ${handler.toString()}`), }); const version = alarmLambda.currentVersion; const aliasName = alarmLambda.addAlias('aliasName'); @@ -53,3 +47,9 @@ new integ.IntegTest(app, 'LambdaAlarmActionIntegrationTest', { }); app.synth(); + +/* eslint-disable no-console */ +function handler(event: any, _context: any, callback: any) { + console.log(JSON.stringify(event, undefined, 2)); + return callback(); +} \ No newline at end of file From 3c5dea560d64005882f7cd9184484b3a4cbccc96 Mon Sep 17 00:00:00 2001 From: neilkuan Date: Thu, 4 Jan 2024 02:43:53 +0000 Subject: [PATCH 14/22] chore: let testing happy --- .../manifest.json | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json index 9db8abcb451e9..47b4344b9923c 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json @@ -55,10 +55,7 @@ "/LambdaAlarmActionIntegrationTestStack/Alarm/CurrentVersionAlarmPermission": [ { "type": "aws:cdk:logicalId", - "data": "AlarmCurrentVersionAlarmPermission22DB4772", - "trace": [ - "!!DESTRUCTIVE_CHANGES: WILL_REPLACE" - ] + "data": "AlarmCurrentVersionAlarmPermission22DB4772" } ], "/LambdaAlarmActionIntegrationTestStack/Alarm/AliasaliasNameAlarmPermission": [ @@ -108,15 +105,6 @@ "type": "aws:cdk:logicalId", "data": "CheckBootstrapVersion" } - ], - "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a": [ - { - "type": "aws:cdk:logicalId", - "data": "alarmLambdaCurrentVersionBDCE825Ced84cae8d64e7eaa69f8105fc0363a2a", - "trace": [ - "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" - ] - } ] }, "displayName": "LambdaAlarmActionIntegrationTestStack" From 579c88b3b5619d0a702c311c541477fc44131a33 Mon Sep 17 00:00:00 2001 From: neilkuan Date: Thu, 4 Jan 2024 03:16:27 +0000 Subject: [PATCH 15/22] chore: run integ test From d8bde8a0cacd6403ae719006032fcfdebd6bbf25 Mon Sep 17 00:00:00 2001 From: Pahud Hsieh Date: Thu, 4 Jan 2024 03:18:18 +0000 Subject: [PATCH 16/22] update snapshots --- ...larmActionIntegrationTestStack.assets.json | 4 +- ...rmActionIntegrationTestStack.template.json | 149 ++++++------- .../manifest.json | 63 ++++-- .../tree.json | 197 ++++++++---------- 4 files changed, 194 insertions(+), 219 deletions(-) diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json index 5698e0c924a0b..4d0f97e8f7149 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json @@ -1,7 +1,7 @@ { "version": "36.0.0", "files": { - "88b21afc833974331e24a331feac58d1ab1f74688025e5e90d7b77bdf6d33820": { + "01b15cd45f337d30b1b8c28cf3ed77a67ed6679eb13828d43f8b809a84819959": { "source": { "path": "LambdaAlarmActionIntegrationTestStack.template.json", "packaging": "file" @@ -9,7 +9,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "88b21afc833974331e24a331feac58d1ab1f74688025e5e90d7b77bdf6d33820.json", + "objectKey": "01b15cd45f337d30b1b8c28cf3ed77a67ed6679eb13828d43f8b809a84819959.json", "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" } } diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json index 23c72da8a9e9c..4e23d71e1308a 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json @@ -87,95 +87,6 @@ "TreatMissingData": "notBreaching" } }, - "AlarmCurrentVersionAlarmPermission22DB4772": { - "Type": "AWS::Lambda::Permission", - "Properties": { - "Action": "lambda:InvokeFunction", - "FunctionName": { - "Fn::Join": [ - "", - [ - { - "Ref": "alarmLambda131DB691" - }, - ":", - { - "Fn::GetAtt": [ - "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b", - "Version" - ] - } - ] - ] - }, - "Principal": "lambda.alarms.cloudwatch.amazonaws.com", - "SourceAccount": { - "Ref": "AWS::AccountId" - }, - "SourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, - "AlarmAliasaliasNameAlarmPermission3887D390": { - "Type": "AWS::Lambda::Permission", - "Properties": { - "Action": "lambda:InvokeFunction", - "FunctionName": { - "Fn::Join": [ - "", - [ - { - "Fn::Select": [ - 6, - { - "Fn::Split": [ - ":", - { - "Ref": "alarmLambdaAliasaliasName41B27313" - } - ] - } - ] - }, - ":aliasName" - ] - ] - }, - "Principal": "lambda.alarms.cloudwatch.amazonaws.com", - "SourceAccount": { - "Ref": "AWS::AccountId" - }, - "SourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, - "AlarmalarmLambdaAlarmPermission5CAF3DE7": { - "Type": "AWS::Lambda::Permission", - "Properties": { - "Action": "lambda:InvokeFunction", - "FunctionName": { - "Ref": "alarmLambda131DB691" - }, - "Principal": "lambda.alarms.cloudwatch.amazonaws.com", - "SourceAccount": { - "Ref": "AWS::AccountId" - }, - "SourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, "alarmLambdaServiceRoleCDAABB9D": { "Type": "AWS::IAM::Role", "Properties": { @@ -235,6 +146,25 @@ } } }, + "alarmLambdaCurrentVersionCurrentVersionAlarmPermission274CB36D": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Ref": "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b" + }, + "Principal": "lambda.alarms.cloudwatch.amazonaws.com", + "SourceAccount": { + "Ref": "AWS::AccountId" + }, + "SourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, "alarmLambdaAliasaliasName41B27313": { "Type": "AWS::Lambda::Alias", "Properties": { @@ -249,6 +179,47 @@ }, "Name": "aliasName" } + }, + "alarmLambdaAliasaliasNameAliasaliasNameAlarmPermissionED9EC624": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Ref": "alarmLambdaAliasaliasName41B27313" + }, + "Principal": "lambda.alarms.cloudwatch.amazonaws.com", + "SourceAccount": { + "Ref": "AWS::AccountId" + }, + "SourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "alarmLambdaalarmLambdaAlarmPermissionA0BF56DE": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Fn::GetAtt": [ + "alarmLambda131DB691", + "Arn" + ] + }, + "Principal": "lambda.alarms.cloudwatch.amazonaws.com", + "SourceAccount": { + "Ref": "AWS::AccountId" + }, + "SourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } } }, "Parameters": { diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json index 47b4344b9923c..3b03037d718fb 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json @@ -18,7 +18,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}/88b21afc833974331e24a331feac58d1ab1f74688025e5e90d7b77bdf6d33820.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/01b15cd45f337d30b1b8c28cf3ed77a67ed6679eb13828d43f8b809a84819959.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ @@ -52,46 +52,52 @@ "data": "Alarm7103F465" } ], - "/LambdaAlarmActionIntegrationTestStack/Alarm/CurrentVersionAlarmPermission": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda": [ + { + "type": "aws:cdk:warning", + "data": "AWS Lambda has changed their authorization strategy, which may cause client invocations using the 'Qualifier' parameter of the lambda function to fail with Access Denied errors.\nIf you are using a lambda Version or Alias, make sure to call 'grantInvoke' or 'addPermission' on the Version or Alias, not the underlying Function\nSee: https://github.com/aws/aws-cdk/issues/19273 [ack: @aws-cdk/aws-lambda:addPermissionsToVersionOrAlias]" + } + ], + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/ServiceRole/Resource": [ { "type": "aws:cdk:logicalId", - "data": "AlarmCurrentVersionAlarmPermission22DB4772" + "data": "alarmLambdaServiceRoleCDAABB9D" } ], - "/LambdaAlarmActionIntegrationTestStack/Alarm/AliasaliasNameAlarmPermission": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/Resource": [ { "type": "aws:cdk:logicalId", - "data": "AlarmAliasaliasNameAlarmPermission3887D390" + "data": "alarmLambda131DB691" } ], - "/LambdaAlarmActionIntegrationTestStack/Alarm/alarmLambdaAlarmPermission": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/Resource": [ { "type": "aws:cdk:logicalId", - "data": "AlarmalarmLambdaAlarmPermission5CAF3DE7" + "data": "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b" } ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda/ServiceRole/Resource": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/CurrentVersionAlarmPermission": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaServiceRoleCDAABB9D" + "data": "alarmLambdaCurrentVersionCurrentVersionAlarmPermission274CB36D" } ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda/Resource": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/Resource": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambda131DB691" + "data": "alarmLambdaAliasaliasName41B27313" } ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/Resource": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/AliasaliasNameAlarmPermission": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b" + "data": "alarmLambdaAliasaliasNameAliasaliasNameAlarmPermissionED9EC624" } ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/Resource": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/alarmLambdaAlarmPermission": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaAliasaliasName41B27313" + "data": "alarmLambdaalarmLambdaAlarmPermissionA0BF56DE" } ], "/LambdaAlarmActionIntegrationTestStack/BootstrapVersion": [ @@ -105,6 +111,33 @@ "type": "aws:cdk:logicalId", "data": "CheckBootstrapVersion" } + ], + "AlarmCurrentVersionAlarmPermission22DB4772": [ + { + "type": "aws:cdk:logicalId", + "data": "AlarmCurrentVersionAlarmPermission22DB4772", + "trace": [ + "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" + ] + } + ], + "AlarmAliasaliasNameAlarmPermission3887D390": [ + { + "type": "aws:cdk:logicalId", + "data": "AlarmAliasaliasNameAlarmPermission3887D390", + "trace": [ + "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" + ] + } + ], + "AlarmalarmLambdaAlarmPermission5CAF3DE7": [ + { + "type": "aws:cdk:logicalId", + "data": "AlarmalarmLambdaAlarmPermission5CAF3DE7", + "trace": [ + "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" + ] + } ] }, "displayName": "LambdaAlarmActionIntegrationTestStack" diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json index 5dd118e34d395..0e7cf5a37f3e7 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json @@ -147,119 +147,6 @@ "fqn": "aws-cdk-lib.aws_cloudwatch.CfnAlarm", "version": "0.0.0" } - }, - "CurrentVersionAlarmPermission": { - "id": "CurrentVersionAlarmPermission", - "path": "LambdaAlarmActionIntegrationTestStack/Alarm/CurrentVersionAlarmPermission", - "attributes": { - "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", - "aws:cdk:cloudformation:props": { - "action": "lambda:InvokeFunction", - "functionName": { - "Fn::Join": [ - "", - [ - { - "Ref": "alarmLambda131DB691" - }, - ":", - { - "Fn::GetAtt": [ - "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b", - "Version" - ] - } - ] - ] - }, - "principal": "lambda.alarms.cloudwatch.amazonaws.com", - "sourceAccount": { - "Ref": "AWS::AccountId" - }, - "sourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, - "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", - "version": "0.0.0" - } - }, - "AliasaliasNameAlarmPermission": { - "id": "AliasaliasNameAlarmPermission", - "path": "LambdaAlarmActionIntegrationTestStack/Alarm/AliasaliasNameAlarmPermission", - "attributes": { - "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", - "aws:cdk:cloudformation:props": { - "action": "lambda:InvokeFunction", - "functionName": { - "Fn::Join": [ - "", - [ - { - "Fn::Select": [ - 6, - { - "Fn::Split": [ - ":", - { - "Ref": "alarmLambdaAliasaliasName41B27313" - } - ] - } - ] - }, - ":aliasName" - ] - ] - }, - "principal": "lambda.alarms.cloudwatch.amazonaws.com", - "sourceAccount": { - "Ref": "AWS::AccountId" - }, - "sourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, - "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", - "version": "0.0.0" - } - }, - "alarmLambdaAlarmPermission": { - "id": "alarmLambdaAlarmPermission", - "path": "LambdaAlarmActionIntegrationTestStack/Alarm/alarmLambdaAlarmPermission", - "attributes": { - "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", - "aws:cdk:cloudformation:props": { - "action": "lambda:InvokeFunction", - "functionName": { - "Ref": "alarmLambda131DB691" - }, - "principal": "lambda.alarms.cloudwatch.amazonaws.com", - "sourceAccount": { - "Ref": "AWS::AccountId" - }, - "sourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, - "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", - "version": "0.0.0" - } } }, "constructInfo": { @@ -372,6 +259,33 @@ "fqn": "aws-cdk-lib.aws_lambda.CfnVersion", "version": "0.0.0" } + }, + "CurrentVersionAlarmPermission": { + "id": "CurrentVersionAlarmPermission", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/CurrentVersionAlarmPermission", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Ref": "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b" + }, + "principal": "lambda.alarms.cloudwatch.amazonaws.com", + "sourceAccount": { + "Ref": "AWS::AccountId" + }, + "sourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } } }, "constructInfo": { @@ -413,12 +327,69 @@ "fqn": "aws-cdk-lib.Resource", "version": "0.0.0" } + }, + "AliasaliasNameAlarmPermission": { + "id": "AliasaliasNameAlarmPermission", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/AliasaliasNameAlarmPermission", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Ref": "alarmLambdaAliasaliasName41B27313" + }, + "principal": "lambda.alarms.cloudwatch.amazonaws.com", + "sourceAccount": { + "Ref": "AWS::AccountId" + }, + "sourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } } }, "constructInfo": { "fqn": "aws-cdk-lib.aws_lambda.Alias", "version": "0.0.0" } + }, + "alarmLambdaAlarmPermission": { + "id": "alarmLambdaAlarmPermission", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/alarmLambdaAlarmPermission", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Fn::GetAtt": [ + "alarmLambda131DB691", + "Arn" + ] + }, + "principal": "lambda.alarms.cloudwatch.amazonaws.com", + "sourceAccount": { + "Ref": "AWS::AccountId" + }, + "sourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } } }, "constructInfo": { From d8aded16df69d97e24741491306496bad3eb5289 Mon Sep 17 00:00:00 2001 From: neilkuan Date: Thu, 4 Jan 2024 04:22:03 +0000 Subject: [PATCH 17/22] chore: update testing and comment --- ...larmActionIntegrationTestStack.assets.json | 4 +- ...rmActionIntegrationTestStack.template.json | 149 +++++++------ .../manifest.json | 48 ++--- .../tree.json | 197 ++++++++++-------- .../aws-cloudwatch-actions/lib/lambda.ts | 2 +- 5 files changed, 226 insertions(+), 174 deletions(-) diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json index 4d0f97e8f7149..5698e0c924a0b 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json @@ -1,7 +1,7 @@ { "version": "36.0.0", "files": { - "01b15cd45f337d30b1b8c28cf3ed77a67ed6679eb13828d43f8b809a84819959": { + "88b21afc833974331e24a331feac58d1ab1f74688025e5e90d7b77bdf6d33820": { "source": { "path": "LambdaAlarmActionIntegrationTestStack.template.json", "packaging": "file" @@ -9,7 +9,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "01b15cd45f337d30b1b8c28cf3ed77a67ed6679eb13828d43f8b809a84819959.json", + "objectKey": "88b21afc833974331e24a331feac58d1ab1f74688025e5e90d7b77bdf6d33820.json", "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" } } diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json index 4e23d71e1308a..23c72da8a9e9c 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json @@ -87,6 +87,95 @@ "TreatMissingData": "notBreaching" } }, + "AlarmCurrentVersionAlarmPermission22DB4772": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Fn::Join": [ + "", + [ + { + "Ref": "alarmLambda131DB691" + }, + ":", + { + "Fn::GetAtt": [ + "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b", + "Version" + ] + } + ] + ] + }, + "Principal": "lambda.alarms.cloudwatch.amazonaws.com", + "SourceAccount": { + "Ref": "AWS::AccountId" + }, + "SourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "AlarmAliasaliasNameAlarmPermission3887D390": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Fn::Join": [ + "", + [ + { + "Fn::Select": [ + 6, + { + "Fn::Split": [ + ":", + { + "Ref": "alarmLambdaAliasaliasName41B27313" + } + ] + } + ] + }, + ":aliasName" + ] + ] + }, + "Principal": "lambda.alarms.cloudwatch.amazonaws.com", + "SourceAccount": { + "Ref": "AWS::AccountId" + }, + "SourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "AlarmalarmLambdaAlarmPermission5CAF3DE7": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Ref": "alarmLambda131DB691" + }, + "Principal": "lambda.alarms.cloudwatch.amazonaws.com", + "SourceAccount": { + "Ref": "AWS::AccountId" + }, + "SourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, "alarmLambdaServiceRoleCDAABB9D": { "Type": "AWS::IAM::Role", "Properties": { @@ -146,25 +235,6 @@ } } }, - "alarmLambdaCurrentVersionCurrentVersionAlarmPermission274CB36D": { - "Type": "AWS::Lambda::Permission", - "Properties": { - "Action": "lambda:InvokeFunction", - "FunctionName": { - "Ref": "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b" - }, - "Principal": "lambda.alarms.cloudwatch.amazonaws.com", - "SourceAccount": { - "Ref": "AWS::AccountId" - }, - "SourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, "alarmLambdaAliasaliasName41B27313": { "Type": "AWS::Lambda::Alias", "Properties": { @@ -179,47 +249,6 @@ }, "Name": "aliasName" } - }, - "alarmLambdaAliasaliasNameAliasaliasNameAlarmPermissionED9EC624": { - "Type": "AWS::Lambda::Permission", - "Properties": { - "Action": "lambda:InvokeFunction", - "FunctionName": { - "Ref": "alarmLambdaAliasaliasName41B27313" - }, - "Principal": "lambda.alarms.cloudwatch.amazonaws.com", - "SourceAccount": { - "Ref": "AWS::AccountId" - }, - "SourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, - "alarmLambdaalarmLambdaAlarmPermissionA0BF56DE": { - "Type": "AWS::Lambda::Permission", - "Properties": { - "Action": "lambda:InvokeFunction", - "FunctionName": { - "Fn::GetAtt": [ - "alarmLambda131DB691", - "Arn" - ] - }, - "Principal": "lambda.alarms.cloudwatch.amazonaws.com", - "SourceAccount": { - "Ref": "AWS::AccountId" - }, - "SourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } } }, "Parameters": { diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json index 3b03037d718fb..3007ab0eab94d 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json @@ -18,7 +18,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}/01b15cd45f337d30b1b8c28cf3ed77a67ed6679eb13828d43f8b809a84819959.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/88b21afc833974331e24a331feac58d1ab1f74688025e5e90d7b77bdf6d33820.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ @@ -52,52 +52,46 @@ "data": "Alarm7103F465" } ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda": [ - { - "type": "aws:cdk:warning", - "data": "AWS Lambda has changed their authorization strategy, which may cause client invocations using the 'Qualifier' parameter of the lambda function to fail with Access Denied errors.\nIf you are using a lambda Version or Alias, make sure to call 'grantInvoke' or 'addPermission' on the Version or Alias, not the underlying Function\nSee: https://github.com/aws/aws-cdk/issues/19273 [ack: @aws-cdk/aws-lambda:addPermissionsToVersionOrAlias]" - } - ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda/ServiceRole/Resource": [ + "/LambdaAlarmActionIntegrationTestStack/Alarm/CurrentVersionAlarmPermission": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaServiceRoleCDAABB9D" + "data": "AlarmCurrentVersionAlarmPermission22DB4772" } ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda/Resource": [ + "/LambdaAlarmActionIntegrationTestStack/Alarm/AliasaliasNameAlarmPermission": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambda131DB691" + "data": "AlarmAliasaliasNameAlarmPermission3887D390" } ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/Resource": [ + "/LambdaAlarmActionIntegrationTestStack/Alarm/alarmLambdaAlarmPermission": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b" + "data": "AlarmalarmLambdaAlarmPermission5CAF3DE7" } ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/CurrentVersionAlarmPermission": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/ServiceRole/Resource": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaCurrentVersionCurrentVersionAlarmPermission274CB36D" + "data": "alarmLambdaServiceRoleCDAABB9D" } ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/Resource": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/Resource": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaAliasaliasName41B27313" + "data": "alarmLambda131DB691" } ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/AliasaliasNameAlarmPermission": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/Resource": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaAliasaliasNameAliasaliasNameAlarmPermissionED9EC624" + "data": "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b" } ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda/alarmLambdaAlarmPermission": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/Resource": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaalarmLambdaAlarmPermissionA0BF56DE" + "data": "alarmLambdaAliasaliasName41B27313" } ], "/LambdaAlarmActionIntegrationTestStack/BootstrapVersion": [ @@ -112,28 +106,28 @@ "data": "CheckBootstrapVersion" } ], - "AlarmCurrentVersionAlarmPermission22DB4772": [ + "alarmLambdaCurrentVersionCurrentVersionAlarmPermission274CB36D": [ { "type": "aws:cdk:logicalId", - "data": "AlarmCurrentVersionAlarmPermission22DB4772", + "data": "alarmLambdaCurrentVersionCurrentVersionAlarmPermission274CB36D", "trace": [ "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" ] } ], - "AlarmAliasaliasNameAlarmPermission3887D390": [ + "alarmLambdaAliasaliasNameAliasaliasNameAlarmPermissionED9EC624": [ { "type": "aws:cdk:logicalId", - "data": "AlarmAliasaliasNameAlarmPermission3887D390", + "data": "alarmLambdaAliasaliasNameAliasaliasNameAlarmPermissionED9EC624", "trace": [ "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" ] } ], - "AlarmalarmLambdaAlarmPermission5CAF3DE7": [ + "alarmLambdaalarmLambdaAlarmPermissionA0BF56DE": [ { "type": "aws:cdk:logicalId", - "data": "AlarmalarmLambdaAlarmPermission5CAF3DE7", + "data": "alarmLambdaalarmLambdaAlarmPermissionA0BF56DE", "trace": [ "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" ] diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json index 0e7cf5a37f3e7..5dd118e34d395 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json @@ -147,6 +147,119 @@ "fqn": "aws-cdk-lib.aws_cloudwatch.CfnAlarm", "version": "0.0.0" } + }, + "CurrentVersionAlarmPermission": { + "id": "CurrentVersionAlarmPermission", + "path": "LambdaAlarmActionIntegrationTestStack/Alarm/CurrentVersionAlarmPermission", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Fn::Join": [ + "", + [ + { + "Ref": "alarmLambda131DB691" + }, + ":", + { + "Fn::GetAtt": [ + "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b", + "Version" + ] + } + ] + ] + }, + "principal": "lambda.alarms.cloudwatch.amazonaws.com", + "sourceAccount": { + "Ref": "AWS::AccountId" + }, + "sourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } + }, + "AliasaliasNameAlarmPermission": { + "id": "AliasaliasNameAlarmPermission", + "path": "LambdaAlarmActionIntegrationTestStack/Alarm/AliasaliasNameAlarmPermission", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Fn::Join": [ + "", + [ + { + "Fn::Select": [ + 6, + { + "Fn::Split": [ + ":", + { + "Ref": "alarmLambdaAliasaliasName41B27313" + } + ] + } + ] + }, + ":aliasName" + ] + ] + }, + "principal": "lambda.alarms.cloudwatch.amazonaws.com", + "sourceAccount": { + "Ref": "AWS::AccountId" + }, + "sourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } + }, + "alarmLambdaAlarmPermission": { + "id": "alarmLambdaAlarmPermission", + "path": "LambdaAlarmActionIntegrationTestStack/Alarm/alarmLambdaAlarmPermission", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Ref": "alarmLambda131DB691" + }, + "principal": "lambda.alarms.cloudwatch.amazonaws.com", + "sourceAccount": { + "Ref": "AWS::AccountId" + }, + "sourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } } }, "constructInfo": { @@ -259,33 +372,6 @@ "fqn": "aws-cdk-lib.aws_lambda.CfnVersion", "version": "0.0.0" } - }, - "CurrentVersionAlarmPermission": { - "id": "CurrentVersionAlarmPermission", - "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/CurrentVersionAlarmPermission", - "attributes": { - "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", - "aws:cdk:cloudformation:props": { - "action": "lambda:InvokeFunction", - "functionName": { - "Ref": "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b" - }, - "principal": "lambda.alarms.cloudwatch.amazonaws.com", - "sourceAccount": { - "Ref": "AWS::AccountId" - }, - "sourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, - "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", - "version": "0.0.0" - } } }, "constructInfo": { @@ -327,69 +413,12 @@ "fqn": "aws-cdk-lib.Resource", "version": "0.0.0" } - }, - "AliasaliasNameAlarmPermission": { - "id": "AliasaliasNameAlarmPermission", - "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/AliasaliasNameAlarmPermission", - "attributes": { - "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", - "aws:cdk:cloudformation:props": { - "action": "lambda:InvokeFunction", - "functionName": { - "Ref": "alarmLambdaAliasaliasName41B27313" - }, - "principal": "lambda.alarms.cloudwatch.amazonaws.com", - "sourceAccount": { - "Ref": "AWS::AccountId" - }, - "sourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, - "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", - "version": "0.0.0" - } } }, "constructInfo": { "fqn": "aws-cdk-lib.aws_lambda.Alias", "version": "0.0.0" } - }, - "alarmLambdaAlarmPermission": { - "id": "alarmLambdaAlarmPermission", - "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/alarmLambdaAlarmPermission", - "attributes": { - "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", - "aws:cdk:cloudformation:props": { - "action": "lambda:InvokeFunction", - "functionName": { - "Fn::GetAtt": [ - "alarmLambda131DB691", - "Arn" - ] - }, - "principal": "lambda.alarms.cloudwatch.amazonaws.com", - "sourceAccount": { - "Ref": "AWS::AccountId" - }, - "sourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, - "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", - "version": "0.0.0" - } } }, "constructInfo": { diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts index 4b7bb14add9f5..41e589060dc49 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts @@ -23,7 +23,7 @@ export class LambdaAction implements cloudwatch.IAlarmAction { */ bind(_scope: Construct, _alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig { - this.lambdaFunction.addPermission(`${this.lambdaFunction.node.id}AlarmPermission`, { + this.lambdaFunction.addPermission('AlarmPermission', { sourceAccount: Stack.of(_scope).account, action: 'lambda:InvokeFunction', sourceArn: _alarm.alarmArn, From b20de813369f9d18403466955e23d50343b6d28e Mon Sep 17 00:00:00 2001 From: Neil Kuan Date: Thu, 4 Jan 2024 14:02:16 +0800 Subject: [PATCH 18/22] chore: let testing happy --- ...larmActionIntegrationTestStack.assets.json | 4 +- ...rmActionIntegrationTestStack.template.json | 149 ++++++------- .../manifest.json | 48 +++-- .../tree.json | 197 ++++++++---------- 4 files changed, 173 insertions(+), 225 deletions(-) diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json index 5698e0c924a0b..93e694b2f20de 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.assets.json @@ -1,7 +1,7 @@ { "version": "36.0.0", "files": { - "88b21afc833974331e24a331feac58d1ab1f74688025e5e90d7b77bdf6d33820": { + "4e1c8f560b3bd32ad0758c6b7d04153c21111750f33dcfff05c52796bc71f9de": { "source": { "path": "LambdaAlarmActionIntegrationTestStack.template.json", "packaging": "file" @@ -9,7 +9,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "88b21afc833974331e24a331feac58d1ab1f74688025e5e90d7b77bdf6d33820.json", + "objectKey": "4e1c8f560b3bd32ad0758c6b7d04153c21111750f33dcfff05c52796bc71f9de.json", "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" } } diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json index 23c72da8a9e9c..4ed772d3969f5 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/LambdaAlarmActionIntegrationTestStack.template.json @@ -87,95 +87,6 @@ "TreatMissingData": "notBreaching" } }, - "AlarmCurrentVersionAlarmPermission22DB4772": { - "Type": "AWS::Lambda::Permission", - "Properties": { - "Action": "lambda:InvokeFunction", - "FunctionName": { - "Fn::Join": [ - "", - [ - { - "Ref": "alarmLambda131DB691" - }, - ":", - { - "Fn::GetAtt": [ - "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b", - "Version" - ] - } - ] - ] - }, - "Principal": "lambda.alarms.cloudwatch.amazonaws.com", - "SourceAccount": { - "Ref": "AWS::AccountId" - }, - "SourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, - "AlarmAliasaliasNameAlarmPermission3887D390": { - "Type": "AWS::Lambda::Permission", - "Properties": { - "Action": "lambda:InvokeFunction", - "FunctionName": { - "Fn::Join": [ - "", - [ - { - "Fn::Select": [ - 6, - { - "Fn::Split": [ - ":", - { - "Ref": "alarmLambdaAliasaliasName41B27313" - } - ] - } - ] - }, - ":aliasName" - ] - ] - }, - "Principal": "lambda.alarms.cloudwatch.amazonaws.com", - "SourceAccount": { - "Ref": "AWS::AccountId" - }, - "SourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, - "AlarmalarmLambdaAlarmPermission5CAF3DE7": { - "Type": "AWS::Lambda::Permission", - "Properties": { - "Action": "lambda:InvokeFunction", - "FunctionName": { - "Ref": "alarmLambda131DB691" - }, - "Principal": "lambda.alarms.cloudwatch.amazonaws.com", - "SourceAccount": { - "Ref": "AWS::AccountId" - }, - "SourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, "alarmLambdaServiceRoleCDAABB9D": { "Type": "AWS::IAM::Role", "Properties": { @@ -235,6 +146,25 @@ } } }, + "alarmLambdaCurrentVersionAlarmPermissionFEBD056F": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Ref": "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b" + }, + "Principal": "lambda.alarms.cloudwatch.amazonaws.com", + "SourceAccount": { + "Ref": "AWS::AccountId" + }, + "SourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, "alarmLambdaAliasaliasName41B27313": { "Type": "AWS::Lambda::Alias", "Properties": { @@ -249,6 +179,47 @@ }, "Name": "aliasName" } + }, + "alarmLambdaAliasaliasNameAlarmPermission64A91652": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Ref": "alarmLambdaAliasaliasName41B27313" + }, + "Principal": "lambda.alarms.cloudwatch.amazonaws.com", + "SourceAccount": { + "Ref": "AWS::AccountId" + }, + "SourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "alarmLambdaAlarmPermission43E41C89": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunction", + "FunctionName": { + "Fn::GetAtt": [ + "alarmLambda131DB691", + "Arn" + ] + }, + "Principal": "lambda.alarms.cloudwatch.amazonaws.com", + "SourceAccount": { + "Ref": "AWS::AccountId" + }, + "SourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } } }, "Parameters": { diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json index 3007ab0eab94d..f7d89d600b988 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/manifest.json @@ -18,7 +18,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}/88b21afc833974331e24a331feac58d1ab1f74688025e5e90d7b77bdf6d33820.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/4e1c8f560b3bd32ad0758c6b7d04153c21111750f33dcfff05c52796bc71f9de.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ @@ -52,46 +52,52 @@ "data": "Alarm7103F465" } ], - "/LambdaAlarmActionIntegrationTestStack/Alarm/CurrentVersionAlarmPermission": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda": [ + { + "type": "aws:cdk:warning", + "data": "AWS Lambda has changed their authorization strategy, which may cause client invocations using the 'Qualifier' parameter of the lambda function to fail with Access Denied errors.\nIf you are using a lambda Version or Alias, make sure to call 'grantInvoke' or 'addPermission' on the Version or Alias, not the underlying Function\nSee: https://github.com/aws/aws-cdk/issues/19273 [ack: @aws-cdk/aws-lambda:addPermissionsToVersionOrAlias]" + } + ], + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/ServiceRole/Resource": [ { "type": "aws:cdk:logicalId", - "data": "AlarmCurrentVersionAlarmPermission22DB4772" + "data": "alarmLambdaServiceRoleCDAABB9D" } ], - "/LambdaAlarmActionIntegrationTestStack/Alarm/AliasaliasNameAlarmPermission": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/Resource": [ { "type": "aws:cdk:logicalId", - "data": "AlarmAliasaliasNameAlarmPermission3887D390" + "data": "alarmLambda131DB691" } ], - "/LambdaAlarmActionIntegrationTestStack/Alarm/alarmLambdaAlarmPermission": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/Resource": [ { "type": "aws:cdk:logicalId", - "data": "AlarmalarmLambdaAlarmPermission5CAF3DE7" + "data": "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b" } ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda/ServiceRole/Resource": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/AlarmPermission": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaServiceRoleCDAABB9D" + "data": "alarmLambdaCurrentVersionAlarmPermissionFEBD056F" } ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda/Resource": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/Resource": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambda131DB691" + "data": "alarmLambdaAliasaliasName41B27313" } ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/Resource": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/AlarmPermission": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b" + "data": "alarmLambdaAliasaliasNameAlarmPermission64A91652" } ], - "/LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/Resource": [ + "/LambdaAlarmActionIntegrationTestStack/alarmLambda/AlarmPermission": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaAliasaliasName41B27313" + "data": "alarmLambdaAlarmPermission43E41C89" } ], "/LambdaAlarmActionIntegrationTestStack/BootstrapVersion": [ @@ -106,28 +112,28 @@ "data": "CheckBootstrapVersion" } ], - "alarmLambdaCurrentVersionCurrentVersionAlarmPermission274CB36D": [ + "AlarmCurrentVersionAlarmPermission22DB4772": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaCurrentVersionCurrentVersionAlarmPermission274CB36D", + "data": "AlarmCurrentVersionAlarmPermission22DB4772", "trace": [ "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" ] } ], - "alarmLambdaAliasaliasNameAliasaliasNameAlarmPermissionED9EC624": [ + "AlarmAliasaliasNameAlarmPermission3887D390": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaAliasaliasNameAliasaliasNameAlarmPermissionED9EC624", + "data": "AlarmAliasaliasNameAlarmPermission3887D390", "trace": [ "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" ] } ], - "alarmLambdaalarmLambdaAlarmPermissionA0BF56DE": [ + "AlarmalarmLambdaAlarmPermission5CAF3DE7": [ { "type": "aws:cdk:logicalId", - "data": "alarmLambdaalarmLambdaAlarmPermissionA0BF56DE", + "data": "AlarmalarmLambdaAlarmPermission5CAF3DE7", "trace": [ "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" ] diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json index 5dd118e34d395..622364ad86c22 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch-actions/test/integ.lambda-alarm-action.js.snapshot/tree.json @@ -147,119 +147,6 @@ "fqn": "aws-cdk-lib.aws_cloudwatch.CfnAlarm", "version": "0.0.0" } - }, - "CurrentVersionAlarmPermission": { - "id": "CurrentVersionAlarmPermission", - "path": "LambdaAlarmActionIntegrationTestStack/Alarm/CurrentVersionAlarmPermission", - "attributes": { - "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", - "aws:cdk:cloudformation:props": { - "action": "lambda:InvokeFunction", - "functionName": { - "Fn::Join": [ - "", - [ - { - "Ref": "alarmLambda131DB691" - }, - ":", - { - "Fn::GetAtt": [ - "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b", - "Version" - ] - } - ] - ] - }, - "principal": "lambda.alarms.cloudwatch.amazonaws.com", - "sourceAccount": { - "Ref": "AWS::AccountId" - }, - "sourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, - "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", - "version": "0.0.0" - } - }, - "AliasaliasNameAlarmPermission": { - "id": "AliasaliasNameAlarmPermission", - "path": "LambdaAlarmActionIntegrationTestStack/Alarm/AliasaliasNameAlarmPermission", - "attributes": { - "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", - "aws:cdk:cloudformation:props": { - "action": "lambda:InvokeFunction", - "functionName": { - "Fn::Join": [ - "", - [ - { - "Fn::Select": [ - 6, - { - "Fn::Split": [ - ":", - { - "Ref": "alarmLambdaAliasaliasName41B27313" - } - ] - } - ] - }, - ":aliasName" - ] - ] - }, - "principal": "lambda.alarms.cloudwatch.amazonaws.com", - "sourceAccount": { - "Ref": "AWS::AccountId" - }, - "sourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, - "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", - "version": "0.0.0" - } - }, - "alarmLambdaAlarmPermission": { - "id": "alarmLambdaAlarmPermission", - "path": "LambdaAlarmActionIntegrationTestStack/Alarm/alarmLambdaAlarmPermission", - "attributes": { - "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", - "aws:cdk:cloudformation:props": { - "action": "lambda:InvokeFunction", - "functionName": { - "Ref": "alarmLambda131DB691" - }, - "principal": "lambda.alarms.cloudwatch.amazonaws.com", - "sourceAccount": { - "Ref": "AWS::AccountId" - }, - "sourceArn": { - "Fn::GetAtt": [ - "Alarm7103F465", - "Arn" - ] - } - } - }, - "constructInfo": { - "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", - "version": "0.0.0" - } } }, "constructInfo": { @@ -372,6 +259,33 @@ "fqn": "aws-cdk-lib.aws_lambda.CfnVersion", "version": "0.0.0" } + }, + "AlarmPermission": { + "id": "AlarmPermission", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/CurrentVersion/AlarmPermission", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Ref": "alarmLambdaCurrentVersionBDCE825C61967a018dfe55c69152d31b6e7ab40b" + }, + "principal": "lambda.alarms.cloudwatch.amazonaws.com", + "sourceAccount": { + "Ref": "AWS::AccountId" + }, + "sourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } } }, "constructInfo": { @@ -413,12 +327,69 @@ "fqn": "aws-cdk-lib.Resource", "version": "0.0.0" } + }, + "AlarmPermission": { + "id": "AlarmPermission", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/AliasaliasName/AlarmPermission", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Ref": "alarmLambdaAliasaliasName41B27313" + }, + "principal": "lambda.alarms.cloudwatch.amazonaws.com", + "sourceAccount": { + "Ref": "AWS::AccountId" + }, + "sourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } } }, "constructInfo": { "fqn": "aws-cdk-lib.aws_lambda.Alias", "version": "0.0.0" } + }, + "AlarmPermission": { + "id": "AlarmPermission", + "path": "LambdaAlarmActionIntegrationTestStack/alarmLambda/AlarmPermission", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Permission", + "aws:cdk:cloudformation:props": { + "action": "lambda:InvokeFunction", + "functionName": { + "Fn::GetAtt": [ + "alarmLambda131DB691", + "Arn" + ] + }, + "principal": "lambda.alarms.cloudwatch.amazonaws.com", + "sourceAccount": { + "Ref": "AWS::AccountId" + }, + "sourceArn": { + "Fn::GetAtt": [ + "Alarm7103F465", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnPermission", + "version": "0.0.0" + } } }, "constructInfo": { From dc13eeca0b9ecabe50447958dc11b9b4a2084898 Mon Sep 17 00:00:00 2001 From: Neil Kuan Date: Thu, 4 Jan 2024 16:16:47 +0800 Subject: [PATCH 19/22] chore: typo --- packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts index 41e589060dc49..069ab42768833 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts @@ -16,13 +16,11 @@ export class LambdaAction implements cloudwatch.IAlarmAction { } /** - * Returns an alarm action configuration to use an Lambda action as an alarm action + * Returns an alarm action configuration to use an Lambda action as an alarm action. * * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricAlarm.html - * */ bind(_scope: Construct, _alarm: cloudwatch.IAlarm): cloudwatch.AlarmActionConfig { - this.lambdaFunction.addPermission('AlarmPermission', { sourceAccount: Stack.of(_scope).account, action: 'lambda:InvokeFunction', From 36f0b154e868c91848704c82054d8c6b568967ec Mon Sep 17 00:00:00 2001 From: paulhcsun <47882901+paulhcsun@users.noreply.github.com> Date: Wed, 10 Jan 2024 16:44:55 -0800 Subject: [PATCH 20/22] Small grammar change --- packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts index 069ab42768833..7913aaead7551 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts @@ -5,7 +5,7 @@ import * as lambda from '../../aws-lambda'; import { Stack } from '../../core'; /** - * Use an Lambda action as an Alarm action + * Use a Lambda action as an Alarm action */ export class LambdaAction implements cloudwatch.IAlarmAction { private lambdaFunction: lambda.IAlias | lambda.IVersion | lambda.IFunction From c7468eeda915c86af8a9611ed41ba70e260aab8c Mon Sep 17 00:00:00 2001 From: paulhcsun <47882901+paulhcsun@users.noreply.github.com> Date: Wed, 10 Jan 2024 16:45:06 -0800 Subject: [PATCH 21/22] Small grammar change --- packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts index 7913aaead7551..f754b60b3f1a5 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts @@ -16,7 +16,7 @@ export class LambdaAction implements cloudwatch.IAlarmAction { } /** - * Returns an alarm action configuration to use an Lambda action as an alarm action. + * Returns an alarm action configuration to use a Lambda action as an alarm action. * * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricAlarm.html */ From d4c760f7c8fcdd2e6a2da2bf6414c1334b00db69 Mon Sep 17 00:00:00 2001 From: Neil Kuan Date: Thu, 11 Jan 2024 09:32:02 +0800 Subject: [PATCH 22/22] chore: add description for lambda alias and version --- .../aws-cdk-lib/aws-cloudwatch-actions/README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md b/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md index 5499de4410264..d7bcb9ef651c5 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md +++ b/packages/aws-cdk-lib/aws-cloudwatch-actions/README.md @@ -46,10 +46,24 @@ alarm.addAlarmAction( import * as lambda from 'aws-cdk-lib/aws-lambda' declare const alarm: cloudwatch.Alarm; declare const fn: lambda.Function; +declare const alias: lambda.Alias; +declare const version: lambda.Version; +// Attach a Lambda Function when alarm triggers alarm.addAlarmAction( new actions.LambdaAction(fn) ); + +// Attach a Lambda Function Alias when alarm triggers +alarm.addAlarmAction( + new actions.LambdaAction(alias) +); + +// Attach a Lambda Function version when alarm triggers +alarm.addAlarmAction( + new actions.LambdaAction(version) +); + ``` See `aws-cdk-lib/aws-cloudwatch` for more information.