Skip to content

Instantly share code, notes, and snippets.

@jhuizy
Created August 17, 2017 01:04
Show Gist options
  • Save jhuizy/5b0a6a58acf64dd2aa3030d8f05696ec to your computer and use it in GitHub Desktop.
Save jhuizy/5b0a6a58acf64dd2aa3030d8f05696ec to your computer and use it in GitHub Desktop.
from cfn_flip import to_yaml
from troposphere import Ref, Template, Select, GetAZs, Output, GetAtt
import troposphere.ec2 as ec2
import troposphere.elasticloadbalancing as lb
import troposphere.autoscaling as asg
t = Template()
def subnet(vpc, i, az, classC, public=True):
return ec2.Subnet(
"{0}Subnet{1}".format("Public" if public else "Private", az),
CidrBlock = "10.{0}.{1}.0/24".format(classC, i),
MapPublicIpOnLaunch = public,
VpcId = Ref(vpc),
AvailabilityZone = Select(az, GetAZs())
)
vpc = ec2.VPC(
"VPC",
CidrBlock = "10.0.0.0/16",
EnableDnsSupport = True,
EnableDnsHostnames = True,
InstanceTenancy = "default"
)
t.add_resource(vpc)
for subnet in (map(lambda i: subnet(vpc, i, i / 2, 0, i % 2 == 0), range(0, 4))):
t.add_resource(subnet)
t.add_output(Output(subnet.title, Value=Ref(subnet)))
print(to_yaml(t.to_json()))
# Produces following output
# Outputs:
# PrivateSubnet0:
# Value: !Ref 'PrivateSubnet0'
# PrivateSubnet1:
# Value: !Ref 'PrivateSubnet1'
# PublicSubnet0:
# Value: !Ref 'PublicSubnet0'
# PublicSubnet1:
# Value: !Ref 'PublicSubnet1'
# Resources:
# PrivateSubnet0:
# Properties:
# AvailabilityZone: !Select [0, !GetAZs '']
# CidrBlock: 10.0.1.0/24
# MapPublicIpOnLaunch: 'false'
# VpcId: !Ref 'VPC'
# Type: AWS::EC2::Subnet
# PrivateSubnet1:
# Properties:
# AvailabilityZone: !Select [1, !GetAZs '']
# CidrBlock: 10.0.3.0/24
# MapPublicIpOnLaunch: 'false'
# VpcId: !Ref 'VPC'
# Type: AWS::EC2::Subnet
# PublicSubnet0:
# Properties:
# AvailabilityZone: !Select [0, !GetAZs '']
# CidrBlock: 10.0.0.0/24
# MapPublicIpOnLaunch: 'true'
# VpcId: !Ref 'VPC'
# Type: AWS::EC2::Subnet
# PublicSubnet1:
# Properties:
# AvailabilityZone: !Select [1, !GetAZs '']
# CidrBlock: 10.0.2.0/24
# MapPublicIpOnLaunch: 'true'
# VpcId: !Ref 'VPC'
# Type: AWS::EC2::Subnet
# VPC:
# Properties:
# CidrBlock: 10.0.0.0/16
# EnableDnsHostnames: 'true'
# EnableDnsSupport: 'true'
# InstanceTenancy: default
# Type: AWS::EC2::VPC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment