Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloudwatch-actions): support alarm lambda action #28484

Merged
merged 32 commits into from
Jan 11, 2024
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
7429bca
feat: cloudwatch alarm lambda action
neilkuan Dec 24, 2023
07f3987
chore: add testing
neilkuan Dec 24, 2023
a347097
chore: update code
neilkuan Jan 1, 2024
0fefaed
chore: add integ testing
neilkuan Jan 1, 2024
b895833
feat: cloudwatch alarm lambda action
neilkuan Dec 24, 2023
6cbfe8b
chore: add testing
neilkuan Dec 24, 2023
3756913
chore: update code
neilkuan Jan 1, 2024
d2e6e06
chore: add integ testing
neilkuan Jan 1, 2024
3403cfb
Merge branch 'support-cloudwatch-alarm-lambda-action' of https://gith…
neilkuan Jan 1, 2024
e8b5869
Merge branch 'main' into support-cloudwatch-alarm-lambda-action
kaizencc Jan 2, 2024
ad2939b
chore: update readme
neilkuan Jan 3, 2024
b8c33eb
chore: update readme
neilkuan Jan 3, 2024
dfb285f
Merge branch 'main' into support-cloudwatch-alarm-lambda-action
neilkuan Jan 3, 2024
83ff289
Merge branch 'main' into support-cloudwatch-alarm-lambda-action
neilkuan Jan 4, 2024
3a3e5b1
chore: update code from pr review
neilkuan Jan 4, 2024
6e5d592
chore: update snapshot
neilkuan Jan 4, 2024
17ec0a2
chore: update integ testing snapshot
neilkuan Jan 4, 2024
adced0d
Merge branch 'main' into support-cloudwatch-alarm-lambda-action
neilkuan Jan 4, 2024
3c5dea5
chore: let testing happy
neilkuan Jan 4, 2024
579c88b
chore: run integ test
neilkuan Jan 4, 2024
d8bde8a
update snapshots
pahud Jan 4, 2024
e634b2f
Merge branch 'support-cloudwatch-alarm-lambda-action' of https://gith…
pahud Jan 4, 2024
d8aded1
chore: update testing and comment
neilkuan Jan 4, 2024
b20de81
chore: let testing happy
neilkuan Jan 4, 2024
dc13eec
chore: typo
neilkuan Jan 4, 2024
ce176dc
Merge branch 'main' into support-cloudwatch-alarm-lambda-action
neilkuan Jan 4, 2024
36f0b15
Small grammar change
paulhcsun Jan 11, 2024
c7468ee
Small grammar change
paulhcsun Jan 11, 2024
d4c760f
chore: add description for lambda alias and version
neilkuan Jan 11, 2024
fb79f07
Merge branch 'main' into support-cloudwatch-alarm-lambda-action
neilkuan Jan 11, 2024
d876c52
Merge branch 'main' into support-cloudwatch-alarm-lambda-action
neilkuan Jan 11, 2024
7aec9fe
Merge branch 'main' into support-cloudwatch-alarm-lambda-action
paulhcsun Jan 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions packages/aws-cdk-lib/aws-cloudwatch-actions/lib/lambda.ts
Original file line number Diff line number Diff line change
@@ -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
paulhcsun marked this conversation as resolved.
Show resolved Hide resolved
*/
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
neilkuan marked this conversation as resolved.
Show resolved Hide resolved
*
*/
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}`,
};
}
}
}
Loading