generated from ExamProCo/aws-bootcamp-cruddur-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
187 lines (187 loc) · 6.27 KB
/
template.yaml
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
AWSTemplateFormatVersion: 2010-09-09
Description: |
- CodeStar Connection V2 Github
- CodePipeline
- Codebuild
Parameters:
GitHubBranch:
Type: String
Default: prod
GithubRepo:
Type: String
Default: 'shehzadashiq/aws-bootcamp-cruddur-2023'
ClusterStack:
Type: String
ServiceStack:
Type: String
ArtifactBucketName:
Type: String
BuildSpec:
Type: String
Resources:
Codebuild:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-stack.html
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: nested/codebuild.yaml
Parameters:
ArtifactBucketName: !Ref ArtifactBucketName
BuildSpec: !Ref BuildSpec
CodeStarConnection:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codestarconnections-connection.html
Type: AWS::CodeStarConnections::Connection
Properties:
ConnectionName: !Sub ${AWS::StackName}-connection
ProviderType: GitHub
Pipeline:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codepipeline-pipeline.html
Type: AWS::CodePipeline::Pipeline
Properties:
ArtifactStore:
Location: !Ref ArtifactBucketName
Type: S3
RoleArn: !GetAtt CodePipelineRole.Arn
Stages:
- Name: Source
Actions:
- Name: ApplicationSource
RunOrder: 1
ActionTypeId:
Category: Source
Provider: CodeStarSourceConnection
Owner: AWS
Version: '1'
OutputArtifacts:
- Name: Source
Configuration:
ConnectionArn: !Ref CodeStarConnection
FullRepositoryId: !Ref GithubRepo
BranchName: !Ref GitHubBranch
OutputArtifactFormat: "CODE_ZIP"
- Name: Build
Actions:
- Name: BuildContainerImage
RunOrder: 1
ActionTypeId:
Category: Build
Owner: AWS
Provider: CodeBuild
Version: '1'
InputArtifacts:
- Name: Source
OutputArtifacts:
- Name: ImageDefinition
Configuration:
ProjectName: !GetAtt Codebuild.Outputs.CodeBuildProjectName
BatchEnabled: false
# https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-ECS.html
- Name: Deploy
Actions:
- Name: Deploy
RunOrder: 1
ActionTypeId:
Category: Deploy
Provider: ECS
Owner: AWS
Version: '1'
InputArtifacts:
- Name: ImageDefinition
Configuration:
# In Minutes
DeploymentTimeout: "10"
ClusterName:
Fn::ImportValue:
!Sub ${ClusterStack}ClusterName
# We decided not use a cross-stack so we can tear
# down a service seperate from it.
ServiceName: backend-flask
# Fn::ImportValue:
# !Sub ${ServiceStack}ServiceName
CodePipelineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: ['sts:AssumeRole']
Effect: Allow
Principal:
Service: [codepipeline.amazonaws.com]
Version: '2012-10-17'
Path: /
Policies:
# When the Application Source downloads the code.
# It needs to zip it and place it a bucket, so we need
# to supply an artifacts bucket.
- PolicyName: !Sub ${AWS::StackName}S3ArtifactAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- s3:*
Effect: Allow
Resource:
- !Sub arn:aws:s3:::${ArtifactBucketName}
- !Sub arn:aws:s3:::${ArtifactBucketName}/*
- PolicyName: !Sub ${AWS::StackName}EcsDeployPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- ecs:DescribeServices
- ecs:DescribeTaskDefinition
- ecs:DescribeTasks
- ecs:ListTasks
- ecs:RegisterTaskDefinition
- ecs:UpdateService
Effect: Allow
Resource: "*"
- PolicyName: !Sub ${AWS::StackName}CodeStarPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- codestar-connections:UseConnection
Effect: Allow
Resource:
!Ref CodeStarConnection
- PolicyName: !Sub ${AWS::StackName}CodePipelinePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- s3:*
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- cloudformation:*
- iam:PassRole
- iam:CreateRole
- iam:DetachRolePolicy
- iam:DeleteRolePolicy
- iam:PutRolePolicy
- iam:DeleteRole
- iam:AttachRolePolicy
- iam:GetRole
- iam:PassRole
Effect: Allow
Resource: '*'
- PolicyName: !Sub ${AWS::StackName}CodePipelineBuildPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- codebuild:StartBuild
- codebuild:StopBuild
- codebuild:RetryBuild
# even though we are not Batch for CodeBuild
# AWS Still requires permissions
- codebuild:BatchGetBuilds
Effect: Allow
Resource: !Join
- ''
- - 'arn:aws:codebuild:'
- !Ref AWS::Region
- ':'
- !Ref AWS::AccountId
- ':project/'
- !GetAtt Codebuild.Outputs.CodeBuildProjectName