Skip to content

Commit ba9e381

Browse files
committed
specify alarm conditions, improve readability of policy allowing lambda to send emails using SES
1 parent 8811fc9 commit ba9e381

File tree

2 files changed

+21
-58
lines changed

2 files changed

+21
-58
lines changed

ab-testing/cdk/lib/__snapshots__/notificationLambda.test.ts.snap

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,10 @@ exports[`The AB testing notification lambda stack > matches the CODE snapshot 1`
265265
}
266266
},
267267
{
268-
"Action": "ses:SendEmail",
268+
"Action": [
269+
"ses:SendEmail",
270+
"ses:SendRawEmail"
271+
],
269272
"Effect": "Allow",
270273
"Resource": {
271274
"Fn::Join": [
@@ -383,9 +386,10 @@ exports[`The AB testing notification lambda stack > matches the CODE snapshot 1`
383386
]
384387
}
385388
],
386-
"AlarmDescription": "Something went wrong notifying test owners of upcoming AB test expiries in the CODE AB Testing Notification Lambda. Please check the logs",
387-
"AlarmName": "AB Testing Notification Lambda Failures",
389+
"AlarmDescription": "Something went wrong notifying test owners of upcoming AB test expiries in ab-testing-notification-lambda-CODE. Please check the logs",
390+
"AlarmName": "AB Testing Notification Failures",
388391
"ComparisonOperator": "GreaterThanThreshold",
392+
"DatapointsToAlarm": 1,
389393
"EvaluationPeriods": 1,
390394
"Metrics": [
391395
{
@@ -445,26 +449,6 @@ exports[`The AB testing notification lambda stack > matches the CODE snapshot 1`
445449
"ReturnData": false
446450
}
447451
],
448-
"OKActions": [
449-
{
450-
"Fn::Join": [
451-
"",
452-
[
453-
"arn:aws:sns:eu-west-1:",
454-
{
455-
"Ref": "AWS::AccountId"
456-
},
457-
":",
458-
{
459-
"Fn::GetAtt": [
460-
"AbTestingNotificationSnsTopicB3559144",
461-
"TopicName"
462-
]
463-
}
464-
]
465-
]
466-
}
467-
],
468452
"Tags": [
469453
{
470454
"Key": "gu:cdk:version",
@@ -775,7 +759,10 @@ exports[`The AB testing notification lambda stack > matches the PROD snapshot 1`
775759
}
776760
},
777761
{
778-
"Action": "ses:SendEmail",
762+
"Action": [
763+
"ses:SendEmail",
764+
"ses:SendRawEmail"
765+
],
779766
"Effect": "Allow",
780767
"Resource": {
781768
"Fn::Join": [
@@ -953,9 +940,10 @@ exports[`The AB testing notification lambda stack > matches the PROD snapshot 1`
953940
]
954941
}
955942
],
956-
"AlarmDescription": "Something went wrong notifying test owners of upcoming AB test expiries in the PROD AB Testing Notification Lambda. Please check the logs",
957-
"AlarmName": "AB Testing Notification Lambda Failures",
943+
"AlarmDescription": "Something went wrong notifying test owners of upcoming AB test expiries in ab-testing-notification-lambda-PROD. Please check the logs",
944+
"AlarmName": "AB Testing Notification Failures",
958945
"ComparisonOperator": "GreaterThanThreshold",
946+
"DatapointsToAlarm": 1,
959947
"EvaluationPeriods": 1,
960948
"Metrics": [
961949
{
@@ -1015,26 +1003,6 @@ exports[`The AB testing notification lambda stack > matches the PROD snapshot 1`
10151003
"ReturnData": false
10161004
}
10171005
],
1018-
"OKActions": [
1019-
{
1020-
"Fn::Join": [
1021-
"",
1022-
[
1023-
"arn:aws:sns:eu-west-1:",
1024-
{
1025-
"Ref": "AWS::AccountId"
1026-
},
1027-
":",
1028-
{
1029-
"Fn::GetAtt": [
1030-
"AbTestingNotificationSnsTopicB3559144",
1031-
"TopicName"
1032-
]
1033-
}
1034-
]
1035-
]
1036-
}
1037-
],
10381006
"Tags": [
10391007
{
10401008
"Key": "gu:cdk:version",

ab-testing/cdk/lib/notificationLambda.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import {
44
type GuStackProps,
55
} from "@guardian/cdk/lib/constructs/core/stack.js";
66
import { GuEmailIdentity } from "@guardian/cdk/lib/constructs/ses/index.js";
7-
import type { App } from "aws-cdk-lib";
7+
import { type App, Duration } from "aws-cdk-lib";
88
import { Schedule } from "aws-cdk-lib/aws-events";
9-
import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
109
import { Runtime } from "aws-cdk-lib/aws-lambda";
1110
import { Subscription, SubscriptionProtocol, Topic } from "aws-cdk-lib/aws-sns";
1211

@@ -61,9 +60,11 @@ export class AbTestingNotificationLambda extends GuStack {
6160
monitoringConfiguration: {
6261
snsTopicName: snsTopic.topicName,
6362
toleratedErrorPercentage: 0,
64-
alarmName: "AB Testing Notification Lambda Failures",
65-
alarmDescription: `Something went wrong notifying test owners of upcoming AB test expiries in the ${props.stage} AB Testing Notification Lambda. Please check the logs`,
66-
okAction: true,
63+
alarmName: "AB Testing Notification Failures",
64+
alarmDescription: `Something went wrong notifying test owners of upcoming AB test expiries in ${appName}-${props.stage}. Please check the logs`,
65+
lengthOfEvaluationPeriod: Duration.minutes(1),
66+
numberOfEvaluationPeriodsAboveThresholdBeforeAlarm: 1,
67+
datapointsToAlarm: 1,
6768
},
6869
runtime: Runtime.NODEJS_22_X,
6970
environment: {
@@ -73,12 +74,6 @@ export class AbTestingNotificationLambda extends GuStack {
7374
},
7475
);
7576

76-
lambda.addToRolePolicy(
77-
new PolicyStatement({
78-
effect: Effect.ALLOW,
79-
actions: ["ses:SendEmail"],
80-
resources: [emailIdentity.emailIdentityArn],
81-
}),
82-
);
77+
emailIdentity.grantSendEmail(lambda);
8378
}
8479
}

0 commit comments

Comments
 (0)