forked from cloudtools/troposphere
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathECSCluster.py
More file actions
173 lines (160 loc) · 7.3 KB
/
Copy pathECSCluster.py
File metadata and controls
173 lines (160 loc) · 7.3 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
from troposphere import Base64, Join
from troposphere import Ref, Template
from troposphere.cloudformation import Init, InitConfig, InitFiles, InitFile
from troposphere.cloudformation import InitServices, InitService
from troposphere.iam import PolicyType
from troposphere.autoscaling import LaunchConfiguration
from troposphere.iam import Role
from troposphere.ecs import Cluster
from troposphere.autoscaling import AutoScalingGroup, Metadata
from troposphere.iam import InstanceProfile
t = Template()
t.add_version('2010-09-09')
PolicyEcr = t.add_resource(PolicyType(
'PolicyEcr',
PolicyName='EcrPolicy',
PolicyDocument={'Version': '2012-10-17',
'Statement': [{'Action': ['ecr:GetAuthorizationToken'],
'Resource': ['*'],
'Effect': 'Allow'},
{'Action': ['ecr:GetDownloadUrlForLayer',
'ecr:BatchGetImage',
'ecr:BatchCheckLayerAvailability'
],
'Resource': [
'*'],
'Effect': 'Allow',
'Sid': 'AllowPull'},
]},
Roles=[Ref('EcsClusterRole')],
))
PolicyEcs = t.add_resource(PolicyType(
'PolicyEcs',
PolicyName='EcsPolicy',
PolicyDocument={'Version': '2012-10-17',
'Statement': [
{'Action': ['ecs:CreateCluster',
'ecs:RegisterContainerInstance',
'ecs:DeregisterContainerInstance',
'ecs:DiscoverPollEndpoint',
'ecs:Submit*',
'ecs:Poll',
'ecs:StartTelemetrySession'],
'Resource': '*',
'Effect': 'Allow'}
]},
Roles=[Ref('EcsClusterRole')],
))
PolicyCloudwatch = t.add_resource(PolicyType(
'PolicyCloudwatch',
PolicyName='Cloudwatch',
PolicyDocument={'Version': '2012-10-17',
'Statement': [{'Action': ['cloudwatch:*'], 'Resource': '*',
'Effect': 'Allow'}]},
Roles=[Ref('EcsClusterRole')],
))
ContainerInstances = t.add_resource(LaunchConfiguration(
'ContainerInstances',
Metadata=Metadata(
Init({
'config': InitConfig(
files=InitFiles({
'/etc/cfn/cfn-hup.conf': InitFile(
content=Join('', ['[main]\n', 'stack=', Ref('AWS::StackId'), # NOQA
'\n', 'region=', Ref('AWS::Region'), '\n']), # NOQA
mode='000400',
owner='root',
group='root'
),
'/etc/cfn/hooks.d/cfn-auto-reloader.conf': InitFile(
content=Join('', ['[cfn-auto-reloader-hook]\n',
'triggers=post.update\n',
'path=Resources.ContainerInstances.Metadata.AWS::CloudFormation::Init\n', # NOQA
'action=/opt/aws/bin/cfn-init -v ', '--stack ', Ref( # NOQA
'AWS::StackName'), ' --resource ContainerInstances ', ' --region ', Ref('AWS::Region'), '\n', # NOQA
'runas=root\n']),
mode='000400',
owner='root',
group='root'
)},
),
services=InitServices({
'cfn-hup': InitService(
ensureRunning='true',
enabled='true',
files=['/etc/cfn/cfn-hup.conf',
'/etc/cfn/hooks.d/cfn-auto-reloader.conf']
)}
),
commands={
'01_add_instance_to_cluster': {'command': Join('',
['#!/bin/bash\n', # NOQA
'echo ECS_CLUSTER=', # NOQA
Ref('ECSCluster'), # NOQA
' >> /etc/ecs/ecs.config'])}, # NOQA
'02_install_ssm_agent': {'command': Join('',
['#!/bin/bash\n',
'yum -y update\n', # NOQA
'curl https://amazon-ssm-eu-west-1.s3.amazonaws.com/latest/linux_amd64/amazon-ssm-agent.rpm -o amazon-ssm-agent.rpm\n', # NOQA
'yum install -y amazon-ssm-agent.rpm' # NOQA
])}
}
)
}
),
),
UserData=Base64(Join('',
['#!/bin/bash -xe\n',
'yum install -y aws-cfn-bootstrap\n',
'/opt/aws/bin/cfn-init -v ',
' --stack ',
Ref('AWS::StackName'),
' --resource ContainerInstances ',
' --region ',
Ref('AWS::Region'),
'\n',
'/opt/aws/bin/cfn-signal -e $? ',
' --stack ',
Ref('AWS::StackName'),
' --resource ECSAutoScalingGroup ',
' --region ',
Ref('AWS::Region'),
'\n'])),
ImageId='ami-13f84d60',
KeyName='yourkey',
SecurityGroups=['sg-96114ef2'],
IamInstanceProfile=Ref('EC2InstanceProfile'),
InstanceType='t2.micro',
AssociatePublicIpAddress='true',
))
ECSCluster = t.add_resource(Cluster(
'ECSCluster',
))
ECSAutoScalingGroup = t.add_resource(AutoScalingGroup(
'ECSAutoScalingGroup',
DesiredCapacity='1',
MinSize='1',
MaxSize='1',
VPCZoneIdentifier=['subnet-72849a0a', 'subnet-72849a08'],
AvailabilityZones=['eu-west-1a', 'eu-west-1b'],
LaunchConfigurationName=Ref('ContainerInstances'),
))
EC2InstanceProfile = t.add_resource(InstanceProfile(
'EC2InstanceProfile',
Path='/',
Roles=[Ref('EcsClusterRole')],
))
EcsClusterRole = t.add_resource(Role(
'EcsClusterRole',
Path='/',
ManagedPolicyArns=[
'arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM'
],
AssumeRolePolicyDocument={'Version': '2012-10-17',
'Statement': [{'Action': 'sts:AssumeRole',
'Principal':
{'Service': 'ec2.amazonaws.com'},
'Effect': 'Allow',
}]}
))
print(t.to_json())