-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcf-template.yml
454 lines (453 loc) · 13.1 KB
/
cf-template.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
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
---
Description: Splits an input AMI into a new AMI spread across multiple EBS snapshots
Parameters:
AppSourceAMI:
Description: Source AMI
Type: String
Default: ami-0625f87c
RootDevPartition:
Description: Root Filesystem Partition on the AMI's root disk
Type: Number
Default: 2
AllowedValues:
- 1
- 2
- 3
- 4
AppFilesystemParameters:
Description: "space-separated filesystem designations of the form [PATH]:[SIZE]:[MOUNT_OPTIONS]"
# Default disk partitions for Center for Internet Security recommendations
Default: "/tmp:20:nodev,nosuid,noexec /var/tmp:7:nodev,nosuid,noexec /var:10:defaults /home:10:defaults,nodev /var/log/audit:5:defaults /var/log:15:defaults"
Type: String
LogRetention:
Description: Number of days to retain the log
Type: Number
Default: 1
SoftwareRepoURL:
Description: The git checkout URL for the repo that contains splitAMI.rb
Type: String
Default: https://github.com/benton/splitami.git
SoftwareVersion:
Description: Which branch / Git reference of splitAMI.rb to run
Type: String
Default: root-dev-partition
InstanceType:
Description: Worker EC2 instance type (m3.medium, etc).
Type: String
ConstraintDescription: must be a valid EC2 instance type.
Default: t2.micro
InstanceBootKey:
Description: EC2 Boot Key, installed for user 'ec2-user'
Type: AWS::EC2::KeyPair::KeyName
Default: medistrano
Shutdown:
Description: Whether or not to delete this CloudFormation Stack on completion
Type: String
Default: true
AllowedValues:
- true
- false
PublicImage:
Description: Whether or not to make the resulting AMI Public
Type: String
Default: false
AllowedValues:
- true
- false
Resources:
AMIInstance:
Type: "AWS::EC2::Instance"
DependsOn: LogStream
Metadata:
AWS::CloudFormation::Init:
AppInstall:
package_upgrade: true
packages:
yum:
git: []
gcc: []
ruby-devel: []
files:
"/scripts/app-install":
content:
!Sub |
echo Installing splitAMI...
gem install io-console bundler
git clone ${SoftwareRepoURL} /app
cd /app && git checkout ${SoftwareVersion}
bundle install --jobs=4
AppRun:
files:
"/scripts/app-run":
content:
!Sub |
echo Running splitAMI...
./splitAMI.rb ${AppSourceAMI} ${AppFilesystemParameters}
SetupLogging:
files:
"/scripts/setup-logging":
content: !Sub |
curl -O \
https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py
chmod +x ./awslogs-agent-setup.py
./awslogs-agent-setup.py -n -r ${AWS::Region} -c /etc/awslogs.conf
"/etc/awslogs.conf":
content:
Fn::Join:
- ''
- - !Sub |
[general]
state_file = /var/awslogs/state/agent-state
[/var/log/cloud-init-output.log]
file = /var/log/cloud-init-output.log
log_group_name = ${LogGroup}
log_stream_name = ${LogStream}
datetime_format = %b %d %H:%M:%S
configSets:
All:
- SetupLogging
- AppInstall
- AppRun
Properties:
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
set -ex pipefail
export PATH="/usr/local/sbin:/usr/local/bin:$PATH:/opt/aws/bin"
export PUBLICIMAGE="${PublicImage}"
export ROOT_DEV_PARTITION=${RootDevPartition}
aws configure set default.region ${AWS::Region}
cfn-init --region ${AWS::Region} -s ${AWS::StackName} \
-r AMIInstance --configsets All
bash -ex /scripts/setup-logging
bash -ex /scripts/app-install
cd /app
bash -ex /scripts/app-run
if ${Shutdown} ; then
echo Deleting Stack ${AWS::StackName}...
sleep 30 # wait for CloudwatchLogs Agent to finish sending data
aws cloudformation delete-stack --stack-name ${AWS::StackName}
fi
ImageId:
Fn::FindInMap:
- RegionAMIMap
- Ref: AWS::Region
- AMI
InstanceType: !Ref InstanceType
KeyName: !Ref InstanceBootKey
SecurityGroupIds:
- !GetAtt EC2SecurityGroup.GroupId
SubnetId: !Ref PublicSubnet
IamInstanceProfile: !Ref IAMInstanceProfile
Tags:
- Key: Application
Value: !Ref AWS::StackName
- Key: Name
Value: AMI Splitter
- Key: Project
Value: splitami
- Key: Environment
Value: sandbox
- Key: Type
Value: app
- Key: 'mdsol:splitami:sandbox:app'
Value: ""
IAMRole:
Type: AWS::IAM::Role
Properties:
Path: "/"
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: CloudWatchLogs-LogEvents
PolicyDocument:
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:DescribeLogGroups
- logs:DescribeLogStreams
- logs:PutLogEvents
Resource: "*"
- PolicyName: Split-EBS-AMIs
PolicyDocument:
Statement:
- Effect: Allow
Action:
- ec2:AttachVolume
- ec2:CreateVolume
- ec2:DeleteVolume
- ec2:CreateImage
- ec2:DescribeVolumes
- ec2:DetachVolume
- ec2:DescribeInstances
- ec2:CreateSnapshot
- ec2:DescribeSnapshots
- ec2:DescribeImages
- ec2:RegisterImage
- ec2:CreateTags
- ec2:ModifyImageAttribute
- ec2:ModifySnapshotAttribute
Resource: "*"
- PolicyName: Delete-My-Own-CF-stack
PolicyDocument:
Statement:
- Action:
- cloudformation:DeleteStack
Effect: Allow
Resource: !Sub arn:aws:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${AWS::StackName}/*
- Action:
- ec2:TerminateInstances
- ec2:DeleteSecurityGroup
Condition:
StringEquals:
ec2:ResourceTag/aws:cloudformation:stack-id:
Ref: AWS::StackId
Effect: Allow
Resource: "*"
- Action:
- ec2:DescribeInstances
- ec2:DeleteVpc
- ec2:DeleteSubnet
- ec2:DeleteInternetGateway
- ec2:DeleteNetworkAcl
- ec2:DetachInternetGateway
- ec2:DeleteRouteTable
- ec2:DeleteNetworkAclEntry
- ec2:DisassociateRouteTable
- ec2:DescribeRouteTables
- ec2:DeleteRoute
- ec2:DescribeNetworkAcls
- ec2:DescribeInternetGatewaysResult
- ec2:DescribeSubnets
- ec2:ReplaceNetworkAclAssociation
- ec2:DescribeInternetGateways
- ec2:DescribeVpcs
Effect: Allow
Resource: "*"
- Action:
- iam:DeleteInstanceProfile
- iam:RemoveRoleFromInstanceProfile
Effect: Allow
Resource: !Sub >-
arn:aws:iam::${AWS::AccountId}:instance-
profile/${AWS::StackName}-IAMInstanceProfile-*
Resource:
Fn::Join:
- ':'
- - 'arn:aws:iam:'
- Ref: AWS::AccountId
- !Sub instance-profile/${AWS::StackName}-IAMInstanceProfile-*
- Action:
- iam:DeleteRole
- iam:DeleteRolePolicy
Effect: Allow
Resource: !Sub >-
arn:aws:iam::${AWS::AccountId}:role/${AWS::StackName}-IAMRole-*
IAMInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: "/"
Roles:
- Ref: IAMRole
VPC:
Type: AWS::EC2::VPC
DependsOn: IAMInstanceProfile
Properties:
EnableDnsSupport: true
EnableDnsHostnames: true
CidrBlock:
Fn::FindInMap:
- SubnetConfig
- VPC
- CIDR
Tags:
- Key: Application
Value: !Ref AWS::StackName
- Key: Network
Value: Public
InternetGateway:
Type: AWS::EC2::InternetGateway
DependsOn: IAMInstanceProfile
Properties:
Tags:
- Key: Application
Value: !Ref AWS::StackName
- Key: Network
Value: Public
PublicSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
MapPublicIpOnLaunch: true
CidrBlock:
Fn::FindInMap:
- SubnetConfig
- Public
- CIDR
Tags:
- Key: Application
Value: !Ref AWS::StackName
- Key: Network
Value: Public
GatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId:
Ref: VPC
InternetGatewayId:
Ref: InternetGateway
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId:
Ref: VPC
Tags:
- Key: Application
Value: !Ref AWS::StackName
- Key: Network
Value: Public
PublicRoute:
Type: AWS::EC2::Route
DependsOn: GatewayAttachment
Properties:
RouteTableId:
Ref: PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId:
Ref: InternetGateway
PublicSubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: PublicSubnet
RouteTableId:
Ref: PublicRouteTable
PublicNetworkAcl:
Type: AWS::EC2::NetworkAcl
Properties:
VpcId:
Ref: VPC
Tags:
- Key: Application
Value: !Ref AWS::StackName
- Key: Network
Value: Public
InboundPublicNetworkAclEntry:
Type: AWS::EC2::NetworkAclEntry
Properties:
NetworkAclId:
Ref: PublicNetworkAcl
RuleNumber: '100'
Protocol: '6'
RuleAction: allow
Egress: false
CidrBlock: 0.0.0.0/0
PortRange:
From: '0'
To: '65535'
OutboundPublicNetworkAclEntry:
Type: AWS::EC2::NetworkAclEntry
Properties:
NetworkAclId:
Ref: PublicNetworkAcl
RuleNumber: '100'
Protocol: '6'
RuleAction: allow
Egress: true
CidrBlock: 0.0.0.0/0
PortRange:
From: '0'
To: '65535'
PublicSubnetNetworkAclAssociation:
Type: AWS::EC2::SubnetNetworkAclAssociation
Properties:
SubnetId:
Ref: PublicSubnet
NetworkAclId:
Ref: PublicNetworkAcl
EC2SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow ssh to host
VpcId:
Ref: VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: 0.0.0.0/0
LogGroup:
DependsOn: GatewayAttachment
Type: "AWS::Logs::LogGroup"
DeletionPolicy: Retain
Properties:
RetentionInDays: !Ref LogRetention
LogStream:
Type: "AWS::Logs::LogStream"
DeletionPolicy: Retain
Properties:
LogGroupName: !Ref LogGroup
Outputs:
LogURL:
Description: The AWS Console URL for the log output
Value:
Fn::Join:
- ''
- - https://console.aws.amazon.com/cloudwatch/home?region=
- !Sub ${AWS::Region}#logEventViewer:group=${LogGroup};
- !Sub stream=${LogStream}
LogGroup:
Description: The CloudwatchLogs LogGroup
Value: !Ref LogGroup
LogStream:
Description: The CloudwatchLogs LogStream
Value: !Ref LogStream
DebugCommand:
Description: Run this command if something goes wrong
Value: !Sub |-
ssh -i ~/.ssh/${InstanceBootKey} ec2-user@${AMIInstance.PublicDnsName}
cat /var/log/cloud-init-output.log
Mappings:
# AMI Name = amzn-ami-hvm-2017.03.0.20170417-x86_64-gp2
RegionAMIMap:
ap-northeast-1: # Tokyo, Japan
AMI: ami-923d12f5
ap-northeast-2: # Seoul, South Korea
AMI: ami-9d15c7f3
ap-south-1: # Mumbai, India
AMI: ami-52c7b43d
sa-east-1: # Sao Paolo, Brazil
AMI: ami-37cfad5b
ap-southeast-2: # Sydney, Australia
AMI: ami-162c2575
ap-southeast-1: # Singapore
AMI: ami-fc5ae39f
eu-central-1: # Frankfurt, Germany
AMI: ami-b968bad6
eu-west-1: # Dublin, Ireland
AMI: ami-01ccc867
eu-west-2: # London, England
AMI: ami-b6daced2
us-east-1: # Virginia, USA
AMI: ami-c58c1dd3
us-east-2: # Ohio, USA
AMI: ami-4191b524
us-west-1: # California, USA
AMI: ami-7a85a01a
us-west-2: # Oregon, USA
AMI: ami-4836a428
SubnetConfig:
VPC:
CIDR: 10.0.0.0/16
Public:
CIDR: 10.0.0.0/24