forked from andreaswittig/codepipeline-codedeploy-example
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.yml
More file actions
298 lines (298 loc) · 8.37 KB
/
stack.yml
File metadata and controls
298 lines (298 loc) · 8.37 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
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Continuous Delivery pipeline for static website'
Parameters:
GitHubOwner:
Description: 'The owner of the GitHub repository.'
Type: String
GitHubOAuthToken:
Description: 'The OAuthToken of the GitHub user.'
Type: String
GitHubRepo:
Description: 'The GitHub repository.'
Type: String
VPC:
Description: 'The VPC Id to launch the EC2 instance in.'
Type: AWS::EC2::VPC::Id
Subnet:
Description: 'The Subnet Id to launch the EC2 instance in.'
Type: AWS::EC2::Subnet::Id
Mappings:
RegionMap:
'ap-south-1':
AMI: 'ami-cacbbea5'
'eu-west-1':
AMI: 'ami-d41d58a7'
'ap-northeast-2':
AMI: 'ami-a04297ce'
'ap-northeast-1':
AMI: 'ami-1a15c77b'
'sa-east-1':
AMI: 'ami-b777e4db'
'ap-southeast-1':
AMI: 'ami-7243e611'
'ap-southeast-2':
AMI: 'ami-55d4e436'
'eu-central-1':
AMI: 'ami-0044b96f'
'us-east-1':
AMI: 'ami-c481fad3'
'us-east-2':
AMI: 'ami-71ca9114'
'us-west-1':
AMI: 'ami-de347abe'
'us-west-2':
AMI: 'ami-b04e92d0'
Resources:
ArtifactStore:
Type: "AWS::S3::Bucket"
Properties:
VersioningConfiguration:
Status: Enabled
CodePipelineIAMRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- 'codepipeline.amazonaws.com'
Action:
- 'sts:AssumeRole'
Path: '/'
Policies:
- PolicyName: logs
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- s3:GetObject
- s3:GetObjectVersion
- s3:GetBucketVersioning
Resource: "*"
Effect: Allow
- Action:
- s3:PutObject
Resource:
- arn:aws:s3:::codepipeline*
- arn:aws:s3:::elasticbeanstalk*
Effect: Allow
- Action:
- codecommit:CancelUploadArchive
- codecommit:GetBranch
- codecommit:GetCommit
- codecommit:GetUploadArchiveStatus
- codecommit:UploadArchive
Resource: "*"
Effect: Allow
- Action:
- codedeploy:CreateDeployment
- codedeploy:GetApplicationRevision
- codedeploy:GetDeployment
- codedeploy:GetDeploymentConfig
- codedeploy:RegisterApplicationRevision
Resource: "*"
Effect: Allow
- Action:
- elasticbeanstalk:*
- ec2:*
- elasticloadbalancing:*
- autoscaling:*
- cloudwatch:*
- s3:*
- sns:*
- cloudformation:*
- rds:*
- sqs:*
- ecs:*
- iam:PassRole
Resource: "*"
Effect: Allow
- Action:
- lambda:InvokeFunction
- lambda:ListFunctions
Resource: "*"
Effect: Allow
- Action:
- opsworks:CreateDeployment
- opsworks:DescribeApps
- opsworks:DescribeCommands
- opsworks:DescribeDeployments
- opsworks:DescribeInstances
- opsworks:DescribeStacks
- opsworks:UpdateApp
- opsworks:UpdateStack
Resource: "*"
Effect: Allow
CodePipeline:
DependsOn:
- EC2Instance
Type: "AWS::CodePipeline::Pipeline"
Properties:
ArtifactStore:
Location: !Ref ArtifactStore
Type: S3
RoleArn: !Sub '${CodePipelineIAMRole.Arn}'
Stages:
- Name: Source
Actions:
- Name: Source
ActionTypeId:
Category: Source
Owner: ThirdParty
Version: 1
Provider: GitHub
OutputArtifacts:
- Name: staticwebsite
Configuration:
Owner: !Ref GitHubOwner
Repo: !Ref GitHubRepo
Branch: master
OAuthToken: !Ref GitHubOAuthToken
- Name: Deploy
Actions:
- Name: Deploy
ActionTypeId:
Category: Deploy
Owner: AWS
Version: 1
Provider: CodeDeploy
InputArtifacts:
- Name: staticwebsite
Configuration:
ApplicationName: !Ref Application
DeploymentGroupName: !Ref DeploymentGroup
- Name: Test
Actions:
- Name: Test
ActionTypeId:
Category: Invoke
Owner: AWS
Version: 1
Provider: Lambda
Configuration:
FunctionName: !Ref TestLambda
UserParameters: !Sub 'http://${EC2Instance.PublicDnsName}'
CodeDeployIAMRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- 'codedeploy.amazonaws.com'
Action:
- 'sts:AssumeRole'
Path: '/'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole'
Application:
Type: "AWS::CodeDeploy::Application"
DeploymentGroup:
Type: "AWS::CodeDeploy::DeploymentGroup"
Properties:
ApplicationName: !Ref Application
Ec2TagFilters:
- Key: DeploymentGroup
Type: KEY_AND_VALUE
Value: !Ref AWS::StackName
ServiceRoleArn: !Sub '${CodeDeployIAMRole.Arn}'
SecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: 'static website'
VpcId: !Ref VPC
SecurityGroupIngress:
- FromPort: 80
ToPort: 80
IpProtocol: tcp
CidrIp: '0.0.0.0/0'
EC2InstanceProfile:
Type: 'AWS::IAM::InstanceProfile'
Properties:
Path: '/'
Roles:
- !Ref EC2InstanceIAMRole
EC2InstanceIAMRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- 'ec2.amazonaws.com'
Action:
- 'sts:AssumeRole'
Path: '/'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforAWSCodeDeploy'
EC2Instance:
Type: 'AWS::EC2::Instance'
Properties:
IamInstanceProfile: !Ref EC2InstanceProfile
ImageId: !FindInMap [RegionMap, !Ref 'AWS::Region', AMI]
InstanceType: 't2.micro'
SecurityGroupIds:
- !Ref SecurityGroup
UserData:
'Fn::Base64': !Sub |
#!/bin/bash -x
yum update -y && yum install -y ruby wget && wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install && chmod +x ./install && ./install auto && service codedeploy-agent start
/opt/aws/bin/cfn-signal -e $? --region ${AWS::Region} --stack ${AWS::StackName} --resource EC2Instance
SubnetId: !Ref Subnet
Tags:
- Key: DeploymentGroup
Value: !Ref AWS::StackName
- Key: Name
Value: !Ref AWS::StackName
CreationPolicy:
ResourceSignal:
Count: 1
Timeout: PT10M
LambdaIAMRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- 'lambda.amazonaws.com'
Action:
- 'sts:AssumeRole'
Path: '/'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
Policies:
- PolicyName: codepipeline
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- codepipeline:PutJobFailureResult
- codepipeline:PutJobSuccessResult
Resource: "*"
Effect: Allow
TestLambda:
Type: "AWS::Lambda::Function"
Properties:
Code:
S3Bucket: 'codepipeline-codedeploy-example-lambda'
S3Key: 'v2.zip'
FunctionName: 'codepipeline_http_test'
Handler: 'index.handler'
MemorySize: 128
Role: !Sub '${LambdaIAMRole.Arn}'
Runtime: 'nodejs4.3'
Timeout: 30
Outputs:
URL:
Description: 'The URL pointing to the static website.'
Value: !Sub 'http://${EC2Instance.PublicDnsName}'