Repository navigation
Expand file tree
/
Copy pathstack.ts
More file actions
89 lines (69 loc) · 2.42 KB
/
Copy pathstack.ts
File metadata and controls
89 lines (69 loc) · 2.42 KB
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
import { Construct } from 'constructs';
import { App, Duration, Stack, StackProps } from 'aws-cdk-lib';
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { Code, Function, Runtime, Tracing } from 'aws-cdk-lib/aws-lambda';
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
import { Table } from 'aws-cdk-lib/aws-dynamodb';
import { Queue } from 'aws-cdk-lib/aws-sqs';
import { SqsEventSource } from 'aws-cdk-lib/aws-lambda-event-sources';
class BackgroundJobStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const app = this.node.tryGetContext('app');
const version = this.node.tryGetContext('version');
const name = `${app}_public-api-background-job_${version}`;
const configRootKey = `/${app}/${version}`;
const queue = Queue.fromQueueArn(
this,
'Queue',
`arn:aws:sqs:${this.region}:${this.account}:${app}_passes_load_${version}.fifo`
);
const lambda = new Function(this, 'Lambda', {
functionName: name,
handler: 'index.handler',
runtime: Runtime.NODEJS_18_X,
memorySize: 3008,
timeout: Duration.minutes(5),
tracing: Tracing.ACTIVE,
code: Code.fromAsset(`./dist`),
environment: {
NODE_ENV: 'production',
CONFIG_ROOT_KEY: configRootKey
}
});
lambda.addToRolePolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: ['ssm:GetParametersByPath'],
resources: [
`arn:aws:ssm:${this.region}:${this.account}:parameter${configRootKey}`
]
})
);
for (const partialName of ['applicants', 'passes']) {
const tableName = StringParameter.valueForStringParameter(
this,
`${configRootKey}/${partialName}Table`
);
const table = Table.fromTableArn(
this,
`${partialName}DynamoDBTable`,
`arn:aws:dynamodb:${this.region}:${this.account}:table/${tableName}`
);
table.grantReadWriteData(lambda);
}
lambda.addEventSource(new SqsEventSource(queue));
queue.grantConsumeMessages(lambda);
}
}
const app = new App();
new BackgroundJobStack(app, 'PublicApiBackgroundJobStack', {
env: {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION
},
stackName: `${app.node.tryGetContext(
'app'
)}-public-api-background-job-${app.node.tryGetContext('version')}`
});
app.synth();