-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathserverless-code-pipeline-cf-template.yml
257 lines (235 loc) · 7.64 KB
/
serverless-code-pipeline-cf-template.yml
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
# Copyright [2024] [Phil Chen]
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
AWSTemplateFormatVersion: 2010-09-09
Description: 'This solution allows you to create a AWS Codepipline for deploying a serverless application with AWS CodeBuild triggered by a Github repo commit. The setup of this solution is fully automated via a CloudFormation template.'
Parameters:
Environment:
AllowedValues:
- dev
- stg
- prod
Description: 'Environment'
Type: 'String'
Default: 'stg'
CodePipelineBucketPrefix:
Description: CodePipeline needs a utility bucket for its internal use. Specify a unique prefix for the bucket name
Type: String
Default: codepipeline-work-
GitHubRepository:
Type: String
Default: getcft/aws-serverless-code-pipeline-cfn-template/master
Description: owner/repository/branch
GitHubOAuthToken:
Type: String
NoEcho: true
Description: CodePipeline requires an GitHub OAuthToken
Resources:
CodeBuildRole:
Type: "AWS::IAM::Role"
Properties:
RoleName:
'Fn::Join': [ "", ["CodeBuildRole", "-", !Ref 'AWS::StackName', "-", !Ref 'Environment']]
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: "Allow"
Principal:
Service: "codebuild.amazonaws.com"
Action: "sts:AssumeRole"
CodePipelineRole:
Type: "AWS::IAM::Role"
Properties:
RoleName:
'Fn::Join': [ "", ["CodePipelineRole", "-", !Ref 'AWS::StackName', "-", !Ref 'Environment']]
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
Effect: "Allow"
Principal:
Service: "codepipeline.amazonaws.com"
Action: "sts:AssumeRole"
# This can be restricted more
CodeBuildPolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName:
'Fn::Join': [ "", ["CodeBuildPolicy", "-", !Ref 'AWS::StackName', "-", !Ref 'Environment']]
PolicyDocument:
Version: "2012-10-17"
Statement:
Effect: Allow
Action:
- 'logs:*'
- 'codebuild:*'
- 'cloudformation:*'
- 'lambda:*'
- 'apigateway:*'
- 's3:*'
- 'iam:PassRole'
- 'iam:GetRole'
- 'iam:CreateRole'
- 'iam:PutRolePolicy'
- 'iam:DeleteRolePolicy'
- 'iam:DeleteRole'
- 'iam:TagRole'
- 'events:Put*'
- 'events:Remove*'
- 'events:Delete*'
Resource: "*"
Roles:
- !Ref CodeBuildRole
CodePipelinePolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName:
'Fn::Join': [ "", ["CodePipelinePolicy", "-", !Ref 'AWS::StackName', "-", !Ref 'Environment']]
PolicyDocument:
Version: "2012-10-17"
Statement:
Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- s3:putObject
- s3:getObject
- codebuild:*
Resource:
- "*"
Roles:
- !Ref CodePipelineRole
S3:
Type: "AWS::S3::Bucket"
Properties:
BucketName: !Join [ "", [!Ref CodePipelineBucketPrefix, !Ref 'Environment', "-", !Ref "AWS::Region"]]
TestBuild:
Type: "AWS::CodeBuild::Project"
Properties:
Name: Test-Build
Description: Demo of CodeBuild with CodeDeploy pipeline.
ServiceRole: !Ref CodeBuildRole
TimeoutInMinutes: 5
Source:
Type: CODEPIPELINE
BuildSpec: !Join [ "", ["buildspec", "-", !Ref 'Environment', ".yml"]]
Environment:
Type: "LINUX_CONTAINER"
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:7.0
Artifacts:
Type: CODEPIPELINE
BuildDeploy:
Type: "AWS::CodeBuild::Project"
Properties:
Name: Build-Deploy
Description: Demo of CodeBuild with CodeDeploy pipeline.
ServiceRole: !Ref CodeBuildRole
TimeoutInMinutes: 5
Source:
Type: CODEPIPELINE
BuildSpec: |
version: 0.2
phases:
build:
commands:
- n 18.19.0
- npm install --silent --progress-false
- npm install --silent --progress=false -g [email protected]
- npm install --silent --save–dev serverless-stack-output
- serverless deploy
Environment:
Type: "LINUX_CONTAINER"
ComputeType: BUILD_GENERAL1_SMALL
EnvironmentVariables:
- Name: env
Type: PLAINTEXT
Value: !Ref Environment
Image: aws/codebuild/standard:7.0
Artifacts:
Type: CODEPIPELINE
# CodePipeline with its stages:
Pipeline:
Type: "AWS::CodePipeline::Pipeline"
Properties:
Name: !Join [ "", ["DevOps-Pipeline", "-", !Ref 'AWS::StackName'] ]
ArtifactStore:
Location: !Ref S3
Type: S3
RestartExecutionOnUpdate: true
RoleArn: !Join [ "", ["arn:aws:iam::", !Ref "AWS::AccountId", ":role/", !Ref CodePipelineRole ] ]
Stages:
# Stage 1: Get the source from GitHub:
- Name: Source
Actions:
- Name: SourceAction
RunOrder: 1
ActionTypeId:
Category: Source
Owner: ThirdParty
Provider: GitHub
Version: 1
Configuration:
Owner: !Select [0, !Split [ "/" , !Ref GitHubRepository ]]
Repo: !Select [1, !Split [ "/" , !Ref GitHubRepository ]]
Branch: !Select [2, !Split [ "/" , !Ref GitHubRepository ]]
PollForSourceChanges: true
OAuthToken: !Ref GitHubOAuthToken
OutputArtifacts:
- Name: TheSourceCode
# Stage 2: Run Your Tests
- Name: TestBuild
Actions:
- Name: Test-Build
RunOrder: 1
InputArtifacts:
- Name: TheSourceCode
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: 1
Configuration:
ProjectName: !Ref TestBuild
OutputArtifacts:
- Name: TheArtifact
# Stage 3: Build and Deploy using Serverless Framework
- Name: BuildDeploy
Actions:
- Name: Build-Deploy
RunOrder: 1
InputArtifacts:
- Name: TheArtifact
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: 1
Configuration:
ProjectName: !Ref BuildDeploy
OutputArtifacts:
- Name: TheArtifact2
Outputs:
CodeBuild:
Value: !Ref Pipeline
Environment:
Value: !Ref Environment
Github:
Value: !Ref GitHubRepository
CodeBuildRole:
Value: !Ref CodeBuildRole
CodeBuildPolicy:
Value: !Ref CodeBuildPolicy
CodePipelineRole:
Value: !Ref CodePipelineRole
CodePipelinePolicy:
Value: !Ref CodePipelinePolicy
S3:
Value: !Ref S3