Skip to content

Instantly share code, notes, and snippets.

@pitakill
Created April 12, 2019 02:21
Show Gist options
  • Save pitakill/073d38c2b3de3543df21aa1ed9d58358 to your computer and use it in GitHub Desktop.
Save pitakill/073d38c2b3de3543df21aa1ed9d58358 to your computer and use it in GitHub Desktop.
CloudFormation VPC, IG, EC2
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"MyVPC": {
"Type": "AWS::EC2::VPC",
"Properties": {
"CidrBlock": {
"Ref": "VpcCidrBlock"
}
}
},
"PublicSubnet": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"VpcId": {
"Ref": "MyVPC"
},
"CidrBlock": {
"Ref": "PublicSubnetCidrBlock"
},
"MapPublicIpOnLaunch": "True"
}
},
"IGW" : {
"Type" : "AWS::EC2::InternetGateway",
"Properties" : {}
},
"IgwAttachment" : {
"Type" : "AWS::EC2::VPCGatewayAttachment",
"Properties" : {
"VpcId" : { "Ref" : "MyVPC" },
"InternetGatewayId" : { "Ref" : "IGW" }
}
},
"PublicRouteTable" : {
"Type" : "AWS::EC2::RouteTable",
"Properties" : {
"VpcId" : { "Ref" : "MyVPC" }
}
},
"PublicRoute" : {
"Type" : "AWS::EC2::Route",
"DependsOn" : "IGW",
"Properties" : {
"RouteTableId" : { "Ref" : "PublicRouteTable" },
"DestinationCidrBlock" : "0.0.0.0/0",
"GatewayId" : { "Ref" : "IGW" }
}
},
"PublicSubnetRouteAssociation": {
"Type" : "AWS::EC2::SubnetRouteTableAssociation",
"Properties" : {
"RouteTableId" : { "Ref" : "PublicRouteTable" },
"SubnetId" : { "Ref" : "PublicSubnet" }
}
},
"InstanceSecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Allow HTTP and SSH traffic to our host",
"VpcId" : {"Ref" : "MyVPC"},
"SecurityGroupIngress" : [
{
"IpProtocol" : "tcp",
"FromPort" : "22",
"ToPort" : "22",
"CidrIp" : "0.0.0.0/0"
},
{
"IpProtocol" : "tcp",
"FromPort" : "80",
"ToPort" : "80",
"CidrIp" : "0.0.0.0/0"
}
],
"SecurityGroupEgress" : [
{
"IpProtocol" : "tcp",
"FromPort" : "1",
"ToPort" : "65535",
"CidrIp" : "0.0.0.0/0"
}
]
}
},
"MyEC2Instance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"ImageId": "ami-035be7bafff33b6b6",
"KeyName" : { "Ref": "SSHKeyName"},
"SubnetId": {
"Ref": "PublicSubnet"
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroupIds": [
{
"Ref": "InstanceSecurityGroup"
}
],
"UserData": {
"Fn::Base64": { "Fn::Join": [ "", [
"#!/bin/bash\n",
"sudo yum update -y\n",
"sudo amazon-linux-extras install docker -y\n",
"sudo service docker start\n",
"sudo usermod -a -G docker ec2-user\n",
"sudo docker run -d -p 80:80 pitakill/ecb"
]]}}
}
}
},
"Parameters": {
"VpcCidrBlock": {
"Description": "VPC CIDR Range (will be a /16 block)",
"Type": "String",
"Default": "10.0.0.0/16"
},
"PublicSubnetCidrBlock": {
"Description": "Public Subnet CIDR Range",
"Type": "String",
"Default": "10.0.1.0/24"
},
"SSHKeyName": {
"Description": "Name of an existing EC2 KeyPair to enable SSH access to the instances",
"Default": "pitakill's key",
"Type": "AWS::EC2::KeyPair::KeyName",
"ConstraintDescription": "must be the name of an existing EC2 KeyPair."
},
"InstanceType": {
"Description": "Select one of the possible instance types",
"Type": "String",
"Default": "t2.micro",
"AllowedValues": ["t2.micro"]
}
},
"Outputs": {
"MyEC2InstancePublicIP": {
"Value": {
"Fn::GetAtt": [
"MyEC2Instance",
"PublicIp"
]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment