-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
integ.lambda-deployed-through-codepipeline.lit.ts
134 lines (126 loc) · 4.13 KB
/
integ.lambda-deployed-through-codepipeline.lit.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
import codebuild = require('@aws-cdk/aws-codebuild');
import codecommit = require('@aws-cdk/aws-codecommit');
import codepipeline = require('@aws-cdk/aws-codepipeline');
import lambda = require('@aws-cdk/aws-lambda');
import cdk = require('@aws-cdk/cdk');
import codepipeline_actions = require('../lib');
const app = new cdk.App();
/// !show
const lambdaStack = new cdk.Stack(app, 'LambdaStack', {
// remove the Stack from `cdk synth` and `cdk deploy`
// unless you explicitly filter for it
autoDeploy: false,
});
const lambdaCode = lambda.Code.cfnParameters();
new lambda.Function(lambdaStack, 'Lambda', {
code: lambdaCode,
handler: 'index.handler',
runtime: lambda.Runtime.NodeJS810,
});
// other resources that your Lambda needs, added to the lambdaStack...
const pipelineStack = new cdk.Stack(app, 'PipelineStack');
const pipeline = new codepipeline.Pipeline(pipelineStack, 'Pipeline');
// add the source code repository containing this code to your Pipeline,
// and the source code of the Lambda Function, if they're separate
const cdkSourceOutput = new codepipeline.Artifact();
const cdkSourceAction = new codepipeline_actions.CodeCommitSourceAction({
repository: new codecommit.Repository(pipelineStack, 'CdkCodeRepo', { repositoryName: 'CdkCodeRepo' }),
actionName: 'CdkCode_Source',
output: cdkSourceOutput,
});
const lambdaSourceOutput = new codepipeline.Artifact();
const lambdaSourceAction = new codepipeline_actions.CodeCommitSourceAction({
repository: new codecommit.Repository(pipelineStack, 'LambdaCodeRepo', { repositoryName: 'LambdaCodeRepo' }),
actionName: 'LambdaCode_Source',
output: lambdaSourceOutput,
});
pipeline.addStage({
name: 'Source',
actions: [cdkSourceAction, lambdaSourceAction],
});
// synthesize the Lambda CDK template, using CodeBuild
// the below values are just examples, assuming your CDK code is in TypeScript/JavaScript -
// adjust the build environment and/or commands accordingly
const cdkBuildProject = new codebuild.Project(pipelineStack, 'CdkBuildProject', {
environment: {
buildImage: codebuild.LinuxBuildImage.UBUNTU_14_04_NODEJS_10_1_0,
},
buildSpec: {
version: '0.2',
phases: {
install: {
commands: 'npm install',
},
build: {
commands: [
'npm run build',
'npm run cdk synth LambdaStack -- -o .',
],
},
},
artifacts: {
files: 'LambdaStack.template.yaml',
},
},
});
const cdkBuildOutput = new codepipeline.Artifact();
const cdkBuildAction = new codepipeline_actions.CodeBuildAction({
actionName: 'CDK_Build',
project: cdkBuildProject,
input: cdkSourceOutput,
output: cdkBuildOutput,
});
// build your Lambda code, using CodeBuild
// again, this example assumes your Lambda is written in TypeScript/JavaScript -
// make sure to adjust the build environment and/or commands if they don't match your specific situation
const lambdaBuildProject = new codebuild.Project(pipelineStack, 'LambdaBuildProject', {
environment: {
buildImage: codebuild.LinuxBuildImage.UBUNTU_14_04_NODEJS_10_1_0,
},
buildSpec: {
version: '0.2',
phases: {
install: {
commands: 'npm install',
},
build: {
commands: 'npm run build',
},
},
artifacts: {
files: [
'index.js',
'node_modules/**/*',
],
},
},
});
const lambdaBuildOutput = new codepipeline.Artifact();
const lambdaBuildAction = new codepipeline_actions.CodeBuildAction({
actionName: 'Lambda_Build',
project: lambdaBuildProject,
input: lambdaSourceOutput,
output: lambdaBuildOutput,
});
pipeline.addStage({
name: 'Build',
actions: [cdkBuildAction, lambdaBuildAction],
});
// finally, deploy your Lambda Stack
pipeline.addStage({
name: 'Deploy',
actions: [
new codepipeline_actions.CloudFormationCreateUpdateStackAction({
actionName: 'Lambda_CFN_Deploy',
templatePath: cdkBuildOutput.atPath('LambdaStack.template.yaml'),
stackName: 'LambdaStackDeployedName',
adminPermissions: true,
parameterOverrides: {
...lambdaCode.assign(lambdaBuildOutput.s3Coordinates),
},
extraInputs: [
lambdaBuildOutput,
],
}),
],
});