-
Notifications
You must be signed in to change notification settings - Fork 6
/
constructs.ts
102 lines (98 loc) Β· 2.94 KB
/
constructs.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
import * as cdk from '@aws-cdk/core'
import * as lambda from '@aws-cdk/aws-lambda'
import { Code, FunctionProps } from '@aws-cdk/aws-lambda'
import { Duration, Tag } from '@aws-cdk/core'
import { toTitleCase } from './tools'
import * as s3 from '@aws-cdk/aws-s3'
import * as iam from '@aws-cdk/aws-iam'
import * as sfn from '@aws-cdk/aws-stepfunctions'
import * as tasks from '@aws-cdk/aws-stepfunctions-tasks'
import { RetryProps } from '@aws-cdk/aws-stepfunctions'
export interface StepFunctionProps {
stack: string
stage: string
deployBucket: s3.IBucket
outputBucket: s3.IBucket
backendURL: string
frontsTopicArn: string
frontsTopicRoleArn: string
guNotifyServiceApiKey: string
}
export interface Params {
stack: string
stage: string
deployBucket: s3.IBucket
outputBucket: s3.IBucket
frontsTopicArn: string
frontsTopicRole: iam.IRole
retry?: RetryProps | boolean
}
export const taskLambda = (
scope: cdk.Construct,
name: string,
{
stack,
stage,
deployBucket,
outputBucket,
frontsTopicArn,
frontsTopicRole,
}: Params,
environment?: { [key: string]: string },
overrides?: Partial<FunctionProps>,
) => {
const lambdaName = `EditionsArchiver${toTitleCase(name)}`
const fn = new lambda.Function(scope, lambdaName, {
functionName: `editions-archiver-stepmachine-${name}-${stage}`,
runtime: lambda.Runtime.NODEJS_10_X,
timeout: Duration.minutes(5),
memorySize: 1500,
code: Code.bucket(
deployBucket,
`${stack}/${stage}/archiver/archiver.zip`,
),
handler: `index.${name}`,
environment: {
...environment,
stage: stage,
bucket: outputBucket.bucketName,
topic: frontsTopicArn,
role: frontsTopicRole.roleArn,
},
initialPolicy: [
new iam.PolicyStatement({
actions: ['*'],
resources: [
outputBucket.arnForObjects('*'),
outputBucket.bucketArn,
],
}),
new iam.PolicyStatement({
actions: ['sts:AssumeRole'],
resources: [frontsTopicRole.roleArn],
}),
],
...overrides,
})
Tag.add(fn, 'App', `editions-archiver-${name}`)
Tag.add(fn, 'Stage', stage)
Tag.add(fn, 'Stack', stack)
return fn
}
export const task = (
scope: cdk.Construct,
name: string,
desc: string,
{ retry, ...lambdaParams }: Params,
environment?: { [key: string]: string },
overrides?: Partial<FunctionProps>,
) => {
const lambda = taskLambda(scope, name, lambdaParams, environment, overrides)
const task = new sfn.Task(scope, [name, desc].join(': '), {
task: new tasks.InvokeFunction(lambda),
})
if (retry == true) {
task.addRetry(retry === true ? {} : retry)
}
return { lambda, task }
}