Skip to content

Instantly share code, notes, and snippets.

@Millward2000
Last active July 25, 2021 18:51
Show Gist options
  • Save Millward2000/3cf94204397ae1836f83788f4efa9066 to your computer and use it in GitHub Desktop.
Save Millward2000/3cf94204397ae1836f83788f4efa9066 to your computer and use it in GitHub Desktop.
sample cloudformation snippet
Parameters:
AvailabilityZones:
Description: 'List of Availability Zones to use for the subnets in the VPC. Note: The logical order is preserved.'
Type: List<AWS::EC2::AvailabilityZone::Name>
KeyName:
Description: The existing AWS Key that you would like to use
Type: AWS::EC2::KeyPair::KeyName
Default: af-south-1
Environment:
Description: Launch a larger instance type if this is Production otherwise use a smaller instance type if preferred
Type: String
AllowedValues: [Production,Test]
Default: Test
ConstraintDescription: Please choose between a Production or Test Environment
Conditions:
Production: !Equals [!Ref Environment, Production]
Mappings:
RegionMap:
eu-west-1:
ami: ami-0ffea00000f287d30
af-south-1:
ami: ami-050312a64b6fd7ad9
Resources:
VPC:
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock: 10.30.0.0/16
InstanceTenancy: default
EnableDnsSupport: true
EnableDnsHostnames: true
IG:
Type: 'AWS::EC2::InternetGateway'
IGATTACH:
Type: 'AWS::EC2::VPCGatewayAttachment'
Properties:
InternetGatewayId: !Ref IG
VpcId: !Ref VPC
RT:
Type: 'AWS::EC2::RouteTable'
Properties:
VpcId: !Ref VPC
ROUTE:
Type: 'AWS::EC2::Route'
Properties:
RouteTableId: !Ref RT
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref IG
SUBNETA:
Type: 'AWS::EC2::Subnet'
Properties:
CidrBlock: 10.30.1.0/24
MapPublicIpOnLaunch: true
VpcId: !Ref VPC
AvailabilityZone: !Select ['0', !Ref 'AvailabilityZones']
SUBNETROUTEA:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
RouteTableId: !Ref RT
SubnetId: !Ref SUBNETA
SUBNETB:
Type: 'AWS::EC2::Subnet'
Properties:
CidrBlock: 10.30.2.0/24
MapPublicIpOnLaunch: true
VpcId: !Ref VPC
AvailabilityZone: !Select ['1', !Ref 'AvailabilityZones']
SUBNETROUTEB:
Type: 'AWS::EC2::SubnetRouteTableAssociation'
Properties:
RouteTableId: !Ref RT
SubnetId: !Ref SUBNETB
SGEC2:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: NGINX-EC2-SG
GroupName: NGINX-EC2-SG
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
Description: Allow HTTP traffic on port 80
FromPort: 80
IpProtocol: tcp
ToPort: 80
- CidrIp: 0.0.0.0/0
Description: Allow SSH Access on port 22
FromPort: 22
IpProtocol: tcp
ToPort: 22
VpcId: !Ref VPC
SGALB:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: DEMO-ALB-SG
GroupName: DEMO-ALB-SG
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
Description: Allow HTTP traffic on port 80
FromPort: 80
IpProtocol: tcp
ToPort: 80
- CidrIp: 0.0.0.0/0
Description: Allow HTTPS traffic on port 443
FromPort: 443
IpProtocol: tcp
ToPort: 443
VpcId: !Ref VPC
EC2INSTANCEA:
Type: AWS::EC2::Instance
Metadata:
'AWS::CloudFormation::Init':
configSets:
nginx-install:
- install-web
- create-index
- start-nginx
install-web:
commands:
install-nginx:
command: amazon-linux-extras install nginx1 -y
create-index:
files:
/usr/share/nginx/html/index.html:
content: !Join
- ''
- - "<html>\n"
- " <head>\n"
- " <title>RED Server</title>\n"
- " </head>\n"
- " <body BGCOLOR='red'>\n"
- " <h1>RED Server</h1>\n"
- " </body>\n"
- "</html>\n"
mode: '000644'
owner: root
group: root
start-nginx:
commands:
start-nginx:
command: systemctl start nginx
Properties:
ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", ami]
InstanceType: !If [Production, t3.large, t3.micro]
KeyName: !Ref KeyName
SubnetId: !Ref SUBNETA
SecurityGroupIds:
- !Ref SGEC2
UserData: !Base64
'Fn::Join':
- ''
- - |
#!/bin/bash -xe
- |
yum update -y aws-cfn-bootstrap
- '/opt/aws/bin/cfn-init -v '
- ' --stack '
- !Ref 'AWS::StackName'
- ' --resource EC2INSTANCEA '
- ' --configsets nginx-install '
- ' --region '
- !Ref 'AWS::Region'
- |+
- '/opt/aws/bin/cfn-signal -e $? '
- ' --stack '
- !Ref 'AWS::StackName'
- ' --resource EC2INSTANCEA '
- ' --region '
- !Ref 'AWS::Region'
- |+
CreationPolicy:
ResourceSignal:
Timeout: PT15M
EC2INSTANCEB:
Type: AWS::EC2::Instance
Metadata:
'AWS::CloudFormation::Init':
configSets:
nginx-install:
- install-web
- create-index
- start-nginx
install-web:
commands:
install-nginx:
command: amazon-linux-extras install nginx1 -y
create-index:
files:
/usr/share/nginx/html/index.html:
content: !Join
- ''
- - "<html>\n"
- " <head>\n"
- " <title>BLUE Server</title>\n"
- " </head>\n"
- " <body BGCOLOR='blue'>\n"
- " <h1>BLUE Server</h1>\n"
- " </body>\n"
- "</html>\n"
mode: '000644'
owner: root
group: root
start-nginx:
commands:
start-nginx:
command: systemctl start nginx
Properties:
ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", ami]
InstanceType: !If [Production, t3.large, t3.micro]
KeyName: !Ref KeyName
SubnetId: !Ref SUBNETB
SecurityGroupIds:
- !Ref SGEC2
UserData: !Base64
'Fn::Join':
- ''
- - |
#!/bin/bash -xe
- |
yum update -y aws-cfn-bootstrap
- '/opt/aws/bin/cfn-init -v '
- ' --stack '
- !Ref 'AWS::StackName'
- ' --resource EC2INSTANCEB '
- ' --configsets nginx-install '
- ' --region '
- !Ref 'AWS::Region'
- |+
- '/opt/aws/bin/cfn-signal -e $? '
- ' --stack '
- !Ref 'AWS::StackName'
- ' --resource EC2INSTANCEB '
- ' --region '
- !Ref 'AWS::Region'
- |+
CreationPolicy:
ResourceSignal:
Timeout: PT15M
Outputs:
RedInstancePublicIP:
Value: !Join
- ''
- - http://
- !GetAtt EC2INSTANCEA.PublicIp
Description: Red Instance Public IP Address
BlueInstancePublicIP:
Value: !Join
- ''
- - http://
- !GetAtt EC2INSTANCEB.PublicIp
Description: Blue Instance Public IP Address
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment