-
Notifications
You must be signed in to change notification settings - Fork 2
/
mobile-save-for-later.ts
155 lines (141 loc) · 5.18 KB
/
mobile-save-for-later.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { join } from "path";
import { GuApiGatewayWithLambdaByPath } from "@guardian/cdk";
import type { ApiGatewayAlarms } from "@guardian/cdk";
import type { NoMonitoring } from "@guardian/cdk/lib/constructs/cloudwatch";
import type { GuStackProps } from "@guardian/cdk/lib/constructs/core";
import { GuStack } from "@guardian/cdk/lib/constructs/core";
import { GuLambdaFunction } from "@guardian/cdk/lib/constructs/lambda";
import type { App } from "aws-cdk-lib";
import { Duration } from "aws-cdk-lib";
import { CfnBasePathMapping, CfnDomainName } from "aws-cdk-lib/aws-apigateway";
import { PolicyStatement } from "aws-cdk-lib/aws-iam";
import { Runtime } from "aws-cdk-lib/aws-lambda";
import { CfnRecordSetGroup } from "aws-cdk-lib/aws-route53";
import { CfnInclude } from "aws-cdk-lib/cloudformation-include";
export interface MobileSaveForLaterProps extends GuStackProps {
certificateId: string;
domainName: string;
hostedZoneName: string;
hostedZoneId: string;
identityApiHost: string;
reservedConcurrentExecutions: number;
monitoringConfiguration: NoMonitoring | ApiGatewayAlarms;
identityOktaIssuerUrl: string;
identityOktaAudience: string;
}
export class MobileSaveForLater extends GuStack {
constructor(scope: App, id: string, props: MobileSaveForLaterProps) {
super(scope, id, props);
const yamlTemplateFilePath = join(
__dirname,
"../..",
"mobile-save-for-later/conf/cfn.yaml"
);
const yamlDefinedResources = new CfnInclude(this, "YamlTemplate", {
templateFile: yamlTemplateFilePath,
});
const app = "mobile-save-for-later";
const commonLambdaProps = {
runtime: Runtime.JAVA_21,
app,
fileName: `${app}.jar`,
};
const commonEnvironmentVariables = {
App: app,
Stack: this.stack,
Stage: this.stage,
IdentityApiHost: props.identityApiHost,
IdentityOktaIssuerUrl: props.identityOktaIssuerUrl,
IdentityOktaAudience: props.identityOktaAudience,
};
const saveArticlesLambda = new GuLambdaFunction(
this,
"save-articles-lambda",
{
handler: "com.gu.sfl.lambda.SaveArticlesLambda::handleRequest",
functionName: `mobile-save-for-later-SAVE-cdk-${this.stage}`,
timeout: Duration.seconds(60),
environment: {
...commonEnvironmentVariables,
SavedArticleLimit: "1000",
},
...commonLambdaProps,
}
);
const fetchArticlesLambda = new GuLambdaFunction(
this,
"fetch-articles-lambda",
{
handler: "com.gu.sfl.lambda.FetchArticlesLambda::handleRequest",
functionName: `mobile-save-for-later-FETCH-cdk-${this.stage}`,
timeout: Duration.seconds(20),
reservedConcurrentExecutions: props.reservedConcurrentExecutions,
environment: commonEnvironmentVariables,
...commonLambdaProps,
}
);
[saveArticlesLambda, fetchArticlesLambda].map((lambda) => {
lambda.addToRolePolicy(
new PolicyStatement({
actions: [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:UpdateItem",
"dynamodb:Query",
],
resources: [
yamlDefinedResources
.getResource("SaveForLaterDynamoTable")
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html#aws-resource-dynamodb-table-return-values
.getAtt("Arn")
.toString(),
],
})
);
});
const saveForLaterApi = new GuApiGatewayWithLambdaByPath(this, {
app,
restApiName: `${app}-api-${this.stage}`,
monitoringConfiguration: props.monitoringConfiguration,
targets: [
{
path: "/syncedPrefs/me/savedArticles",
httpMethod: "POST",
lambda: saveArticlesLambda,
},
{
path: "/syncedPrefs/me",
httpMethod: "GET",
lambda: fetchArticlesLambda,
},
],
});
// N.B. we cannot use GuCertificate here as we deploy to eu-west-1 but the certificate must be created in us-east-1.
// https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-edge-optimized-custom-domain-name.html
const certificateArn = `arn:aws:acm:us-east-1:${this.account}:certificate/${props.certificateId}`;
const cfnDomainName = new CfnDomainName(this, "ApiDomainName", {
domainName: props.domainName,
certificateArn,
});
new CfnBasePathMapping(this, "ApiMapping", {
domainName: cfnDomainName.ref,
restApiId: saveForLaterApi.api.restApiId,
stage: saveForLaterApi.api.deploymentStage.stageName,
});
new CfnRecordSetGroup(this, "ApiRoute53", {
hostedZoneId: props.hostedZoneId,
recordSets: [
{
name: props.domainName,
type: "A",
aliasTarget: {
dnsName: cfnDomainName.attrDistributionDomainName,
// This magical value is taken from the AWS docs:
// https://docs.amazonaws.cn/en_us/AWSCloudFormation/latest/UserGuide/aws-properties-route53-aliastarget-1.html#aws-properties-route53-aliastarget-1-properties
hostedZoneId: "Z2FDTNDATAQYW2",
},
},
],
});
}
}