Last active
May 19, 2020 21:23
-
-
Save cloudchopshop/a79859ec876b58212a3f33e5029817f4 to your computer and use it in GitHub Desktop.
Cloud Formation Template - ELB Web Hosting Instances
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "AWSTemplateFormatVersion": "2010-09-09", | |
| "Description" : "This template provisions a new VPC, with public and private subnets, loadbalanced EC2 instances hosting Apache", | |
| "Mappings" : { | |
| "RegionMap" : { | |
| "us-east-2" : {"HVM64" : "ami-0d542ef84ec55d71c"}, | |
| "us-west-2" : {"HVM64" : "ami-01460aa81365561fe"} | |
| } | |
| }, | |
| "Resources": { | |
| "LBWEBVPC": { | |
| "Type": "AWS::EC2::VPC", | |
| "Properties": { | |
| "CidrBlock": "10.0.0.0/16" | |
| } | |
| }, | |
| "InternetGateway": { | |
| "Type": "AWS::EC2::InternetGateway", | |
| "Properties": {} | |
| }, | |
| "VPCGatewayAttachment": { | |
| "Type": "AWS::EC2::VPCGatewayAttachment", | |
| "Properties": { | |
| "InternetGatewayId": { | |
| "Ref": "InternetGateway" | |
| }, | |
| "VpcId": { | |
| "Ref": "LBWEBVPC" | |
| } | |
| } | |
| }, | |
| "EIP1": { | |
| "Type": "AWS::EC2::EIP", | |
| "Properties": {} | |
| }, | |
| "NATGateway1": { | |
| "Type": "AWS::EC2::NatGateway", | |
| "Properties": { | |
| "AllocationId": { | |
| "Fn::GetAtt": [ | |
| "EIP1", | |
| "AllocationId" | |
| ] | |
| }, | |
| "SubnetId": { | |
| "Ref": "Public1Subnet" | |
| } | |
| } | |
| }, | |
| "EIP2": { | |
| "Type": "AWS::EC2::EIP", | |
| "Properties": {} | |
| }, | |
| "NATGateway2": { | |
| "Type": "AWS::EC2::NatGateway", | |
| "Properties": { | |
| "AllocationId": { | |
| "Fn::GetAtt": [ | |
| "EIP2", | |
| "AllocationId" | |
| ] | |
| }, | |
| "SubnetId": { | |
| "Ref": "Public2Subnet" | |
| } | |
| } | |
| }, | |
| "WebServer1RouteTable": { | |
| "Type": "AWS::EC2::RouteTable", | |
| "Properties": { | |
| "VpcId": { | |
| "Ref": "LBWEBVPC" | |
| } | |
| } | |
| }, | |
| "WebServer1Route": { | |
| "Type": "AWS::EC2::Route", | |
| "Properties": { | |
| "RouteTableId": { | |
| "Ref": "WebServer1RouteTable" | |
| }, | |
| "DestinationCidrBlock": "0.0.0.0/0", | |
| "NatGatewayId": { | |
| "Ref": "NATGateway1" | |
| } | |
| } | |
| }, | |
| "WebSubnet1": { | |
| "Type": "AWS::EC2::Subnet", | |
| "Properties": { | |
| "CidrBlock": "10.0.1.0/25", | |
| "AvailabilityZone": { | |
| "Fn::Select" : [ | |
| "0", | |
| { | |
| "Fn::GetAZs" : "" | |
| } | |
| ] | |
| }, | |
| "VpcId": { | |
| "Ref": "LBWEBVPC" | |
| } | |
| } | |
| }, | |
| "WebSubnet1RouteTableAssociation": { | |
| "Type": "AWS::EC2::SubnetRouteTableAssociation", | |
| "Properties": { | |
| "RouteTableId": { | |
| "Ref": "WebServer1RouteTable" | |
| }, | |
| "SubnetId": { | |
| "Ref": "WebSubnet1" | |
| } | |
| } | |
| }, | |
| "WebServer2RouteTable": { | |
| "Type": "AWS::EC2::RouteTable", | |
| "Properties": { | |
| "VpcId": { | |
| "Ref": "LBWEBVPC" | |
| } | |
| } | |
| }, | |
| "WebServer2Route": { | |
| "Type": "AWS::EC2::Route", | |
| "Properties": { | |
| "RouteTableId": { | |
| "Ref": "WebServer2RouteTable" | |
| }, | |
| "DestinationCidrBlock": "0.0.0.0/0", | |
| "NatGatewayId": { | |
| "Ref": "NATGateway2" | |
| } | |
| } | |
| }, | |
| "WebSubnet2": { | |
| "Type": "AWS::EC2::Subnet", | |
| "Properties": { | |
| "CidrBlock": "10.0.1.128/25", | |
| "AvailabilityZone": { | |
| "Fn::Select" : [ | |
| "1", | |
| { | |
| "Fn::GetAZs" : "" | |
| } | |
| ] | |
| }, | |
| "VpcId": { | |
| "Ref": "LBWEBVPC" | |
| } | |
| } | |
| }, | |
| "WebSubnet2RouteTableAssociation": { | |
| "Type": "AWS::EC2::SubnetRouteTableAssociation", | |
| "Properties": { | |
| "RouteTableId": { | |
| "Ref": "WebServer2RouteTable" | |
| }, | |
| "SubnetId": { | |
| "Ref": "WebSubnet2" | |
| } | |
| } | |
| }, | |
| "Public1RouteTable": { | |
| "Type": "AWS::EC2::RouteTable", | |
| "Properties": { | |
| "VpcId": { | |
| "Ref": "LBWEBVPC" | |
| } | |
| } | |
| }, | |
| "Public1Route": { | |
| "Type": "AWS::EC2::Route", | |
| "Properties": { | |
| "RouteTableId": { | |
| "Ref": "Public1RouteTable" | |
| }, | |
| "DestinationCidrBlock": "0.0.0.0/0", | |
| "GatewayId": { | |
| "Ref": "InternetGateway" | |
| } | |
| }, | |
| "DependsOn": [ | |
| "InternetGateway" | |
| ] | |
| }, | |
| "Public1Subnet": { | |
| "Type": "AWS::EC2::Subnet", | |
| "Properties": { | |
| "CidrBlock": "10.0.0.0/25", | |
| "AvailabilityZone": { | |
| "Fn::Select" : [ | |
| "0", | |
| { | |
| "Fn::GetAZs" : "" | |
| } | |
| ] | |
| }, | |
| "VpcId": { | |
| "Ref": "LBWEBVPC" | |
| } | |
| } | |
| }, | |
| "Public1SubnetRouteTableAssociation": { | |
| "Type": "AWS::EC2::SubnetRouteTableAssociation", | |
| "Properties": { | |
| "RouteTableId": { | |
| "Ref": "Public1RouteTable" | |
| }, | |
| "SubnetId": { | |
| "Ref": "Public1Subnet" | |
| } | |
| } | |
| }, | |
| "Public2RouteTable": { | |
| "Type": "AWS::EC2::RouteTable", | |
| "Properties": { | |
| "VpcId": { | |
| "Ref": "LBWEBVPC" | |
| } | |
| } | |
| }, | |
| "Public2Route": { | |
| "Type": "AWS::EC2::Route", | |
| "Properties": { | |
| "RouteTableId": { | |
| "Ref": "Public2RouteTable" | |
| }, | |
| "DestinationCidrBlock": "0.0.0.0/0", | |
| "GatewayId": { | |
| "Ref": "InternetGateway" | |
| } | |
| }, | |
| "DependsOn": [ | |
| "InternetGateway" | |
| ] | |
| }, | |
| "Public2Subnet": { | |
| "Type": "AWS::EC2::Subnet", | |
| "Properties": { | |
| "CidrBlock": "10.0.0.128/25", | |
| "AvailabilityZone": { | |
| "Fn::Select" : [ | |
| "1", | |
| { | |
| "Fn::GetAZs" : "" | |
| } | |
| ] | |
| }, | |
| "VpcId": { | |
| "Ref": "LBWEBVPC" | |
| } | |
| } | |
| }, | |
| "Public2SubnetRouteTableAssociation": { | |
| "Type": "AWS::EC2::SubnetRouteTableAssociation", | |
| "Properties": { | |
| "RouteTableId": { | |
| "Ref": "Public2RouteTable" | |
| }, | |
| "SubnetId": { | |
| "Ref": "Public2Subnet" | |
| } | |
| } | |
| }, | |
| "ELBSecurityGroup": { | |
| "Type": "AWS::EC2::SecurityGroup", | |
| "Properties": { | |
| "GroupDescription": "Enable Http to LB", | |
| "SecurityGroupIngress": [ | |
| { | |
| "IpProtocol": "tcp", | |
| "ToPort": "80", | |
| "FromPort": "80", | |
| "CidrIp": "0.0.0.0/0" | |
| } | |
| ], | |
| "VpcId": { | |
| "Ref": "LBWEBVPC" | |
| } | |
| } | |
| }, | |
| "ElasticLoadBalancer": { | |
| "Type": "AWS::ElasticLoadBalancing::LoadBalancer", | |
| "Properties": { | |
| "Listeners": [ | |
| { | |
| "LoadBalancerPort": "80", | |
| "InstancePort": "80", | |
| "Protocol": "HTTP" | |
| } | |
| ], | |
| "HealthCheck": { | |
| "Target": "HTTP:80/", | |
| "HealthyThreshold": "3", | |
| "UnhealthyThreshold": "5", | |
| "Interval": "30", | |
| "Timeout": "5" | |
| }, | |
| "Subnets": [ | |
| { | |
| "Ref": "Public1Subnet" | |
| }, | |
| { | |
| "Ref": "Public2Subnet" | |
| } | |
| ], | |
| "SecurityGroups": [ | |
| { | |
| "Ref": "ELBSecurityGroup" | |
| } | |
| ] | |
| } | |
| }, | |
| "WebSecurityGroup": { | |
| "Type": "AWS::EC2::SecurityGroup", | |
| "Properties": { | |
| "GroupDescription": "Enable HTTP from the load balancer only", | |
| "SecurityGroupIngress": [ | |
| { | |
| "IpProtocol": "tcp", | |
| "ToPort": "80", | |
| "FromPort": "80", | |
| "SourceSecurityGroupId": { | |
| "Ref": "ELBSecurityGroup" | |
| } | |
| } | |
| ], | |
| "VpcId": { | |
| "Ref": "LBWEBVPC" | |
| } | |
| }, | |
| "DependsOn": [ | |
| "ELBSecurityGroup" | |
| ] | |
| }, | |
| "WebScalingGroup": { | |
| "Type": "AWS::AutoScaling::AutoScalingGroup", | |
| "Properties": { | |
| "LaunchConfigurationName": { | |
| "Ref": "LaunchConfig" | |
| }, | |
| "MinSize": "2", | |
| "MaxSize": "2", | |
| "LoadBalancerNames": [ | |
| { | |
| "Ref": "ElasticLoadBalancer" | |
| } | |
| ], | |
| "VPCZoneIdentifier": [ | |
| { | |
| "Ref": "WebSubnet1" | |
| }, | |
| { | |
| "Ref": "WebSubnet2" | |
| } | |
| ] | |
| }, | |
| "CreationPolicy": { | |
| "ResourceSignal": { | |
| "Timeout": "PT15M", | |
| "Count": "1" | |
| } | |
| }, | |
| "UpdatePolicy": { | |
| "AutoScalingRollingUpdate": { | |
| "MinInstancesInService": "1", | |
| "MaxBatchSize": "1", | |
| "PauseTime": "PT15M", | |
| "WaitOnResourceSignals": "true" | |
| }, | |
| "DependsOn": [ | |
| "NATGateway1", | |
| "NATGateway2" | |
| ] | |
| } | |
| }, | |
| "LaunchConfig": { | |
| "Type": "AWS::AutoScaling::LaunchConfiguration", | |
| "Properties": { | |
| "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "HVM64"]}, | |
| "InstanceType": "t2.small", | |
| "SecurityGroups": [ | |
| { | |
| "Ref": "WebSecurityGroup" | |
| } | |
| ], | |
| "UserData": { | |
| "Fn::Base64": { | |
| "Fn::Join": [ | |
| "", | |
| [ | |
| "#!/bin/bash -xe\n", | |
| "yum update -y aws-cfn-bootstrap\n", | |
| "/opt/aws/bin/cfn-init -v ", | |
| " --stack ", | |
| { | |
| "Ref": "AWS::StackName" | |
| }, | |
| " --resource LaunchConfig ", | |
| " --region ", | |
| { | |
| "Ref": "AWS::Region" | |
| }, | |
| "\n", | |
| "/opt/aws/bin/cfn-signal -e $? ", | |
| " --stack ", | |
| { | |
| "Ref": "AWS::StackName" | |
| }, | |
| " --resource WebScalingGroup ", | |
| " --region ", | |
| { | |
| "Ref": "AWS::Region" | |
| }, | |
| "\n" | |
| ] | |
| ] | |
| } | |
| } | |
| }, | |
| "Metadata": { | |
| "Comment": "Install a simple application", | |
| "AWS::CloudFormation::Init": { | |
| "config": { | |
| "packages": { | |
| "yum": { | |
| "httpd": [] | |
| } | |
| }, | |
| "files": { | |
| "/var/www/html/index.html": { | |
| "content": "<h1>This is the Root Page.</h1>", | |
| "mode": "000644", | |
| "owner": "root", | |
| "group": "root" | |
| }, | |
| "/var/www/html/www/index.html": { | |
| "content": "<h1>Hello Test.</h1>", | |
| "mode": "000644", | |
| "owner": "root", | |
| "group": "root" | |
| }, | |
| "/var/www/html/ww2/index.html": { | |
| "content": "<h1>Hello Test2.</h1>", | |
| "mode": "000644", | |
| "owner": "root", | |
| "group": "root" | |
| }, | |
| "/etc/httpd/conf.d/vhosts.conf": { | |
| "content": { | |
| "Fn::Join": [ | |
| "", | |
| [ | |
| "NameVirtualHost *:80\n", | |
| "<VirtualHost *:80>\n", | |
| "DocumentRoot '/var/www/html'\n", | |
| "ServerName test.com\n", | |
| "</VirtualHost>\n", | |
| "<VirtualHost *:80>\n", | |
| "DocumentRoot '/var/www/html/www'\n", | |
| "ServerName www.test.com\n", | |
| "</VirtualHost>\n", | |
| "<VirtualHost *:80>\n", | |
| "DocumentRoot '/var/www/html/ww2'\n", | |
| "ServerName ww2.test.com\n", | |
| "</VirtualHost>\n" | |
| ] | |
| ] | |
| }, | |
| "mode": "000644", | |
| "owner": "root", | |
| "group": "root" | |
| } | |
| }, | |
| "services": { | |
| "sysvinit": { | |
| "httpd": { | |
| "enabled": "true", | |
| "ensureRunning": "true" | |
| } | |
| } | |
| } | |
| } | |
| } | |
| }, | |
| "DependsOn": [ | |
| "NATGateway1", | |
| "NATGateway2" | |
| ] | |
| } | |
| }, | |
| "Outputs": { | |
| "URL": { | |
| "Description": "The URL of the website", | |
| "Value": { | |
| "Fn::Join": [ | |
| "", | |
| [ | |
| "http://", | |
| { | |
| "Fn::GetAtt": [ | |
| "ElasticLoadBalancer", | |
| "DNSName" | |
| ] | |
| } | |
| ] | |
| ] | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment