This repository has been archived by the owner on Mar 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.py
195 lines (172 loc) · 5.53 KB
/
template.py
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
from awacs.aws import Action, Policy, Principal, Statement
from troposphere import Parameter, Ref, Template
from troposphere.helpers.userdata import from_file
import troposphere.autoscaling as asc
import troposphere.ec2 as ec2
import troposphere.iam as iam
template = Template()
template.set_version("2010-09-09")
template.set_description("euca.me dns and http services")
availability_zone_1 = template.add_parameter(Parameter(
"AvailabilityZone1",
Description="1st EC2 availability zone to use",
Type="String",
Default="us-west-1a",
ConstraintDescription="must be the name of an availability zone."
))
availability_zone_2 = template.add_parameter(Parameter(
"AvailabilityZone2",
Description="2nd EC2 availability zone to use",
Type="String",
Default="us-west-1c",
ConstraintDescription="must be the name of an availability zone."
))
ssh_key_name = template.add_parameter(Parameter(
"KeyName",
Description="Name of an existing EC2 KeyPair for instance SSH access",
Type="String",
ConstraintDescription="must be the name of an existing EC2 KeyPair."
))
instance_type = template.add_parameter(Parameter(
"InstanceType",
Description="EC2 instance type",
Type="String",
Default="t2.nano",
AllowedValues=["t2.nano", "t2.micro", "t2.small", "t2.medium", "m1.small",
"m1.medium", "m1.large"],
ConstraintDescription="must be a valid EC2 instance type."
))
image = template.add_parameter(Parameter(
"Image",
Description="EC2 image identifier",
Type="String",
Default="ami-bf5540df",
AllowedPattern="[ae]mi-[0-9a-fA-F]{8}",
ConstraintDescription="must be a valid EC2 image identifier."
))
vpc = template.add_resource(ec2.VPC(
"Vpc",
CidrBlock="10.0.0.0/16",
EnableDnsSupport=True,
EnableDnsHostnames=True,
Tags=ec2.Tags(Name="euca.me")
))
subnet_1 = template.add_resource(ec2.Subnet(
"Subnet1",
VpcId=Ref(vpc),
CidrBlock="10.0.0.0/24",
AvailabilityZone=Ref(availability_zone_1),
Tags=ec2.Tags(Name="euca.me")
))
subnet_2 = template.add_resource(ec2.Subnet(
"Subnet2",
VpcId=Ref(vpc),
CidrBlock="10.0.1.0/24",
AvailabilityZone=Ref(availability_zone_2),
Tags=ec2.Tags(Name="euca.me")
))
internet_gateway = template.add_resource(ec2.InternetGateway(
"InternetGateway",
Tags=ec2.Tags(Name="euca.me")
))
vpc_gateway_attachment = template.add_resource(ec2.VPCGatewayAttachment(
"GatewayToInternet",
VpcId=Ref(vpc),
InternetGatewayId=Ref(internet_gateway)
))
public_route_table = template.add_resource(ec2.RouteTable(
"PublicRouteTable",
VpcId=Ref(vpc),
Tags=ec2.Tags(Name="euca.me")
))
public_route = template.add_resource(ec2.Route(
"PublicRoute",
DependsOn=["GatewayToInternet"],
RouteTableId=Ref(public_route_table),
DestinationCidrBlock="0.0.0.0/0",
GatewayId=Ref(internet_gateway)
))
subnet_route_table_association_1 = template.add_resource(
ec2.SubnetRouteTableAssociation(
"SubnetRouteTableAssociation1",
SubnetId=Ref(subnet_1),
RouteTableId=Ref(public_route_table)
)
)
subnet_route_table_association_2 = template.add_resource(
ec2.SubnetRouteTableAssociation(
"SubnetRouteTableAssociation2",
SubnetId=Ref(subnet_2),
RouteTableId=Ref(public_route_table)
)
)
security_group = template.add_resource(ec2.SecurityGroup(
"SecurityGroup",
GroupDescription="SecurityGroup",
VpcId=Ref(vpc),
SecurityGroupIngress=[
ec2.SecurityGroupRule(IpProtocol="tcp", FromPort=53, ToPort=53,
CidrIp="0.0.0.0/0"),
ec2.SecurityGroupRule(IpProtocol="udp", FromPort=53, ToPort=53,
CidrIp="0.0.0.0/0"),
ec2.SecurityGroupRule(IpProtocol="tcp", FromPort=80, ToPort=80,
CidrIp="0.0.0.0/0")
],
Tags=ec2.Tags(Name="euca.me")
))
role = template.add_resource(iam.Role(
"Role",
AssumeRolePolicyDocument=Policy(
Version="2012-10-17",
Statement=[
Statement(
Action=[Action("sts", "AssumeRole")],
Effect="Allow",
Principal=Principal("Service", ["ec2.amazonaws.com"]))
]
),
Path="/",
Policies=[
iam.Policy(
PolicyName="assign-public-address",
PolicyDocument=Policy(
Version="2012-10-17",
Statement=[
Statement(
Action=[Action("ec2", "*Address*")],
Resource=["*"],
Effect="Allow"
)
]
)
)
]
))
instance_profile = template.add_resource(iam.InstanceProfile(
"InstanceProfile",
Path="/",
Roles=[Ref(role)]
))
launch_configuration = template.add_resource(asc.LaunchConfiguration(
"LaunchConfiguration",
ImageId=Ref(image),
SecurityGroups=[Ref(security_group)],
InstanceMonitoring=False,
IamInstanceProfile=Ref(instance_profile),
InstanceType=Ref(instance_type),
KeyName=Ref(ssh_key_name),
UserData=from_file("out/cloud-config.yaml"),
AssociatePublicIpAddress=True
))
autoscaling_group = template.add_resource(asc.AutoScalingGroup(
"AutoScalingGroup",
DependsOn=["PublicRoute"],
AvailabilityZones=[Ref(availability_zone_1), Ref(availability_zone_2)],
VPCZoneIdentifier=[Ref(subnet_1), Ref(subnet_2)],
LaunchConfigurationName=Ref(launch_configuration),
MinSize=0,
MaxSize=1,
DesiredCapacity=1,
Tags=asc.Tags(Name="euca.me")
))
print(template.to_json())