-
Notifications
You must be signed in to change notification settings - Fork 0
/
liflig-cdk-pipeline.ts
325 lines (296 loc) · 9.62 KB
/
liflig-cdk-pipeline.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import * as constructs from "constructs"
import * as codepipeline from "aws-cdk-lib/aws-codepipeline"
import * as codepipelineActions from "aws-cdk-lib/aws-codepipeline-actions"
import * as iam from "aws-cdk-lib/aws-iam"
import * as lambda from "aws-cdk-lib/aws-lambda"
import * as s3 from "aws-cdk-lib/aws-s3"
import * as cdk from "aws-cdk-lib"
import * as pipelines from "aws-cdk-lib/pipelines"
import * as fs from "fs"
import * as path from "path"
import { getGriidArtefactBucket } from "../griid/artefact-bucket"
import {
cloudAssemblyLookupHandler,
CloudAssemblyLookupUserParameters,
} from "./cloud-assembly-lookup-handler"
import { SlackNotification, SlackNotificationProps } from "./slack-notification"
export interface LifligCdkPipelineProps {
/**
* Bucket holding pipeline configuration and trigger file.
*
* @default - use existing bucket based on Griid conventions
*/
artifactsBucket?: s3.IBucket
/**
* Name of pipeline. This is used for the path where configuration
* is stored in S3.
*/
pipelineName: string
/**
* Type of uploaded artifact. This changes the behaviour of the
* pipeline and what kind of process it performs.
*
* Two types are supported:
*
* - cdk-source: The uploaded artifact represents a CDK application
* as source code. A build step will compile this into a
* CDK Cloud Assembly.
*
* As part of synthesizing this into a CDK Cloud Assembly,
* a file "variables.json" will be written for the
* CDK application to parameterize the build if any
* variables are found in the pipeline source.
*
* - cloud-assembly: The uploaded artifact represents a
* CDK Cloud Assembly which is ready for deployment.
*
* This does not support reading variables at the current time
* since CDK Pipelines don't support parameterized deploys.
* See https://github.com/aws/aws-cdk/issues/9560
*/
sourceType: "cdk-source" | "cloud-assembly"
/**
* The namespace used for parameters in Parameter Store.
*
* Only relevant for sourceType of "cdk-soruce".
*
* @default default
*/
parametersNamespace?: string
}
/**
* CDK Pipeline for Liflig.
*
* Avoid putting multiple pipelines in a stack, since the pipeline
* will also keep the hosting stack up-to-date.
*
* The pipeline is executed by writing an empty file to
* s3://<artifacts-bucket>/pipelines/<pipeline-name>/trigger
*
* Configuration files are read from S3 at the path
* s3://<artifacts-bucket>/pipelines/<pipeline-name>/
*
* For upload type "cdk-source":
*
* - cdk-source.json holding a pointer to the active CDK source
* that should be used. Schema:
*
* {
* bucketName: string
* bucketKey: string
* }
*
* - variables*.json which can be zero or more files
* with string-string map holding variables that will
* be written to variables.json and can be read by the
* the CDK application during synthesize.
*
* For upload type "cloud-assembly":
*
* - cloud-assembly.json holding a pointer to the active
* CDK Cloud Assembly that should be used: Schema:
*
* {
* cloudAssemblyBucketName: string
* cloudAssemblyBucketKey: string
* }
*
* Variables enables separation of IaC code and application code if
* they are not colocated in the same repository.
*/
export class LifligCdkPipeline extends constructs.Construct {
/**
* Path on S3 for pipeline configuration.
*/
static pipelineS3Prefix(pipelineName: string): string {
return `pipelines/${pipelineName}/`
}
/**
* Key in S3 bucket used to trigger pipeline.
*
* This is an empty file within the pipeline path.
*/
static pipelineS3TriggerKey(pipelineName: string): string {
return `pipelines/${pipelineName}/trigger`
}
public readonly cdkPipeline: pipelines.CodePipeline
public readonly codePipeline: codepipeline.Pipeline
constructor(
scope: constructs.Construct,
id: string,
props: LifligCdkPipelineProps,
) {
super(scope, id)
const artifactsBucket =
props.artifactsBucket ?? getGriidArtefactBucket(this)
const cloudAssemblyArtifact = new codepipeline.Artifact()
let synth: pipelines.IFileSetProducer
let stages: codepipeline.StageProps[]
switch (props.sourceType) {
case "cloud-assembly":
const cloudAssembly = this.cloudAssemblyStage(
cloudAssemblyArtifact,
artifactsBucket,
props.pipelineName,
)
synth = cloudAssembly.synth
stages = cloudAssembly.stages
break
case "cdk-source":
const cdkSource = this.cdkSourceStage(
cloudAssemblyArtifact,
artifactsBucket,
props.pipelineName,
props.parametersNamespace ?? "default",
)
synth = cdkSource.synth
stages = cdkSource.stages
break
}
const dummyArtifact = new codepipeline.Artifact()
this.codePipeline = new codepipeline.Pipeline(this, "CodePipeline", {
pipelineName: props.pipelineName,
stages: [
{
stageName: "Source",
actions: [
new codepipelineActions.S3SourceAction({
actionName: "source",
bucket: artifactsBucket,
bucketKey: LifligCdkPipeline.pipelineS3TriggerKey(
props.pipelineName,
),
output: dummyArtifact,
}),
],
},
...stages,
],
restartExecutionOnUpdate: true,
})
this.cdkPipeline = new pipelines.CodePipeline(this, "CdkPipeline", {
synth,
codePipeline: this.codePipeline,
})
}
private static getAwsCdkPackageJsonFile(): string | undefined {
// Also look up the tree a bit to handle yarn workspaces.
const candidates = [
path.join(process.cwd(), "node_modules/aws-cdk/package.json"),
path.join(process.cwd(), "../node_modules/aws-cdk/package.json"),
path.join(process.cwd(), "../../node_modules/aws-cdk/package.json"),
path.join(process.cwd(), "../../../node_modules/aws-cdk/package.json"),
]
for (const candidate of candidates) {
if (fs.existsSync(candidate)) {
return candidate
}
}
return undefined
}
private cloudAssemblyStage(
cloudAssemblyArtifact: codepipeline.Artifact,
cdkBucket: s3.IBucket,
pipelineName: string,
): { stages: codepipeline.StageProps[]; synth: pipelines.IFileSetProducer } {
const cloudAssemblyLookupFn = new lambda.Function(
this,
"CloudAssemblyLookupFn",
{
code: new lambda.InlineCode(
`exports.handler = ${cloudAssemblyLookupHandler.toString()};`,
),
handler: "index.handler",
runtime: lambda.Runtime.NODEJS_12_X,
timeout: cdk.Duration.minutes(1),
memorySize: 512,
},
)
cdkBucket.grantReadWrite(cloudAssemblyLookupFn)
const userParameters: CloudAssemblyLookupUserParameters = {
bucketName: cdkBucket.bucketName,
objectKey: `pipelines/${pipelineName}/cloud-assembly.json`,
}
const synth = pipelines.CodePipelineFileSet.fromArtifact(
cloudAssemblyArtifact,
)
const stages = [
{
stageName: "PrepareCloudAssembly",
actions: [
new codepipelineActions.LambdaInvokeAction({
actionName: "cloud-assembly-lookup",
lambda: cloudAssemblyLookupFn,
outputs: [cloudAssemblyArtifact],
userParameters,
}),
],
},
]
return { stages, synth }
}
private cdkSourceStage(
cloudAssemblyArtifact: codepipeline.Artifact,
cdkBucket: s3.IBucket,
pipelineName: string,
parametersNamespace: string,
): { stages: codepipeline.StageProps[]; synth: pipelines.IFileSetProducer } {
const prepareCdkSourceFn = new lambda.Function(this, "PrepareCdkSourceFn", {
code: lambda.Code.fromAsset(
path.join(__dirname, "../../assets/prepare-cdk-source-lambda"),
),
handler: "index.handler",
// Using python instead if NodeJS due to zip-support in stdlib.
runtime: lambda.Runtime.PYTHON_3_8,
timeout: cdk.Duration.minutes(1),
memorySize: 512,
})
const account = cdk.Stack.of(this).account
const region = cdk.Stack.of(this).region
prepareCdkSourceFn.grantPrincipal.addToPrincipalPolicy(
new iam.PolicyStatement({
actions: ["ssm:GetParametersByPath"],
resources: [
`arn:aws:ssm:${region}:${account}:parameter/liflig-cdk/*/pipeline-variables/*`,
],
}),
)
cdkBucket.grantReadWrite(prepareCdkSourceFn)
const cdkSourceArtifact = new codepipeline.Artifact()
const userParameters: PrepareCdkSourceUserParameters = {
bucketName: cdkBucket.bucketName,
prefix: `pipelines/${pipelineName}/`,
parametersNamespace: parametersNamespace,
}
const synth = new pipelines.ShellStep("GenerateCloudAssembly", {
input: pipelines.CodePipelineFileSet.fromArtifact(cdkSourceArtifact),
installCommands: ["npm ci"],
commands: ["npx cdk synth"],
})
const stages = [
{
stageName: "PrepareCdkSource",
actions: [
new codepipelineActions.LambdaInvokeAction({
actionName: "prepare-cdk-source",
lambda: prepareCdkSourceFn,
outputs: [cdkSourceArtifact],
userParameters,
}),
],
},
]
return { stages, synth }
}
addSlackNotification(props: Omit<SlackNotificationProps, "pipeline">): void {
new SlackNotification(this, "Slack", {
pipeline: this.codePipeline,
...props,
})
}
}
interface PrepareCdkSourceUserParameters {
bucketName: string
prefix: string
parametersNamespace: string
}