-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodepipeline.yaml
More file actions
282 lines (273 loc) · 8.41 KB
/
Copy pathcodepipeline.yaml
File metadata and controls
282 lines (273 loc) · 8.41 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
AWSTemplateFormatVersion: 2010-09-09
Description: CI/CD Pipeline
Parameters:
ProjectName:
Description: Name of the project
Type: String
Default: vpc-lab
RepositoryName:
Description: Name of the repository
Type: String
Default: Cu-Hoang/cloudformation-vpc
Environment:
Description: Environment
Type: String
AllowedValues:
- dev
- staging
- prod
ConstraintDescription: Must specify 'dev' or 'staging' or 'prod'
Default: dev
Owner:
Description: Name of Owner
Type: String
Default: ngochoang
NamePrefix:
Description: Prefix name
Type: String
Default: vpc-lab
BucketName:
Description: Name of Bucket
Type: String
Default: vpc-lab-1712
Resources:
GitHubConnection:
Type: AWS::CodeConnections::Connection
Properties:
ConnectionName: !Sub ${ProjectName}-GitHubConnection
ProviderType: GitHub
Tags:
- Key: Application
Value: !Ref AWS::StackName
- Key: Environment
Value: !Ref Environment
- Key: ProjectName
Value: !Ref ProjectName
- Key: Owner
Value: !Ref Owner
- Key: Name
Value: !Sub ${NamePrefix}-github-connection
TemplatesBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref BucketName
VersioningConfiguration:
Status: Enabled
AccessControl: Private
Tags:
- Key: Application
Value: !Ref AWS::StackName
- Key: Environment
Value: !Ref Environment
- Key: ProjectName
Value: !Ref ProjectName
- Key: Owner
Value: !Ref Owner
- Key: Name
Value: !Sub ${NamePrefix}-templates-bucket
ArtifactsBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub ${BucketName}-artifacts
VersioningConfiguration:
Status: Enabled
AccessControl: Private
Tags:
- Key: Application
Value: !Ref AWS::StackName
- Key: Environment
Value: !Ref Environment
- Key: ProjectName
Value: !Ref ProjectName
- Key: Owner
Value: !Ref Owner
- Key: Name
Value: !Sub ${NamePrefix}-artifacts-bucket
CFNRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: ['sts:AssumeRole']
Effect: Allow
Principal:
Service: [cloudformation.amazonaws.com]
Version: '2012-10-17'
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AdministratorAccess
CodeBuildRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: ['sts:AssumeRole']
Effect: Allow
Principal:
Service: [codebuild.amazonaws.com]
Version: '2012-10-17'
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AdministratorAccess
CodePipelineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: ['sts:AssumeRole']
Effect: Allow
Principal:
Service: [codepipeline.amazonaws.com]
Version: '2012-10-17'
Path: /
Policies:
- PolicyName: CodePipelineAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action: s3:ListBucket
Effect: Allow
Resource: !GetAtt [ArtifactsBucket, Arn]
- Action: s3:*Object
Effect: Allow
Resource: !Sub ${ArtifactsBucket.Arn}/*
- Action:
- 'cloudformation:CreateStack'
- 'cloudformation:DescribeStacks'
- 'cloudformation:DeleteStack'
- 'cloudformation:UpdateStack'
- 'cloudformation:CreateChangeSet'
- 'cloudformation:ExecuteChangeSet'
- 'cloudformation:DeleteChangeSet'
- 'cloudformation:DescribeChangeSet'
- 'cloudformation:SetStackPolicy'
- 'iam:PassRole'
Effect: Allow
Resource: '*'
- Action:
- 'codestar-connections:UseConnection'
Effect: Allow
Resource: !GetAtt [GitHubConnection, ConnectionArn]
- Action:
- 'codebuild:StartBuild'
- 'codebuild:BatchGetBuilds'
Effect: Allow
Resource: !GetAtt [CodeBuildProject, Arn]
CodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Name: !Sub ${ProjectName}-CodeBuild
Source:
Type: CODEPIPELINE
GitCloneDepth: 1
BuildSpec: buildspec.yaml
Artifacts:
Type: CODEPIPELINE
Environment:
PrivilegedMode: True
Type: LINUX_CONTAINER
Image: aws/codebuild/standard:5.0
ComputeType: BUILD_GENERAL1_SMALL
ServiceRole: !GetAtt [CodeBuildRole, Arn]
Tags:
- Key: Application
Value: !Ref AWS::StackName
- Key: Environment
Value: !Ref Environment
- Key: ProjectName
Value: !Ref ProjectName
- Key: Owner
Value: !Ref Owner
- Key: Name
Value: !Sub ${NamePrefix}-codebuild
Pipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
ArtifactStore:
Location: !Ref ArtifactsBucket
Type: S3
ExecutionMode: QUEUED
Name: pipeline
PipelineType: V2
RoleArn: !GetAtt [CodePipelineRole, Arn]
Stages:
- Name: Source
Actions:
- Name: Source
ActionTypeId:
Category: Source
Owner: AWS
Provider: CodeStarSourceConnection
Version: 1
OutputArtifacts:
- Name: SourceOutput
Configuration:
FullRepositoryId: !Ref RepositoryName
BranchName: main
ConnectionArn: !GetAtt [GitHubConnection, ConnectionArn]
OutputArtifactFormat: CODE_ZIP
RunOrder: 1
- Name: Build
Actions:
- Name: Build
InputArtifacts:
- Name: SourceOutput
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
OutputArtifacts:
- Name: BuildOutput
Configuration:
ProjectName: !Ref CodeBuildProject
RunOrder: 1
- Name: Deploy
Actions:
- Name: Deploy
InputArtifacts:
- Name: BuildOutput
ActionTypeId:
Category: Deploy
Owner: AWS
Version: 1
Provider: CloudFormation
Configuration:
ActionMode: CREATE_UPDATE
StackName: !Sub ${ProjectName}-deploy-stack
Capabilities: CAPABILITY_NAMED_IAM
TemplatePath: BuildOutput::root.yaml
ParameterOverrides: |
{
"Environment": "dev",
"ProjectName": "vpc-lab",
"Owner": "ngochoang",
"BucketName": "vpc-lab-1712",
"VpcName": "my-vpc-01",
"VpcCIDR": "10.0.0.0/16",
"PublicSubnetName": "public-subnet-01",
"PublicSubnetCIDR": "10.0.0.0/20",
"PrivateSubnetName": "private-subnet-01",
"PrivateSubnetCIDR": "10.0.16.0/20",
"PublicSecurityGroupName": "public-sg-01",
"PrivateSecurityGroupName": "private-sg-01",
"SSHIP": "0.0.0.0/0",
"AMI": "ami-0e8ebb0ab254bb563",
"InstanceType": "t2.micro",
"KeyName": "ssh-key",
"PublicEC2Name": "public-ec2-01",
"PrivateEC2Name": "private-ec2-01"
}
RoleArn: !GetAtt [CFNRole, Arn]
RunOrder: 1
Tags:
- Key: Application
Value: !Ref AWS::StackName
- Key: Environment
Value: !Ref Environment
- Key: ProjectName
Value: !Ref ProjectName
- Key: Owner
Value: !Ref Owner
- Key: Name
Value: !Sub ${NamePrefix}-pipeline