Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A CloudFormation test template to create a VPC with a public subnet and spin up a internet connected instance inside of it.
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Building a VPC from scratch with CloudFormation",
"Mappings": {
"SubnetConfig": {
"VPC": { "CIDR": "10.0.0.0/16" },
"Public": { "CIDR": "10.0.0.0/24" }
},
"RegionMap" : {
"eu-west-1": { "AMI" : "ami-8e987ef9" }
}
},
"Resources": {
"VPC": {
"Type" : "AWS::EC2::VPC",
"Properties" : {
"CidrBlock" : { "Fn::FindInMap": ["SubnetConfig", "VPC", "CIDR"] },
"EnableDnsSupport" : "true",
"EnableDnsHostnames": "true",
"Tags" : [ { "Key": "Application", "Value": { "Ref": "AWS::StackName"}},
{ "Key": "Network", "Value": "Public"}
]
}
},
"PublicSubnet": {
"Type" : "AWS::EC2::Subnet",
"Properties" : {
"CidrBlock" : { "Fn::FindInMap": ["SubnetConfig", "Public", "CIDR"] },
"Tags" : [ { "Key": "Application", "Value": { "Ref": "AWS::StackName"}},
{ "Key": "Network", "Value": "Public"}
],
"VpcId" : { "Ref" : "VPC" }
}
},
"InternetGateway": {
"Type" : "AWS::EC2::InternetGateway"
},
"GatewayToInternet": {
"Type" : "AWS::EC2::VPCGatewayAttachment",
"Properties" : {
"InternetGatewayId" : { "Ref": "InternetGateway" },
"VpcId" : { "Ref": "VPC" }
}
},
"PublicRouteTable": {
"Type" : "AWS::EC2::RouteTable",
"Properties" : {
"VpcId" : { "Ref": "VPC" }
}
},
"PublicRoute": {
"Type" : "AWS::EC2::Route",
"DependsOn": "GatewayToInternet",
"Properties" : {
"DestinationCidrBlock" : "0.0.0.0/0",
"GatewayId" : { "Ref": "InternetGateway"},
"RouteTableId" : { "Ref": "PublicRouteTable" }
}
},
"PublicSubnetRouteTableAssociation": {
"Type" : "AWS::EC2::SubnetRouteTableAssociation",
"Properties" : {
"RouteTableId" : { "Ref": "PublicRouteTable" },
"SubnetId" : { "Ref": "PublicSubnet" }
}
},
"PublicInstance": {
"Type" : "AWS::EC2::Instance",
"DependsOn": "GatewayToInternet",
"Properties" : {
"ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
"InstanceType" : "t1.micro",
"NetworkInterfaces" : [ { "AssociatePublicIpAddress": "true",
"DeviceIndex": "0",
"DeleteOnTermination": "true",
"SubnetId" : { "Ref": "PublicSubnet" } } ]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment