-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from guardian/jw-scheduled-lambda
Add pattern for scheduled lambdas
- v-test
- v61.1.2
- v61.1.1
- v61.1.0
- v61.0.2
- v61.0.1
- v61.0.0
- v60.1.3
- v60.1.2
- v60.1.1
- v60.1.0
- v60.0.0
- v59.5.6
- v59.5.5
- v59.5.4
- v59.5.3
- v59.5.2
- v59.5.1
- v59.5.0
- v59.4.0
- v59.3.5
- v59.3.4
- v59.3.3
- v59.3.2
- v59.3.1
- v59.3.0
- v59.2.4
- v59.2.3
- v59.2.2
- v59.2.1
- v59.2.0
- v59.1.0
- v59.0.0
- v58.2.0
- v58.1.4
- v58.1.3
- v58.1.2
- v58.1.1
- v58.1.0
- v58.0.0
- v57.1.0
- v57.0.0
- v56.0.3
- v56.0.2
- v56.0.1
- v56.0.0
- v55.0.0
- v54.1.0
- v54.0.0
- v53.1.1
- v53.1.0
- v53.0.3
- v53.0.2
- v53.0.1
- v53.0.0
- v52.3.1
- v52.3.0
- v52.2.1
- v52.2.0
- v52.1.0
- v52.0.0
- v51.0.0
- v50.13.0
- v50.12.3
- v50.12.2
- v50.12.1
- v50.12.0
- v50.12.0-beta.1
- v50.11.3
- v50.11.2
- v50.11.1
- v50.11.0
- v50.11.0-beta.1
- v50.10.16
- v50.10.15
- v50.10.14
- v50.10.13
- v50.10.12
- v50.10.11
- v50.10.10
- v50.10.9
- v50.10.8
- v50.10.7
- v50.10.6
- v50.10.5
- v50.10.4
- v50.10.3
- v50.10.2
- v50.10.1
- v50.10.0
- v50.9.0
- v50.8.2
- v50.8.1
- v50.8.0
- v50.7.0
- v50.6.2
- v50.6.1
- v50.6.1-beta.1
- v50.6.0
- v50.5.0
- v50.4.0
- v50.4.0-beta.4
- v50.4.0-beta.3
- v50.4.0-beta.2
- v50.4.0-beta.1
- v50.3.3
- v50.3.2
- v50.3.1
- v50.3.0
- v50.2.0
- v50.1.0
- v50.0.0
- v50.0.0-beta.1
- v49.6.0
- v49.5.0
- v49.4.1
- v49.4.0
- v49.3.0
- v49.3.0-beta.1
- v49.2.1
- v49.2.0
- v49.1.0
- v49.0.3
- v49.0.2
- v49.0.2-beta.1
- v49.0.1
- v49.0.0
- v49.0.0-beta.1
- v48.6.0-beta.2
- v48.6.0-beta.1
- v48.5.3
- v48.5.2
- v48.5.1
- v48.5.1-beta.1
- v48.5.0
- v48.5.0-beta.1
- v48.4.2-beta.1
- v48.4.1
- v48.4.0
- v48.3.1
- v48.3.0
- v48.2.2
- v48.2.1
- v48.2.0
- v48.2.0-beta.1
- v48.1.1-beta.1
- v48.1.0
- v48.0.1
- v48.0.0
- v47.3.5
- v47.3.4
- v47.3.3
- v47.3.2
- v47.3.1
- v47.3.0
- v47.2.2
- v47.2.1
- v47.2.0
- v47.1.0
- v47.1.0-beta.1
- v47.0.4
- v47.0.3
- v47.0.2
- v47.0.1
- v47.0.0
- v46.1.0-beta.2
- v46.1.0-beta.1
- v46.0.1
- v46.0.0
- v45.1.3
- v45.1.2
- v45.1.1
- v45.1.0
- v45.0.0
- v44.0.0
- v43.4.0
- v43.4.0-beta.2
- v43.4.0-beta.1
- v43.3.1
- v43.3.1-beta.1
- v43.3.0
- v43.2.0
- v43.1.0
- v43.0.0
- v42.0.0
- v42.0.0-beta.1
- v41.1.0
- v41.1.0-beta.1
- v41.0.2
- v41.0.2-beta.1
- v41.0.1
- v41.0.0
- v40.0.1
- v40.0.0
- v39.0.0
- v39.0.0-beta.2
- v39.0.0-beta.1
- v38.2.1
- v38.2.0
- v38.1.0
- v38.0.0
- v38.0.0-beta.1
- v37.1.1
- v37.1.0
- v37.0.0
- v37.0.0-beta.1
- v36.1.0
- v36.0.0
- v36.0.0-beta.1
- v35.0.2
- v35.0.1
- v35.0.0
- v34.3.0-beta.1
- v34.2.0
- v34.1.0
- v34.1.0-beta.3
- v34.1.0-beta.2
- v34.1.0-beta.1
- v34.0.3-beta.1
- v34.0.2
- v34.0.1
- v34.0.0
- v33.2.3
- v33.2.2
- v33.2.1
- v33.2.0
- v33.1.0
- v33.0.0
- v32.2.2
- v32.2.1
- v32.2.0
- v32.1.1-beta.1
- v32.1.0
- v32.0.1
- v32.0.0
- v31.5.0
- v31.4.1
- v31.4.0
- v31.3.0
- v31.2.0
- v31.1.0
- v31.0.0
- v30.0.0
- v30.0.0-beta.1
- v29.1.0
- v29.0.0
- v28.0.0
- v27.2.1
- v27.2.0
- v27.1.0
- v27.0.0
- v26.3.1
- v26.3.0
- v26.2.1
- v26.2.0
- v26.1.0
- v26.0.2
- v26.0.1
- v26.0.0
- v25.1.0
- v25.0.1
- v25.0.0
- v24.1.0
- v24.0.1
- v24.0.0
- v23.5.1
- v23.5.0
- v23.4.1
- v23.4.0
- v23.3.0
- v23.2.0
- v23.1.0
- v23.0.0
- v22.3.0
- v22.2.0
- v22.1.0
- v22.1.0-beta.1
- v22.0.1
- v22.0.0
- v21.0.1
- v21.0.0
- v20.0.1
- v20.0.0
- v19.4.0
- v19.3.0
- v19.2.1
- v19.2.0
- v19.1.0
- v19.0.1
- v19.0.0
- v18.1.0
- v18.0.0
- v17.0.0
- v16.2.0
- v16.1.0
- v16.0.0
- v15.1.2
- v15.1.1
- v15.1.0
- v15.0.1
- v15.0.0
- v14.5.0
- v14.4.1
- v14.4.0
- v14.3.0
- v14.2.0
- v14.1.0
- v14.0.1
- v14.0.0
- v13.1.0
- v13.0.0
- v12.0.0
- v11.0.0
- v10.0.0
- v9.2.0
- v9.1.0
- v9.0.0
- v8.5.0
- v8.4.2
- v8.4.1
- v8.4.0
- v8.3.0
- v8.2.0
- v8.1.4
- v8.1.3
- v8.1.2
- v8.1.1
- v8.1.0
- v8.0.2
- v8.0.1
- v8.0.0
- v7.1.2
- v7.1.1
- v7.1.0
- v7.0.0
- v6.4.0
- v6.3.2
- v6.3.1
- v6.3.0
- v6.2.3
- v6.2.2
- v6.2.1
- v6.2.0
- v6.1.0
- v6.0.1
- v6.0.0
- v5.0.0
- v4.0.0
- v3.0.1
- v3.0.0
- v2.0.0
- v1.0.0
- v0.40.0
- v0.39.2
- v0.39.1
- v0.39.0
- v0.38.1
- v0.38.0
- v0.37.0
- v0.36.0
- v0.35.0
- v0.34.0
- v0.33.0
- v0.32.0
- v0.31.0
- v0.30.0
- v0.29.0
- v0.28.0
- v0.27.0
- v0.26.0
- list
Showing
11 changed files
with
1,082 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
273 changes: 273 additions & 0 deletions
273
src/constructs/cloudwatch/__snapshots__/lambda-alarms.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,273 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`The GuLambdaErrorPercentageAlarm pattern should create the correct alarm resource with minimal config 1`] = ` | ||
Object { | ||
"Parameters": Object { | ||
"Stack": Object { | ||
"Default": "deploy", | ||
"Description": "Name of this stack", | ||
"Type": "String", | ||
}, | ||
"Stage": Object { | ||
"AllowedValues": Array [ | ||
"CODE", | ||
"PROD", | ||
], | ||
"Default": "CODE", | ||
"Description": "Stage name", | ||
"Type": "String", | ||
}, | ||
}, | ||
"Resources": Object { | ||
"lambda8B5974B5": Object { | ||
"DependsOn": Array [ | ||
"lambdaServiceRoleDefaultPolicyBF6FA5E7", | ||
"lambdaServiceRole494E4CA6", | ||
], | ||
"Properties": Object { | ||
"Code": Object { | ||
"S3Bucket": "bucket1", | ||
"S3Key": "folder/to/key", | ||
}, | ||
"Handler": "handler.ts", | ||
"MemorySize": 512, | ||
"Role": Object { | ||
"Fn::GetAtt": Array [ | ||
"lambdaServiceRole494E4CA6", | ||
"Arn", | ||
], | ||
}, | ||
"Runtime": "nodejs12.x", | ||
"Tags": Array [ | ||
Object { | ||
"Key": "App", | ||
"Value": "testing", | ||
}, | ||
Object { | ||
"Key": "Stack", | ||
"Value": Object { | ||
"Ref": "Stack", | ||
}, | ||
}, | ||
Object { | ||
"Key": "Stage", | ||
"Value": Object { | ||
"Ref": "Stage", | ||
}, | ||
}, | ||
], | ||
"Timeout": 30, | ||
}, | ||
"Type": "AWS::Lambda::Function", | ||
}, | ||
"lambdaServiceRole494E4CA6": Object { | ||
"Properties": Object { | ||
"AssumeRolePolicyDocument": Object { | ||
"Statement": Array [ | ||
Object { | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": Object { | ||
"Service": "lambda.amazonaws.com", | ||
}, | ||
}, | ||
], | ||
"Version": "2012-10-17", | ||
}, | ||
"ManagedPolicyArns": Array [ | ||
Object { | ||
"Fn::Join": Array [ | ||
"", | ||
Array [ | ||
"arn:", | ||
Object { | ||
"Ref": "AWS::Partition", | ||
}, | ||
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole", | ||
], | ||
], | ||
}, | ||
], | ||
"Tags": Array [ | ||
Object { | ||
"Key": "App", | ||
"Value": "testing", | ||
}, | ||
Object { | ||
"Key": "Stack", | ||
"Value": Object { | ||
"Ref": "Stack", | ||
}, | ||
}, | ||
Object { | ||
"Key": "Stage", | ||
"Value": Object { | ||
"Ref": "Stage", | ||
}, | ||
}, | ||
], | ||
}, | ||
"Type": "AWS::IAM::Role", | ||
}, | ||
"lambdaServiceRoleDefaultPolicyBF6FA5E7": Object { | ||
"Properties": Object { | ||
"PolicyDocument": Object { | ||
"Statement": Array [ | ||
Object { | ||
"Action": Array [ | ||
"s3:GetObject*", | ||
"s3:GetBucket*", | ||
"s3:List*", | ||
], | ||
"Effect": "Allow", | ||
"Resource": Array [ | ||
Object { | ||
"Fn::Join": Array [ | ||
"", | ||
Array [ | ||
"arn:", | ||
Object { | ||
"Ref": "AWS::Partition", | ||
}, | ||
":s3:::bucket1", | ||
], | ||
], | ||
}, | ||
Object { | ||
"Fn::Join": Array [ | ||
"", | ||
Array [ | ||
"arn:", | ||
Object { | ||
"Ref": "AWS::Partition", | ||
}, | ||
":s3:::bucket1/*", | ||
], | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
"Version": "2012-10-17", | ||
}, | ||
"PolicyName": "lambdaServiceRoleDefaultPolicyBF6FA5E7", | ||
"Roles": Array [ | ||
Object { | ||
"Ref": "lambdaServiceRole494E4CA6", | ||
}, | ||
], | ||
}, | ||
"Type": "AWS::IAM::Policy", | ||
}, | ||
"mylambdafunction8D341B54": Object { | ||
"Properties": Object { | ||
"AlarmActions": Array [ | ||
Object { | ||
"Fn::Join": Array [ | ||
"", | ||
Array [ | ||
"arn:aws:sns:", | ||
Object { | ||
"Ref": "AWS::Region", | ||
}, | ||
":", | ||
Object { | ||
"Ref": "AWS::AccountId", | ||
}, | ||
":alerts-topic", | ||
], | ||
], | ||
}, | ||
], | ||
"AlarmDescription": Object { | ||
"Fn::Join": Array [ | ||
"", | ||
Array [ | ||
Object { | ||
"Ref": "lambda8B5974B5", | ||
}, | ||
" exceeded 80% error rate", | ||
], | ||
], | ||
}, | ||
"AlarmName": Object { | ||
"Fn::Join": Array [ | ||
"", | ||
Array [ | ||
"High error % from ", | ||
Object { | ||
"Ref": "lambda8B5974B5", | ||
}, | ||
" lambda in ", | ||
Object { | ||
"Ref": "Stage", | ||
}, | ||
], | ||
], | ||
}, | ||
"ComparisonOperator": "GreaterThanThreshold", | ||
"EvaluationPeriods": 1, | ||
"Metrics": Array [ | ||
Object { | ||
"Expression": "100*m1/m2", | ||
"Id": "expr_1", | ||
"Label": Object { | ||
"Fn::Join": Array [ | ||
"", | ||
Array [ | ||
"Error % of ", | ||
Object { | ||
"Ref": "lambda8B5974B5", | ||
}, | ||
], | ||
], | ||
}, | ||
}, | ||
Object { | ||
"Id": "m1", | ||
"MetricStat": Object { | ||
"Metric": Object { | ||
"Dimensions": Array [ | ||
Object { | ||
"Name": "FunctionName", | ||
"Value": Object { | ||
"Ref": "lambda8B5974B5", | ||
}, | ||
}, | ||
], | ||
"MetricName": "Errors", | ||
"Namespace": "AWS/Lambda", | ||
}, | ||
"Period": 300, | ||
"Stat": "Sum", | ||
}, | ||
"ReturnData": false, | ||
}, | ||
Object { | ||
"Id": "m2", | ||
"MetricStat": Object { | ||
"Metric": Object { | ||
"Dimensions": Array [ | ||
Object { | ||
"Name": "FunctionName", | ||
"Value": Object { | ||
"Ref": "lambda8B5974B5", | ||
}, | ||
}, | ||
], | ||
"MetricName": "Invocations", | ||
"Namespace": "AWS/Lambda", | ||
}, | ||
"Period": 300, | ||
"Stat": "Sum", | ||
}, | ||
"ReturnData": false, | ||
}, | ||
], | ||
"Threshold": 80, | ||
}, | ||
"Type": "AWS::CloudWatch::Alarm", | ||
}, | ||
}, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import "@aws-cdk/assert/jest"; | ||
import { ComparisonOperator } from "@aws-cdk/aws-cloudwatch"; | ||
import { Runtime } from "@aws-cdk/aws-lambda"; | ||
import { simpleGuStackForTesting } from "../../../test/utils"; | ||
import { GuLambdaFunction } from "../lambda"; | ||
import { GuAlarm } from "./alarm"; | ||
|
||
describe("The GuAlarm class", () => { | ||
it("should create a CloudWatch alarm", () => { | ||
const stack = simpleGuStackForTesting(); | ||
const lambda = new GuLambdaFunction(stack, "lambda", { | ||
code: { bucket: "bucket1", key: "folder/to/key" }, | ||
handler: "handler.ts", | ||
runtime: Runtime.NODEJS_12_X, | ||
}); | ||
new GuAlarm(stack, "alarm", { | ||
alarmName: `Alarm in ${stack.stage}`, | ||
alarmDescription: "It's broken", | ||
metric: lambda.metricErrors(), | ||
comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD, | ||
threshold: 1, | ||
evaluationPeriods: 1, | ||
snsTopicName: "alerts-topic", | ||
}); | ||
expect(stack).toHaveResource("AWS::CloudWatch::Alarm"); | ||
}); | ||
|
||
it("should send alerts to the provided SNS Topic", () => { | ||
const stack = simpleGuStackForTesting(); | ||
const lambda = new GuLambdaFunction(stack, "lambda", { | ||
code: { bucket: "bucket1", key: "folder/to/key" }, | ||
handler: "handler.ts", | ||
runtime: Runtime.NODEJS_12_X, | ||
}); | ||
new GuAlarm(stack, "alarm", { | ||
alarmName: `Alarm in ${stack.stage}`, | ||
alarmDescription: "It's broken", | ||
metric: lambda.metricErrors(), | ||
comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD, | ||
threshold: 1, | ||
evaluationPeriods: 1, | ||
snsTopicName: "alerts-topic", | ||
}); | ||
expect(stack).toHaveResource("AWS::CloudWatch::Alarm", { | ||
AlarmActions: [ | ||
{ | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"arn:aws:sns:", | ||
{ | ||
Ref: "AWS::Region", | ||
}, | ||
":", | ||
{ | ||
Ref: "AWS::AccountId", | ||
}, | ||
":alerts-topic", | ||
], | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { AlarmProps } from "@aws-cdk/aws-cloudwatch"; | ||
import { Alarm } from "@aws-cdk/aws-cloudwatch"; | ||
import { SnsAction } from "@aws-cdk/aws-cloudwatch-actions"; | ||
import type { ITopic } from "@aws-cdk/aws-sns"; | ||
import { Topic } from "@aws-cdk/aws-sns"; | ||
import type { GuStack } from "../core"; | ||
|
||
export interface GuAlarmProps extends AlarmProps { | ||
snsTopicName: string; | ||
} | ||
|
||
export class GuAlarm extends Alarm { | ||
constructor(scope: GuStack, id: string, props: GuAlarmProps) { | ||
super(scope, id, props); | ||
const topicArn: string = `arn:aws:sns:${scope.region}:${scope.account}:${props.snsTopicName}`; | ||
const snsTopic: ITopic = Topic.fromTopicArn(scope, "sns-topic-for-alarm-notifications", topicArn); | ||
this.addAlarmAction(new SnsAction(snsTopic)); | ||
} | ||
} |
Oops, something went wrong.