forked from uplift-inc/aws-codepipelines-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeployment-cft.yml
More file actions
477 lines (461 loc) · 16.7 KB
/
deployment-cft.yml
File metadata and controls
477 lines (461 loc) · 16.7 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# This CloudFormation Template should do an end-to-end one-step deployment of the CodePipeline dashboard from source.
AWSTemplateFormatVersion: "2010-09-09"
Description: >
CodePipeline Dashboard Elastic Beanstalk Deployment and CICD Pipeline. It will deploy anything that you do not
supply. If a custom CNAME prefix is specified, the dashboard URL will be provided as an output of the template.
Elastic Beanstalk will always create the default Security Group, which allows open access on Port 80, that must be
manually locked down after stack creation if you want to limit access to the dashboard. Deploys to default VPC.
Parameters:
GitHubRepoOwner:
Type: String
Default: m8786
Description: Repository owner to pull the project from.
GitHubToken:
Type: String
NoEcho: true
Description: >
GitHub OAuth/Personal Access Token for CodePipeline to use to pull source (even though repo is public). Only
needs repo read scope. Alternatively, the you can use an existing secret below.
SecretResolveString:
Type: String
Description: >
CloudFormation SecretsManage Resolve String formatted location of GitHub Access Token in the format
'<secret-id>:SecretString:<json-key>:<version-stage>:<version-id>' for any values that are applicable.
"SecretString" is a required string (for example "MyGHSecretId:SecretString:MyJsonKey"):
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html#dynamic-references-secretsmanager
ApprovalRequired:
Type: String
AllowedValues:
- 'Yes'
- 'No'
Default: 'No'
Description: Require Human Approval for Deployments from this pipeline?
Branch:
Type: String
Default: main
Description: Override the branch of the CodePipelnie repo to pull from GitHub for the CICD pipeline
S3Bucket:
Type: String
Description: The name of a pre-existing S3 bucket to use for temporary storage between stages of the CodePipeline
CodeBuildRoleArn:
Type: String
Description: The ARN of pre-existing the role for CodeBuild to use, should be able to write to S3 build bucket
CodePipelineRoleArn:
Type: String
Description: The Arn of pre-existing role for CodePipeline to use, should be able to read from S3 build bucket
EBServiceRoleArn:
Type: String
Description: The Arn of the pre-existing Elastic Beanstalk Service Role for Elastic Beanstalk to perform actions
InstanceProfileArn:
Type: String
Description: The Arn of the pre-existing Instance Profile for the Elastic Beanstalk EC2 instances to use
CodeBuildImage:
Type: String
Default: 'aws/codebuild/amazonlinux2-x86_64-standard:3.0'
Description: The CodeBuild Image to use to build the application
EBSolutionStack:
Type: String
Default: '64bit Amazon Linux 2 v3.3.2 running Corretto 8'
Description: The ElasticBeanstalk Solution Stack to use, should be using the Corretto8 Platform
InstanceType:
Type: String
Default: t3.micro
Description: The instance type for the single deployed instance, must be supported in this region
CNamePrefix:
Type: String
MaxLength: 63
Description: >
Desired unique CNAME prefix (4-63 chars) for the EB Environment, otherwise automatically generated. Note that
specifying this will prevent CloudFormation upgrades that require replacement
ConstraintDescription: Must be less than 63 charcaters
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: "GitHub Access - Provide only one of the following (Required)"
Parameters:
- GitHubToken
- SecretResolveString
- Label:
default: "Pre-Existing Components (Optional)"
Parameters:
- S3Bucket
- CodeBuildRoleArn
- CodePipelineRoleArn
- EBServiceRoleArn
- InstanceProfileArn
- Label:
default: "Overrides (Optional)"
Parameters:
- GitHubRepoOwner
- ApprovalRequired
- Branch
- CodeBuildImage
- EBSolutionStack
- InstanceType
- CNamePrefix
ParameterLabels:
GitHubRepoOwner:
default: GitHub Repository Owner
GitHubToken:
default: GitHub Access Token
SecretResolveString:
default: GitHub Secret CloudFormation Resolve String
S3Bucket:
default: S3 Bucket
CodeBuildRoleArn:
default: CodeBuild Role
CodePipelineRoleArn:
default: CodePipeline Role
EBServiceRoleArn:
default: Elastic Beanstalk Service Role
InstanceProfileArn:
default: EC2 Instance Profile
ApprovalRequired:
default: Manual Approval Required
Branch:
default: Repo Branch
CodeBuildImage:
default: CodeBuild Image
EBSolutionStack:
default: Elastic Beanstalk Solution Stack
InstanceType:
default: Instance Type
CNamePrefix:
default: CNAME Prefix
Conditions:
AutoDeploy:
!Equals [!Ref ApprovalRequired, "No"]
CreateGHSecret:
!Not [ !Equals [!Ref GitHubToken, ""]]
CreateS3:
!Equals [!Ref S3Bucket, ""]
CreateCodeBuildRole:
!Equals [!Ref CodeBuildRoleArn, ""]
CreateCodePipelineRole:
!Equals [!Ref CodePipelineRoleArn, ""]
CreateEBServiceRole:
!Equals [!Ref EBServiceRoleArn, ""]
CreateInstanceProfileAndRole:
!Equals [!Ref InstanceProfileArn, ""]
CNamePrefixSpecified:
!Not [!Equals [!Ref CNamePrefix, ""]]
Rules: #Unfortunately, Assertions may not be enforced until a change set is executed. Still better than nothing.
GitHubTokenOrSecretExclusivelyProvided:
Assertions: #Ensure Exclusive Or between GH Token and GH Secret
- Assert: !Or [ !Not [!Equals [!Ref GitHubToken, ""]], !Not [!Equals [SecretResolveString, ""]]]
AssertDescription: Either GitHub Access Token or GitHub Secret CloudFormation Resolve String must be provided
- Assert: !Not [!And [ !Not [!Equals [!Ref GitHubToken, ""]], !Not [!Equals [!Ref SecretResolveString, ""]]]]
AssertDescription: Cannot provide both GitHub Access Token and GitHub Secret CloudFormation Resolve String
Resources:
GHAuthTokenSecret:
Condition: CreateGHSecret
Type: AWS::SecretsManager::Secret
Properties:
Description: GH Access Token for AWS CodePipeline Dashboard Source access
SecretString: !Ref GitHubToken
PipelineBucket:
Condition: CreateS3
Type: AWS::S3::Bucket
Properties:
AccessControl: BucketOwnerFullControl
CodeBuildServiceRole:
Condition: CreateCodeBuildRole
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "codebuild.amazonaws.com"
Action:
- "sts:AssumeRole"
Description: Role that CodeBuild assumes to build CodePipeline Dashboard application
Path: /service-role/
Policies:
- PolicyName: !Sub 'CloduwatchLogGenerationPolicy'
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: CreateBuildLogsInCloudwatch
Effect: Allow
Resource:
- !Sub 'arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/*'
- !Sub 'arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/*:*'
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
- PolicyName: !Sub 'S3BuildBucketAccessPolicy'
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: GetAndPutBuildArtifactsFromAndToS3
Action:
- 's3:PutObject'
- 's3:GetObject'
- 's3:PutObjectVersion'
- "s3:GetObjectVersion"
Effect: Allow
Resource: !If
- CreateS3
- !Join
- ''
- - 'arn:aws:s3:::'
- !Ref PipelineBucket
- '/*'
- !Sub 'arn:aws:s3:::${S3Bucket}/*'
ContinuousBuild:
Type: AWS::CodeBuild::Project
Properties:
ServiceRole: !If
- CreateCodeBuildRole
- !GetAtt CodeBuildServiceRole.Arn
- !Ref CodeBuildRoleArn
Artifacts:
Type: CODEPIPELINE
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: !Ref CodeBuildImage
EnvironmentVariables:
- Name: TARGET_BUCKET
Value: !If
- CreateS3
- !Ref PipelineBucket
- !Ref S3Bucket
Source:
Type: CODEPIPELINE
BuildSpec: |
version: 0.2
phases:
install:
runtime-versions:
java: corretto8
build:
commands:
- mvn package
post_build:
commands:
- APP_JAR=$(mvn -q -Dexec.executable="echo" -Dexec.args='${project.build.finalName}' --non-recursive exec:exec)
artifacts:
files:
- 'target/$APP_JAR.jar'
discard-paths: yes
TimeoutInMinutes: 20
EBServiceRole:
Condition: CreateEBServiceRole
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- elasticbeanstalk.amazonaws.com
Action:
- sts:AssumeRole
Description: Role that ElasticBeanstalk Service assumes to manage resources
Path: /service-role/
ManagedPolicyArns: [arn:aws:iam::aws:policy/AWSElasticBeanstalkManagedUpdatesCustomerRolePolicy]
EBRole:
Condition: CreateInstanceProfileAndRole
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Description: ElasticBeanstalk EC2 role that allows ReadOnly Access to CodePipeline
ManagedPolicyArns: [arn:aws:iam::aws:policy/AWSCodePipeline_ReadOnlyAccess]
EBInstanceProfile:
Condition: CreateInstanceProfileAndRole
DependsOn: EBRole
Type: AWS::IAM::InstanceProfile
Properties:
Roles: [!Ref EBRole]
CPDashApp:
Type: AWS::ElasticBeanstalk::Application
Properties:
Description: CodePipeline Dashboard Elastic Beanstalk App
CPDashEnv:
Type: AWS::ElasticBeanstalk::Environment
Properties:
ApplicationName: !Ref CPDashApp
Description: CodePipeline Dashboard Elastic Beanstalk Environment
SolutionStackName: !Ref EBSolutionStack
CNAMEPrefix: !If
- CNamePrefixSpecified
- !Ref CNamePrefix
- !Ref AWS::NoValue
OptionSettings: #Defaults to Webserver Tier
- Namespace: aws:ec2:instances
OptionName: InstanceTypes
Value: !Ref InstanceType
- Namespace: aws:elasticbeanstalk:environment
OptionName: EnvironmentType
Value: SingleInstance
- Namespace: aws:elasticbeanstalk:environment
OptionName: ServiceRole
Value: !If
- CreateEBServiceRole
- !GetAtt EBServiceRole.Arn
- !Ref EBServiceRoleArn
- Namespace: aws:elasticbeanstalk:managedactions:platformupdate
OptionName: UpdateLevel
Value: minor #Managed updates on by default, take all platform updates
- Namespace: aws:autoscaling:launchconfiguration
OptionName: DisableIMDSv1
Value: true
- Namespace: aws:autoscaling:launchconfiguration
OptionName: IamInstanceProfile
Value: !If
- CreateInstanceProfileAndRole
- !Ref EBInstanceProfile
- !Ref InstanceProfileArn
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: SERVER_PORT #Have SpringBoot run on nGinx default for SampleApp compatibility using 'SERVER_PORT'
Value: 5000 #instead of having nGinx listen on SpringBoot default of 8080 using 'PORT'
CodePipelineServiceRole:
Condition: CreateCodePipelineRole
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "codepipeline.amazonaws.com"
Action:
- "sts:AssumeRole"
Description: Role that CodePipeline assumes to manage building and deploying the CodePipeline Dashboard
Path: /service-role/
Policies:
- PolicyName: !Sub 'CodeBuildAndElasticBeanStalkPermissions'
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: PutItemsCodePipelineOrElasticBeanstalkBuckets
Action:
- 's3:PutObject'
Resource:
- 'arn:aws:s3:::codepipeline*'
- 'arn:aws:s3:::elasticbeanstalk*'
Effect: Allow
- Sid: ElasticBeanstalkCreationAndDeployment
Action:
- 'elasticbeanstalk:*'
- 'ec2:*'
- 'elasticloadbalancing:*'
- 'autoscaling:*'
- 'cloudwatch:*'
- 's3:*'
- 'sns:*'
- 'cloudformation:*'
- 'rds:*'
- 'sqs:*'
- 'ecs:*'
- 'iam:PassRole'
Resource: '*'
Effect: Allow
- Sid: CodeBuildTriggeringAndStatus
Action:
- 'codebuild:BatchGetBuilds'
- 'codebuild:StartBuild'
Resource: '*'
Effect: Allow
CiCdPipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
ArtifactStore:
Type: S3
Location: !If
- CreateS3
- !Ref PipelineBucket
- !Ref S3Bucket
RestartExecutionOnUpdate: true
RoleArn: !If
- CreateCodePipelineRole
- !GetAtt CodePipelineServiceRole.Arn
- !Ref CodePipelineRoleArn
Stages:
- Name: Source
Actions:
- InputArtifacts: []
Name: PullSourceFromGitHub
ActionTypeId:
Category: Source
Owner: ThirdParty
Version: '1'
Provider: GitHub #Even though CodeStar is preferred, this (GitHub v1) requires less interaction to set up
OutputArtifacts:
- Name: SourceCode
Configuration:
Owner: !Ref GitHubRepoOwner
Repo: 'aws-codepipelines-dashboard'
Branch: !Ref Branch
OAuthToken: !If
- CreateGHSecret
- !Sub '{{resolve:secretsmanager:${GHAuthTokenSecret}}}'
- !Sub '{{resolve:secretsmanager:${SecretResolveString}}}'
PollForSourceChanges: true #Can't set up Webhook without Admin permissions, must poll
RunOrder: 1
- Name: Build
Actions:
- InputArtifacts:
- Name: SourceCode
Name: MavenBuild
ActionTypeId:
Category: Build
Owner: AWS
Version: '1'
Provider: CodeBuild
OutputArtifacts:
- Name: AppJar
Configuration:
ProjectName: !Ref ContinuousBuild
RunOrder: 1
- !If
- AutoDeploy
- !Ref AWS::NoValue
- Name: Human
Actions:
- InputArtifacts: []
OutputArtifacts: []
Name: HumanApproval
ActionTypeId:
Category: Approval
Owner: AWS
Version: 1
Provider: Manual
- Name: Deploy
Actions:
- InputArtifacts:
- Name: AppJar
Name: DeployToElasticBeanstalk
ActionTypeId:
Category: Deploy
Owner: AWS
Version: '1'
Provider: ElasticBeanstalk
OutputArtifacts: []
Configuration:
ApplicationName: !Ref CPDashApp
EnvironmentName: !Ref CPDashEnv
RunOrder: 1
Outputs:
CPDashURL:
Condition: CNamePrefixSpecified
Description: The Custom URL of the CodePipeline Dashboard for this region
Value: !Sub 'http://${CNamePrefix}.${AWS::Region}.elasticbeanstalk.com'
Export:
Name: !Sub '${AWS::Region}-CodePipelineDashboardURL'