Skip to content

Commit a06ee74

Browse files
Merge pull request #1719 from guardian/jajb/define-dynamoDB-in-cdk-not-yaml
Define dynamo table in cdk
2 parents abb5157 + c9c1f22 commit a06ee74

2 files changed

Lines changed: 33 additions & 74 deletions

File tree

cdk/lib/__snapshots__/footballnotificationslambda.test.ts.snap

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
exports[`The FootballNotificationsLambda stack matches the snapshot for CODE 1`] = `
44
{
5-
"AWSTemplateFormatVersion": "2010-09-09",
6-
"Description": "mobile-notifications-football",
75
"Metadata": {
86
"gu:cdk:constructs": [
97
"GuDistributionBucketParameter",
@@ -14,42 +12,15 @@ exports[`The FootballNotificationsLambda stack matches the snapshot for CODE 1`]
1412
"gu:cdk:version": "TEST",
1513
},
1614
"Parameters": {
17-
"App": {
18-
"Default": "football",
19-
"Description": "Application name",
20-
"Type": "String",
21-
},
22-
"DeployBucket": {
23-
"Default": "mobile-dist",
24-
"Description": "Bucket where RiffRaff uploads artifacts on deploy",
25-
"Type": "String",
26-
},
2715
"DistributionBucketName": {
2816
"Default": "/account/services/artifact.bucket",
2917
"Description": "SSM parameter containing the S3 bucket name holding distribution artifacts",
3018
"Type": "AWS::SSM::Parameter::Value<String>",
3119
},
32-
"DynamoNotificationTopic": {
33-
"Description": "SNS topic to notify when there's a dynamo throttling event",
34-
"Type": "String",
35-
},
36-
"Stack": {
37-
"Default": "mobile-notifications",
38-
"Description": "Stack name",
39-
"Type": "String",
40-
},
41-
"Stage": {
42-
"AllowedValues": [
43-
"CODE",
44-
"PROD",
45-
],
46-
"Default": "CODE",
47-
"Description": "Stage name",
48-
"Type": "String",
49-
},
5020
},
5121
"Resources": {
5222
"DynamoTable": {
23+
"DeletionPolicy": "Retain",
5324
"Properties": {
5425
"AttributeDefinitions": [
5526
{
@@ -67,9 +38,7 @@ exports[`The FootballNotificationsLambda stack matches the snapshot for CODE 1`]
6738
"ReadCapacityUnits": 3,
6839
"WriteCapacityUnits": 3,
6940
},
70-
"TableName": {
71-
"Fn::Sub": "\${Stack}-\${App}-notifications-\${Stage}",
72-
},
41+
"TableName": "mobile-notifications-football-notifications-CODE",
7342
"Tags": [
7443
{
7544
"Key": "devx-backup-enabled",
@@ -98,6 +67,7 @@ exports[`The FootballNotificationsLambda stack matches the snapshot for CODE 1`]
9867
},
9968
},
10069
"Type": "AWS::DynamoDB::Table",
70+
"UpdateReplacePolicy": "Retain",
10171
},
10272
"ErrorEventMetricFilter9872E3AE": {
10373
"Properties": {
@@ -589,8 +559,6 @@ exports[`The FootballNotificationsLambda stack matches the snapshot for CODE 1`]
589559

590560
exports[`The FootballNotificationsLambda stack matches the snapshot for PROD 1`] = `
591561
{
592-
"AWSTemplateFormatVersion": "2010-09-09",
593-
"Description": "mobile-notifications-football",
594562
"Metadata": {
595563
"gu:cdk:constructs": [
596564
"GuDistributionBucketParameter",
@@ -601,42 +569,15 @@ exports[`The FootballNotificationsLambda stack matches the snapshot for PROD 1`]
601569
"gu:cdk:version": "TEST",
602570
},
603571
"Parameters": {
604-
"App": {
605-
"Default": "football",
606-
"Description": "Application name",
607-
"Type": "String",
608-
},
609-
"DeployBucket": {
610-
"Default": "mobile-dist",
611-
"Description": "Bucket where RiffRaff uploads artifacts on deploy",
612-
"Type": "String",
613-
},
614572
"DistributionBucketName": {
615573
"Default": "/account/services/artifact.bucket",
616574
"Description": "SSM parameter containing the S3 bucket name holding distribution artifacts",
617575
"Type": "AWS::SSM::Parameter::Value<String>",
618576
},
619-
"DynamoNotificationTopic": {
620-
"Description": "SNS topic to notify when there's a dynamo throttling event",
621-
"Type": "String",
622-
},
623-
"Stack": {
624-
"Default": "mobile-notifications",
625-
"Description": "Stack name",
626-
"Type": "String",
627-
},
628-
"Stage": {
629-
"AllowedValues": [
630-
"CODE",
631-
"PROD",
632-
],
633-
"Default": "CODE",
634-
"Description": "Stage name",
635-
"Type": "String",
636-
},
637577
},
638578
"Resources": {
639579
"DynamoTable": {
580+
"DeletionPolicy": "Retain",
640581
"Properties": {
641582
"AttributeDefinitions": [
642583
{
@@ -654,9 +595,7 @@ exports[`The FootballNotificationsLambda stack matches the snapshot for PROD 1`]
654595
"ReadCapacityUnits": 3,
655596
"WriteCapacityUnits": 3,
656597
},
657-
"TableName": {
658-
"Fn::Sub": "\${Stack}-\${App}-notifications-\${Stage}",
659-
},
598+
"TableName": "mobile-notifications-football-notifications-PROD",
660599
"Tags": [
661600
{
662601
"Key": "devx-backup-enabled",
@@ -685,6 +624,7 @@ exports[`The FootballNotificationsLambda stack matches the snapshot for PROD 1`]
685624
},
686625
},
687626
"Type": "AWS::DynamoDB::Table",
627+
"UpdateReplacePolicy": "Retain",
688628
},
689629
"ErrorEventMetricFilter9872E3AE": {
690630
"Properties": {

cdk/lib/footballnotificationslambda.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import { join } from 'path';
21
import { GuScheduledLambda } from '@guardian/cdk';
32
import { GuAlarm } from '@guardian/cdk/lib/constructs/cloudwatch';
43
import type { GuStackProps } from '@guardian/cdk/lib/constructs/core';
54
import { GuStack } from '@guardian/cdk/lib/constructs/core';
65
import type { App } from 'aws-cdk-lib';
6+
import { Tags } from 'aws-cdk-lib';
77
import { Duration } from 'aws-cdk-lib';
88
import {
99
ComparisonOperator,
1010
Metric,
1111
TreatMissingData,
1212
Unit,
1313
} from 'aws-cdk-lib/aws-cloudwatch';
14+
import { AttributeType, BillingMode, Table } from 'aws-cdk-lib/aws-dynamodb';
1415
import { Schedule } from 'aws-cdk-lib/aws-events';
1516
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
1617
import { LoggingFormat, Runtime } from 'aws-cdk-lib/aws-lambda';
1718
import { LogGroup, MetricFilter } from 'aws-cdk-lib/aws-logs';
18-
import { CfnInclude } from 'aws-cdk-lib/cloudformation-include';
1919

2020
export class FootballNotificationsLambda extends GuStack {
2121
constructor(scope: App, id: string, props: GuStackProps) {
@@ -24,11 +24,6 @@ export class FootballNotificationsLambda extends GuStack {
2424
const { stack, stage, region, account } = this;
2525
const app = 'football';
2626

27-
const yamlTemplateFilePath = join(__dirname, '../../football/cfn.yaml');
28-
new CfnInclude(this, 'YamlTemplate', {
29-
templateFile: yamlTemplateFilePath,
30-
});
31-
3227
const footballnotificationslambda = new GuScheduledLambda(
3328
this,
3429
`${app}-Lambda`,
@@ -68,9 +63,33 @@ export class FootballNotificationsLambda extends GuStack {
6863
}),
6964
);
7065

71-
// Keep dynoma table defition in the yaml for now
7266
const dynamoTableName = `${stack}-football-notifications-${stage}`;
7367

68+
const dynamoTable = new Table(this, 'DynamoTable', {
69+
tableName: dynamoTableName,
70+
partitionKey: { name: 'notificationId', type: AttributeType.STRING },
71+
billingMode: BillingMode.PROVISIONED,
72+
readCapacity: 3,
73+
writeCapacity: 3,
74+
timeToLiveAttribute: 'ttl',
75+
});
76+
this.overrideLogicalId(dynamoTable, {
77+
logicalId: 'DynamoTable',
78+
reason: 'Retaining a stateful resource previously defined in YAML',
79+
});
80+
81+
Tags.of(dynamoTable).add('devx-backup-enabled', 'true');
82+
83+
footballnotificationslambda.addToRolePolicy(
84+
new PolicyStatement({
85+
actions: ['dynamodb:PutItem', 'dynamodb:UpdateItem', 'dynamodb:Query'],
86+
effect: Effect.ALLOW,
87+
resources: [
88+
`arn:aws:dynamodb:${region}:${account}:table/${dynamoTableName}`,
89+
],
90+
}),
91+
);
92+
7493
footballnotificationslambda.addToRolePolicy(
7594
new PolicyStatement({
7695
actions: ['dynamodb:PutItem', 'dynamodb:UpdateItem', 'dynamodb:Query'],

0 commit comments

Comments
 (0)