-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipeline.yml
336 lines (327 loc) · 10.2 KB
/
pipeline.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
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
326
327
328
329
330
331
332
333
334
335
336
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
GitHubUser:
Type: String
Description: GitHub account Name
GitHubRepo:
Type: String
Description: GitHub Repo Name
Default: "serverles-multi-account-deploy"
GitHubBranch:
Type: String
Description: GitHub Branch name
Default: "main"
CodeBuildEnvironmentType:
Type: String
Default: "LINUX_CONTAINER"
Description: Build container type
CodeBuildComputeType:
Type: String
Default: "BUILD_GENERAL1_SMALL"
Description: Build compute type
CodeBuildImage:
Type: String
Default: "aws/codebuild/amazonlinux2-x86_64-standard:3.0"
Description: Build Image
DevAccountId:
Type: String
Description: AWS Account ID for Dev
TestAccountId:
Type: String
Description: AWS Account ID for Test
ProdAccountId:
Type: String
Description: AWS Account ID for Prod
Resources:
GitHubCodeStarConnection:
Type: AWS::CodeStarConnections::Connection
Properties:
ConnectionName: PiplelineConnection
ProviderType: GitHub
ArtifactsDomain:
Type: "AWS::CodeArtifact::Domain"
Properties:
DomainName: "artifacts-domain"
UpstreamArtifactsRepo:
Type: "AWS::CodeArtifact::Repository"
Properties:
RepositoryName: "upstream-artifacts-repo"
DomainName: !GetAtt ArtifactsDomain.Name
ExternalConnections:
- public:npmjs
ArtifactsRepo:
Type: "AWS::CodeArtifact::Repository"
Properties:
RepositoryName: "artifacts-repo"
DomainName: !GetAtt ArtifactsDomain.Name
Upstreams:
- !GetAtt UpstreamArtifactsRepo.Name
CodeBuildRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- codebuild.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
RoleName: !Sub ${AWS::StackName}-codebuild-role
Policies:
- PolicyName: !Sub ${AWS::StackName}-codebuild-policy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/myapp-build-*"
- !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/myapp-build-*/*"
- Effect: Allow
Action:
- ssm:GetParameter
Resource:
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/myapp/*
- Effect: Allow
Action:
- s3:Put*
- s3:Get*
Resource:
- "*"
- Effect: Allow
Action:
- codeartifact:GetAuthorizationToken
- codeartifact:GetRepositoryEndpoint
- codeartifact:ReadFromRepository
Resource:
- "*"
- Effect: Allow
Action:
- sts:GetServiceBearerToken
Resource:
- "*"
Condition:
StringEquals:
sts:AWSServiceName: codeartifact.amazonaws.com
- Effect: Allow
Action:
- sts:AssumeRole
Resource:
- !Sub arn:aws:iam::${DevAccountId}:role/deployer-role
# - !Sub arn:aws:iam::${TestAccountId}:role/deployer-role
# - !Sub arn:aws:iam::${ProdAccountId}:role/deployer-role
CodePipelineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- codepipeline.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
RoleName: !Sub ${AWS::StackName}-codepipeline-role
Policies:
- PolicyName: !Sub ${AWS::StackName}-codepipeline-policy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- codebuild:BatchGetBuilds
- codebuild:StartBuild
Resource:
- !GetAtt CodeBuildDeployDev.Arn
- !GetAtt CodeBuildDeployTest.Arn
- !GetAtt CodeBuildDeployProd.Arn
- Effect: Allow
Action:
- s3:Put*
- s3:Get*
Resource:
- "*"
- Effect: Allow
Action:
- sns:Publish
Resource:
- !Ref SNSTopic
- Effect: Allow
Action:
- codestar-connections:UseConnection
Resource:
- "*"
CodePipelineBucket:
Type: AWS::S3::Bucket
CodeBuildDeployDev:
Type: AWS::CodeBuild::Project
Properties:
Name: myapp-build-dev
Description: Deploy MyApp to DEV
ServiceRole: !GetAtt CodeBuildRole.Arn
Artifacts:
Type: CODEPIPELINE
Environment:
Type: !Ref CodeBuildEnvironmentType
ComputeType: !Ref CodeBuildComputeType
Image: !Sub ${CodeBuildImage}
EnvironmentVariables:
- Name: DURATION
Value: "1200"
- Name: STAGE
Value: "dev"
- Name: ACCOUNT_ID
Value: !Ref AWS::AccountId
Source:
Type: CODEPIPELINE
TimeoutInMinutes: 20
CodeBuildDeployTest:
Type: AWS::CodeBuild::Project
Properties:
Name: myapp-build-test
Description: Deploy myapp to TEST
ServiceRole: !GetAtt CodeBuildRole.Arn
Artifacts:
Type: CODEPIPELINE
Environment:
Type: !Ref CodeBuildEnvironmentType
ComputeType: !Ref CodeBuildComputeType
Image: !Sub ${CodeBuildImage}
EnvironmentVariables:
- Name: DURATION
Value: "1200"
- Name: STAGE
Value: "test"
- Name: ACCOUNT_ID
Value: !Ref AWS::AccountId
Source:
Type: CODEPIPELINE
TimeoutInMinutes: 20
CodeBuildDeployProd:
Type: AWS::CodeBuild::Project
Properties:
Name: myapp-build-prod
Description: Deploy myapp to PROD
ServiceRole: !GetAtt CodeBuildRole.Arn
Artifacts:
Type: CODEPIPELINE
Environment:
Type: !Ref CodeBuildEnvironmentType
ComputeType: !Ref CodeBuildComputeType
Image: !Sub ${CodeBuildImage}
EnvironmentVariables:
- Name: DURATION
Value: "1200"
- Name: STAGE
Value: "prod"
- Name: ACCOUNT_ID
Value: !Ref AWS::AccountId
Source:
Type: CODEPIPELINE
TimeoutInMinutes: 20
CodePipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
Name: !Sub ${AWS::StackName}
RoleArn: !GetAtt CodePipelineRole.Arn
Stages:
- Name: Source
Actions:
- InputArtifacts: []
Name: Source
ActionTypeId:
Version: "1"
Owner: AWS
Category: Source
Provider: CodeStarSourceConnection
OutputArtifacts:
- Name: SourceArtifact
RunOrder: 1
Configuration:
ConnectionArn: !Ref GitHubCodeStarConnection
FullRepositoryId: !Sub ${GitHubUser}/${GitHubRepo}
BranchName: !Ref GitHubBranch
OutputArtifactFormat: "CODE_ZIP"
- Name: DEV-Deploy
Actions:
- Name: Deploy-to-DEV
ActionTypeId:
Category: Build
Owner: AWS
Version: "1"
Provider: CodeBuild
InputArtifacts:
- Name: SourceArtifact
Configuration:
ProjectName: !Ref CodeBuildDeployDev
RunOrder: 1
- Name: TEST-Approval
Actions:
- Name: Approve-for-Test
ActionTypeId:
Category: Approval
Owner: AWS
Version: "1"
Provider: Manual
Configuration:
NotificationArn: !Ref SNSTopic
ExternalEntityLink: "https://github.com/davetownsend/serverles-multi-account-deploy/releases"
CustomData: "TEST Deployment Pending Approval"
RunOrder: 1
- Name: TEST-Deploy
Actions:
- Name: Deploy-to-TEST
ActionTypeId:
Category: Build
Owner: AWS
Version: "1"
Provider: CodeBuild
InputArtifacts:
- Name: SourceArtifact
Configuration:
ProjectName: !Ref CodeBuildDeployTest
RunOrder: 1
- Name: PROD-Approval
Actions:
- Name: Approve-for-PROD
ActionTypeId:
Category: Approval
Owner: AWS
Version: "1"
Provider: Manual
Configuration:
NotificationArn: !Ref SNSTopic
ExternalEntityLink: "https://github.com/davetownsend/serverles-multi-account-deploy/releases"
CustomData: "PROD Deployment Pending Approval"
RunOrder: 1
- Name: PROD-Deploy
Actions:
- Name: Deploy-to-PROD
ActionTypeId:
Category: Build
Owner: AWS
Version: "1"
Provider: CodeBuild
InputArtifacts:
- Name: SourceArtifact
Configuration:
ProjectName: !Ref CodeBuildDeployProd
RunOrder: 1
ArtifactStore:
Type: S3
Location: !Ref CodePipelineBucket
SNSTopic:
Type: AWS::SNS::Topic
Outputs:
SNSTopicArn:
Description: ARN of SNS Topic
Value: !Ref SNSTopic
CodeStarGithubConnectionARN:
Description: Connection ARN for the Codestar connection
Value: !Ref GitHubCodeStarConnection