-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVPC_EC2_Instance_With_Multiple_Dynamic_IPAddresses.py
More file actions
151 lines (135 loc) · 4.28 KB
/
VPC_EC2_Instance_With_Multiple_Dynamic_IPAddresses.py
File metadata and controls
151 lines (135 loc) · 4.28 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
# Converted from VPC_EC2_Instance_With_Multiple_Dynamic_IPAddresses
# template located at:
# http://aws.amazon.com/cloudformation/aws-cloudformation-templates/
from troposphere import FindInMap, GetAtt, Join
from troposphere import Parameter, Output, Ref, Select, Tags, Template
import troposphere.ec2 as ec2
template = Template()
keyname_param = template.add_parameter(Parameter(
"KeyName",
Description="Name of an existing EC2 KeyPair to enable SSH "
"access to the instance",
Type="String",
))
vpcid_param = template.add_parameter(Parameter(
"VpcId",
Description="VpcId of your existing Virtual Private Cloud (VPC)",
Type="String",
))
subnetid_param = template.add_parameter(Parameter(
"SubnetId",
Description="SubnetId of an existing subnet (for the primary network) in "
"your Virtual Private Cloud (VPC)" "access to the instance",
Type="String",
))
secondary_ip_param = template.add_parameter(Parameter(
"SecondaryIPAddressCount",
Description="Number of secondary IP addresses to assign to the network "
"interface (1-5)",
ConstraintDescription="must be a number from 1 to 5.",
Type="Number",
Default="1",
MinValue="1",
MaxValue="5",
))
sshlocation_param = template.add_parameter(Parameter(
"SSHLocation",
Description="The IP address range that can be used to SSH to the "
"EC2 instances",
Type="String",
MinLength="9",
MaxLength="18",
Default="0.0.0.0/0",
AllowedPattern="(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})"
"/(\\d{1,2})",
ConstraintDescription="must be a valid IP CIDR range of the "
"form x.x.x.x/x."
))
template.add_mapping('RegionMap', {
"us-east-1": {"AMI": "ami-7f418316"},
"us-west-1": {"AMI": "ami-951945d0"},
"us-west-2": {"AMI": "ami-16fd7026"},
"eu-west-1": {"AMI": "ami-24506250"},
"sa-east-1": {"AMI": "ami-3e3be423"},
"ap-southeast-1": {"AMI": "ami-74dda626"},
"ap-northeast-1": {"AMI": "ami-dcfa4edd"}
})
eip1 = template.add_resource(ec2.EIP(
"EIP1",
Domain="vpc",
))
ssh_sg = template.add_resource(ec2.SecurityGroup(
"SSHSecurityGroup",
VpcId=Ref(vpcid_param),
GroupDescription="Enable SSH access via port 22",
SecurityGroupIngress=[
ec2.SecurityGroupRule(
IpProtocol="tcp",
FromPort="22",
ToPort="22",
CidrIp=Ref(sshlocation_param),
),
],
))
eth0 = template.add_resource(ec2.NetworkInterface(
"Eth0",
Description="eth0",
GroupSet=[Ref(ssh_sg), ],
SourceDestCheck=True,
SubnetId=Ref(subnetid_param),
Tags=Tags(
Name="Interface 0",
Interface="eth0",
),
SecondaryPrivateIpAddressCount=Ref(secondary_ip_param),
))
eipassoc1 = template.add_resource(ec2.EIPAssociation(
"EIPAssoc1",
NetworkInterfaceId=Ref(eth0),
AllocationId=GetAtt("EIP1", "AllocationId"),
PrivateIpAddress=GetAtt("Eth0", "PrimaryPrivateIpAddress"),
))
ec2_instance = template.add_resource(ec2.Instance(
"EC2Instance",
ImageId=FindInMap("RegionMap", Ref("AWS::Region"), "AMI"),
KeyName=Ref(keyname_param),
NetworkInterfaces=[
ec2.NetworkInterfaceProperty(
NetworkInterfaceId=Ref(eth0),
DeviceIndex="0",
),
],
Tags=Tags(Name="MyInstance",)
))
template.add_output([
Output(
"InstanceId",
Description="InstanceId of the newly created EC2 instance",
Value=Ref(ec2_instance),
),
Output(
"EIP1",
Description="Primary public IP address for Eth0",
Value=Join(" ", [
"IP address", Ref(eip1), "on subnet", Ref(subnetid_param)
]),
),
Output(
"PrimaryPrivateIPAddress",
Description="Primary private IP address of Eth0",
Value=Join(" ", [
"IP address", GetAtt("Eth0", "PrimaryPrivateIpAddress"),
"on subnet", Ref(subnetid_param)
]),
),
Output(
"FirstSecondaryPrivateIPAddress",
Description="First secondary private IP address of Eth0",
Value=Join(" ", [
"IP address",
Select("0", GetAtt("Eth0", "SecondaryPrivateIpAddresses")),
"on subnet", Ref(subnetid_param)
]),
),
])
print(template.to_json())