Skip to content

Instantly share code, notes, and snippets.

@donhenton
Created August 26, 2019 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donhenton/df68f956cd9644672712e83c59b1c1dd to your computer and use it in GitHub Desktop.
Save donhenton/df68f956cd9644672712e83c59b1c1dd to your computer and use it in GitHub Desktop.
AWS CloudFormation YAML for Simple LoadBalancer
---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'AWS CloudFormation Sample Template ELBStickinessSample: Create a load
balanced sample web site with ELB stickiness enabled. The AI is chosen based on
the region in which the stack is run. This example creates 2 EC2 instances behind
a load balancer with a simple health check. The ec2 instnces are untargeted and
may be deployed in one or more availaiblity zones. The web site is available on
port 80, however, the instances can be configured to listen on any port (8888 by
default). **WARNING** This template creates one or more Amazon EC2 instances and
an Application Load Balancer. You will be billed for the AWS resources used if you
create a stack from this template.'
Parameters:
VpcId:
Type: AWS::EC2::VPC::Id
Description: VpcId of your existing Virtual Private Cloud (VPC)
ConstraintDescription: must be the VPC Id of an existing Virtual Private Cloud.
Subnets:
Type: List<AWS::EC2::Subnet::Id>
Description: The list of SubnetIds in your Virtual Private Cloud (VPC)
ConstraintDescription: must be a list of at least two existing subnets associated
with at least two different availability zones. They should be residing in the
selected Virtual Private Cloud.
InstanceType:
Description: WebServer EC2 instance type
Type: String
Default: t2.small
AllowedValues:
- t1.micro
- t2.nano
- t2.micro
- t2.small
- t2.medium
- t2.large
- m1.small
- m1.medium
- m1.large
- m1.xlarge
- m2.xlarge
- m2.2xlarge
- m2.4xlarge
- m3.medium
- m3.large
- m3.xlarge
- m3.2xlarge
- m4.large
- m4.xlarge
- m4.2xlarge
- m4.4xlarge
- m4.10xlarge
- c1.medium
- c1.xlarge
- c3.large
- c3.xlarge
- c3.2xlarge
- c3.4xlarge
- c3.8xlarge
- c4.large
- c4.xlarge
- c4.2xlarge
- c4.4xlarge
- c4.8xlarge
- g2.2xlarge
- g2.8xlarge
- r3.large
- r3.xlarge
- r3.2xlarge
- r3.4xlarge
- r3.8xlarge
- i2.xlarge
- i2.2xlarge
- i2.4xlarge
- i2.8xlarge
- d2.xlarge
- d2.2xlarge
- d2.4xlarge
- d2.8xlarge
- hi1.4xlarge
- hs1.8xlarge
- cr1.8xlarge
- cc2.8xlarge
- cg1.4xlarge
ConstraintDescription: must be a valid EC2 instance type.
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instances
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.
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.
Mappings:
Region2Examples:
us-east-1:
Examples: https://s3.amazonaws.com/cloudformation-examples-us-east-1
us-west-2:
Examples: https://s3-us-west-2.amazonaws.com/cloudformation-examples-us-west-2
us-west-1:
Examples: https://s3-us-west-1.amazonaws.com/cloudformation-examples-us-west-1
eu-west-1:
Examples: https://s3-eu-west-1.amazonaws.com/cloudformation-examples-eu-west-1
eu-west-2:
Examples: https://s3-eu-west-2.amazonaws.com/cloudformation-examples-eu-west-2
eu-west-3:
Examples: https://s3-eu-west-3.amazonaws.com/cloudformation-examples-eu-west-3
eu-north-1:
Examples: https://s3-eu-north-1.amazonaws.com/cloudformation-examples-eu-north-1
eu-central-1:
Examples: https://s3-eu-central-1.amazonaws.com/cloudformation-examples-eu-central-1
ap-southeast-1:
Examples: https://s3-ap-southeast-1.amazonaws.com/cloudformation-examples-ap-southeast-1
ap-northeast-1:
Examples: https://s3-ap-northeast-1.amazonaws.com/cloudformation-examples-ap-northeast-1
ap-northeast-2:
Examples: https://s3-ap-northeast-2.amazonaws.com/cloudformation-examples-ap-northeast-2
ap-northeast-3:
Examples: https://s3-ap-northeast-3.amazonaws.com/cloudformation-examples-ap-northeast-3
ap-southeast-2:
Examples: https://s3-ap-southeast-2.amazonaws.com/cloudformation-examples-ap-southeast-2
ap-south-1:
Examples: https://s3-ap-south-1.amazonaws.com/cloudformation-examples-ap-south-1
us-east-2:
Examples: https://s3-us-east-2.amazonaws.com/cloudformation-examples-us-east-2
ca-central-1:
Examples: https://s3-ca-central-1.amazonaws.com/cloudformation-examples-ca-central-1
sa-east-1:
Examples: https://s3-sa-east-1.amazonaws.com/cloudformation-examples-sa-east-1
cn-north-1:
Examples: https://s3.cn-north-1.amazonaws.com.cn/cloudformation-examples-cn-north-1
cn-northwest-1:
Examples: https://s3.cn-northwest-1.amazonaws.com.cn/cloudformation-examples-cn-northwest-1
AWSInstanceType2Arch:
t1.micro:
Arch: HVM64
t2.nano:
Arch: HVM64
t2.micro:
Arch: HVM64
t2.small:
Arch: HVM64
t2.medium:
Arch: HVM64
t2.large:
Arch: HVM64
m1.small:
Arch: HVM64
m1.medium:
Arch: HVM64
m1.large:
Arch: HVM64
m1.xlarge:
Arch: HVM64
m2.xlarge:
Arch: HVM64
m2.2xlarge:
Arch: HVM64
m2.4xlarge:
Arch: HVM64
m3.medium:
Arch: HVM64
m3.large:
Arch: HVM64
m3.xlarge:
Arch: HVM64
m3.2xlarge:
Arch: HVM64
m4.large:
Arch: HVM64
m4.xlarge:
Arch: HVM64
m4.2xlarge:
Arch: HVM64
m4.4xlarge:
Arch: HVM64
m4.10xlarge:
Arch: HVM64
c1.medium:
Arch: HVM64
c1.xlarge:
Arch: HVM64
c3.large:
Arch: HVM64
c3.xlarge:
Arch: HVM64
c3.2xlarge:
Arch: HVM64
c3.4xlarge:
Arch: HVM64
c3.8xlarge:
Arch: HVM64
c4.large:
Arch: HVM64
c4.xlarge:
Arch: HVM64
c4.2xlarge:
Arch: HVM64
c4.4xlarge:
Arch: HVM64
c4.8xlarge:
Arch: HVM64
g2.2xlarge:
Arch: HVMG2
g2.8xlarge:
Arch: HVMG2
r3.large:
Arch: HVM64
r3.xlarge:
Arch: HVM64
r3.2xlarge:
Arch: HVM64
r3.4xlarge:
Arch: HVM64
r3.8xlarge:
Arch: HVM64
i2.xlarge:
Arch: HVM64
i2.2xlarge:
Arch: HVM64
i2.4xlarge:
Arch: HVM64
i2.8xlarge:
Arch: HVM64
d2.xlarge:
Arch: HVM64
d2.2xlarge:
Arch: HVM64
d2.4xlarge:
Arch: HVM64
d2.8xlarge:
Arch: HVM64
hi1.4xlarge:
Arch: HVM64
hs1.8xlarge:
Arch: HVM64
cr1.8xlarge:
Arch: HVM64
cc2.8xlarge:
Arch: HVM64
AWSInstanceType2NATArch:
t1.micro:
Arch: NATHVM64
t2.nano:
Arch: NATHVM64
t2.micro:
Arch: NATHVM64
t2.small:
Arch: NATHVM64
t2.medium:
Arch: NATHVM64
t2.large:
Arch: NATHVM64
m1.small:
Arch: NATHVM64
m1.medium:
Arch: NATHVM64
m1.large:
Arch: NATHVM64
m1.xlarge:
Arch: NATHVM64
m2.xlarge:
Arch: NATHVM64
m2.2xlarge:
Arch: NATHVM64
m2.4xlarge:
Arch: NATHVM64
m3.medium:
Arch: NATHVM64
m3.large:
Arch: NATHVM64
m3.xlarge:
Arch: NATHVM64
m3.2xlarge:
Arch: NATHVM64
m4.large:
Arch: NATHVM64
m4.xlarge:
Arch: NATHVM64
m4.2xlarge:
Arch: NATHVM64
m4.4xlarge:
Arch: NATHVM64
m4.10xlarge:
Arch: NATHVM64
c1.medium:
Arch: NATHVM64
c1.xlarge:
Arch: NATHVM64
c3.large:
Arch: NATHVM64
c3.xlarge:
Arch: NATHVM64
c3.2xlarge:
Arch: NATHVM64
c3.4xlarge:
Arch: NATHVM64
c3.8xlarge:
Arch: NATHVM64
c4.large:
Arch: NATHVM64
c4.xlarge:
Arch: NATHVM64
c4.2xlarge:
Arch: NATHVM64
c4.4xlarge:
Arch: NATHVM64
c4.8xlarge:
Arch: NATHVM64
g2.2xlarge:
Arch: NATHVMG2
g2.8xlarge:
Arch: NATHVMG2
r3.large:
Arch: NATHVM64
r3.xlarge:
Arch: NATHVM64
r3.2xlarge:
Arch: NATHVM64
r3.4xlarge:
Arch: NATHVM64
r3.8xlarge:
Arch: NATHVM64
i2.xlarge:
Arch: NATHVM64
i2.2xlarge:
Arch: NATHVM64
i2.4xlarge:
Arch: NATHVM64
i2.8xlarge:
Arch: NATHVM64
d2.xlarge:
Arch: NATHVM64
d2.2xlarge:
Arch: NATHVM64
d2.4xlarge:
Arch: NATHVM64
d2.8xlarge:
Arch: NATHVM64
hi1.4xlarge:
Arch: NATHVM64
hs1.8xlarge:
Arch: NATHVM64
cr1.8xlarge:
Arch: NATHVM64
cc2.8xlarge:
Arch: NATHVM64
AWSRegionArch2AMI:
us-east-1:
HVM64: ami-0080e4c5bc078760e
HVMG2: ami-0aeb704d503081ea6
us-west-2:
HVM64: ami-01e24be29428c15b2
HVMG2: ami-0fe84a5b4563d8f27
us-west-1:
HVM64: ami-0ec6517f6edbf8044
HVMG2: ami-0a7fc72dc0e51aa77
eu-west-1:
HVM64: ami-08935252a36e25f85
HVMG2: ami-0d5299b1c6112c3c7
eu-west-2:
HVM64: ami-01419b804382064e4
HVMG2: NOT_SUPPORTED
eu-west-3:
HVM64: ami-0dd7e7ed60da8fb83
HVMG2: NOT_SUPPORTED
eu-central-1:
HVM64: ami-0cfbf4f6db41068ac
HVMG2: ami-0aa1822e3eb913a11
eu-north-1:
HVM64: ami-86fe70f8
HVMG2: ami-32d55b4c
ap-northeast-1:
HVM64: ami-00a5245b4816c38e6
HVMG2: ami-09d0e0e099ecabba2
ap-northeast-2:
HVM64: ami-00dc207f8ba6dc919
HVMG2: NOT_SUPPORTED
ap-northeast-3:
HVM64: ami-0b65f69a5c11f3522
HVMG2: NOT_SUPPORTED
ap-southeast-1:
HVM64: ami-05b3bcf7f311194b3
HVMG2: ami-0e46ce0d6a87dc979
ap-southeast-2:
HVM64: ami-02fd0b06f06d93dfc
HVMG2: ami-0c0ab057a101d8ff2
ap-south-1:
HVM64: ami-0ad42f4f66f6c1cc9
HVMG2: ami-0244c1d42815af84a
us-east-2:
HVM64: ami-0cd3dfa4e37921605
HVMG2: NOT_SUPPORTED
ca-central-1:
HVM64: ami-07423fb63ea0a0930
HVMG2: NOT_SUPPORTED
sa-east-1:
HVM64: ami-05145e0b28ad8e0b2
HVMG2: NOT_SUPPORTED
cn-north-1:
HVM64: ami-053617c9d818c1189
HVMG2: NOT_SUPPORTED
cn-northwest-1:
HVM64: ami-0f7937761741dc640
HVMG2: NOT_SUPPORTED
Resources:
ApplicationLoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Subnets:
Ref: Subnets
Metadata:
AWS::CloudFormation::Designer:
id: 2b7652ad-8781-4f7a-bdb9-897f74f7352a
ALBListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn:
Ref: ALBTargetGroup
LoadBalancerArn:
Ref: ApplicationLoadBalancer
Port: '80'
Protocol: HTTP
Metadata:
AWS::CloudFormation::Designer:
id: 1b15270f-fa10-4a9f-9c47-d2491e8d5123
ALBTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckIntervalSeconds: 30
HealthCheckTimeoutSeconds: 5
HealthyThresholdCount: 3
Port: 80
Protocol: HTTP
UnhealthyThresholdCount: 5
VpcId:
Ref: VpcId
Targets:
- Id:
Ref: EC2Instance1
Port: 80
- Id:
Ref: EC2Instance2
Port: 80
TargetGroupAttributes:
- Key: stickiness.enabled
Value: 'true'
- Key: stickiness.type
Value: lb_cookie
- Key: stickiness.lb_cookie.duration_seconds
Value: '180'
Metadata:
AWS::CloudFormation::Designer:
id: 8dff3331-f6d1-44d3-bf0c-f8644ac71f1e
EC2Instance1:
Type: AWS::EC2::Instance
Metadata:
Comment: Install a simple application
AWS::CloudFormation::Init:
config:
packages:
yum:
httpd: []
files:
"/var/www/html/index.html":
content:
Fn::Join:
- "\n"
- - <img src="
- Fn::FindInMap:
- Region2Examples
- Ref: AWS::Region
- Examples
- /cloudformation_graphic.png" alt="AWS CloudFormation Logo"/>
- "<h1>Congratulations, you have successfully launched the AWS CloudFormation
sample.</h1>"
mode: '000644'
owner: root
group: root
"/etc/cfn/cfn-hup.conf":
content:
Fn::Join:
- ''
- - "[main]\n"
- stack=
- Ref: AWS::StackId
- "\n"
- region=
- Ref: AWS::Region
- "\n"
mode: '000400'
owner: root
group: root
"/etc/cfn/hooks.d/cfn-auto-reloader.conf":
content:
Fn::Join:
- ''
- - "[cfn-auto-reloader-hook]\n"
- 'triggers=post.update
'
- 'path=Resources.WebServerInstance.Metadata.AWS::CloudFormation::Init
'
- 'action=/opt/aws/bin/cfn-init -v '
- " --stack "
- Ref: AWS::StackName
- " --resource EC2Instance1 "
- " --region "
- Ref: AWS::Region
- "\n"
- 'runas=root
'
mode: '000400'
owner: root
group: root
services:
sysvinit:
httpd:
enabled: 'true'
ensureRunning: 'true'
cfn-hup:
enabled: 'true'
ensureRunning: 'true'
files:
- "/etc/cfn/cfn-hup.conf"
- "/etc/cfn/hooks.d/cfn-auto-reloader.conf"
AWS::CloudFormation::Designer:
id: 38651260-3b85-4839-ab16-6bd3bed2f890
Properties:
SecurityGroups:
- Ref: InstanceSecurityGroup
KeyName:
Ref: KeyName
InstanceType:
Ref: InstanceType
ImageId:
Fn::FindInMap:
- AWSRegionArch2AMI
- Ref: AWS::Region
- Fn::FindInMap:
- AWSInstanceType2Arch
- Ref: InstanceType
- Arch
UserData:
Fn::Base64:
Fn::Join:
- ''
- - "#!/bin/bash -xe\n"
- 'yum update -y aws-cfn-bootstrap
'
- "/opt/aws/bin/cfn-init -v "
- " --stack "
- Ref: AWS::StackName
- " --resource EC2Instance1 "
- " --region "
- Ref: AWS::Region
- "\n"
- "/opt/aws/bin/cfn-signal -e $? "
- " --stack "
- Ref: AWS::StackName
- " --resource EC2Instance1 "
- " --region "
- Ref: AWS::Region
- "\n"
CreationPolicy:
ResourceSignal:
Timeout: PT15M
EC2Instance2:
Type: AWS::EC2::Instance
Properties:
SecurityGroups:
- Ref: InstanceSecurityGroup
KeyName:
Ref: KeyName
InstanceType:
Ref: InstanceType
ImageId:
Fn::FindInMap:
- AWSRegionArch2AMI
- Ref: AWS::Region
- Fn::FindInMap:
- AWSInstanceType2Arch
- Ref: InstanceType
- Arch
UserData:
Fn::Base64:
Fn::Join:
- ''
- - "#!/bin/bash -xe\n"
- 'yum update -y aws-cfn-bootstrap
'
- "/opt/aws/bin/cfn-init -v "
- " --stack "
- Ref: AWS::StackName
- " --resource EC2Instance1 "
- " --region "
- Ref: AWS::Region
- "\n"
- "/opt/aws/bin/cfn-signal -e $? "
- " --stack "
- Ref: AWS::StackName
- " --resource EC2Instance2 "
- " --region "
- Ref: AWS::Region
- "\n"
CreationPolicy:
ResourceSignal:
Timeout: PT15M
Metadata:
AWS::CloudFormation::Designer:
id: e011ab0a-d7f6-428c-aaab-880a7c259c41
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access and HTTP access on the inbound port
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp:
Ref: SSHLocation
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
Metadata:
AWS::CloudFormation::Designer:
id: 1be84c38-d163-4a22-bfd4-26df3c660ee9
Outputs:
URL:
Description: URL of the sample website
Value:
Fn::Join:
- ''
- - http://
- Fn::GetAtt:
- ApplicationLoadBalancer
- DNSName
Metadata:
AWS::CloudFormation::Designer:
1be84c38-d163-4a22-bfd4-26df3c660ee9:
size:
width: 60
height: 60
position:
x: 60
y: 90
z: 1
embeds: []
e011ab0a-d7f6-428c-aaab-880a7c259c41:
size:
width: 60
height: 60
position:
x: 180
y: 90
z: 1
embeds: []
isassociatedwith:
- 1be84c38-d163-4a22-bfd4-26df3c660ee9
38651260-3b85-4839-ab16-6bd3bed2f890:
size:
width: 60
height: 60
position:
x: 60
y: 210
z: 1
embeds: []
isassociatedwith:
- 1be84c38-d163-4a22-bfd4-26df3c660ee9
8dff3331-f6d1-44d3-bf0c-f8644ac71f1e:
size:
width: 60
height: 60
position:
x: 180
y: 210
z: 1
embeds: []
2b7652ad-8781-4f7a-bdb9-897f74f7352a:
size:
width: 60
height: 60
position:
x: 300
y: 90
z: 1
embeds: []
1b15270f-fa10-4a9f-9c47-d2491e8d5123:
size:
width: 60
height: 60
position:
x: 300
y: 210
z: 1
embeds: []
isassociatedwith:
- 2b7652ad-8781-4f7a-bdb9-897f74f7352a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment