Skip to content

Instantly share code, notes, and snippets.

@desiby
Created February 15, 2021 15:59
Show Gist options
  • Save desiby/533a017d71803eeccadbc0fb21877b29 to your computer and use it in GitHub Desktop.
Save desiby/533a017d71803eeccadbc0fb21877b29 to your computer and use it in GitHub Desktop.
scalable web server
AWSTemplateFormatVersion: 2010-09-09
Description: Sample Nginx scalable webserver
Parameters:
VpcCidrBlock:
Description: VPC cidr block
Type: String
Default: 192.168.0.0/20
InstanceType :
Description : WebServer EC2 instance type
Type : String
Default : t2.large
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
- hs1.8xlarge
- cr1.8xlarge
- cc2.8xlarge
ConstraintDescription : must be a valid EC2 instance type.
Mappings:
RegionMap:
us-east-1:
HVM64: ami-047a51fa27710816e
us-west-1:
HVM64: ami-005c06c6de69aee84
us-east-2:
HVM64: ami-01aab85a5e4a5a0fe
us-west-2:
HVM64: ami-0e999cbd62129e3b1
Resources:
#vpc
SampleVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCidrBlock
InstanceTenancy: default
Tags:
- Key: name
Value: pipeline-factory
#public subnet
SamplePublicSubnet1:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone:
Fn::Select:
- 0
- Fn::GetAZs: ""
VpcId: !Ref SampleVPC
CidrBlock: 192.168.0.0/24
Tags:
- Key: name
Value: pipeline-factory
SamplePublicSubnet2:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone:
Fn::Select:
- 1
- Fn::GetAZs: ""
VpcId: !Ref SampleVPC
CidrBlock: 192.168.1.0/24
Tags:
- Key: name
Value: pipeline-factory
#internet Gateway
SampleInternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: name
Value: pipeline-factory
#Internet Gateway attachement
InternetGatewayAttachement:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref SampleInternetGateway
VpcId: !Ref SampleVPC
#route table
SampleRouteTable:
Type: AWS::EC2::RouteTable
Properties:
Tags:
- Key: name
Value: pipeline-factory
VpcId: !Ref SampleVPC
#subnet route table associations(public subnet)
PBSubnet1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref SampleRouteTable
SubnetId: !Ref SamplePublicSubnet1
PBSubnet2RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref SampleRouteTable
SubnetId: !Ref SamplePublicSubnet2
#routes
#internet gateway route
SampleRoute:
Type: AWS::EC2::Route
Properties:
GatewayId: !Ref SampleInternetGateway
RouteTableId: !Ref SampleRouteTable
DestinationCidrBlock: 0.0.0.0/0
#security group
SampleSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow HTTP trafic
SecurityGroupIngress:
- Description: Allow HTTP traffic from everywhere
CidrIp: 0.0.0.0/0
IpProtocol: tcp
FromPort: 80
ToPort: 80
VpcId: !Ref SampleVPC
#auto scaling group
WebServerAutoScalingGroup:
Type: 'AWS::AutoScaling::AutoScalingGroup'
Properties:
AutoScalingGroupName: 'AutomationBoxes'
VPCZoneIdentifier:
- !Ref SamplePublicSubnet1
- !Ref SamplePublicSubnet2
DesiredCapacity: '3'
HealthCheckType: 'ELB'
HealthCheckGracePeriod: 30
LaunchConfigurationName: !Ref WebServersLaunchConfiguration
MaxSize: '3'
MinSize: '3'
TargetGroupARNs:
- !Ref MyApplicationLoadBalancerTargetGrp
#load balancer target group
MyApplicationLoadBalancerTargetGrp:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckEnabled: true
HealthCheckIntervalSeconds: 15
HealthCheckPath: /index.html
HealthCheckPort: traffic-port
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 6
HealthyThresholdCount: 5
Name: MyalbTargetGroup
Port: 80
Protocol: HTTP
Tags:
- Key: name
Value: pipeline-factory
TargetType: instance
UnhealthyThresholdCount: 2
VpcId: !Ref SampleVPC
#application load balancer
MyAppLoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
IpAddressType: ipv4
Scheme: internet-facing
SecurityGroups:
- !GetAtt SampleSecurityGroup.GroupId
Subnets:
- !Ref SamplePublicSubnet1
- !Ref SamplePublicSubnet2
Tags:
- Key: name
Value: pipeline-factory
Type: application
#load balancing listener
MyAlbListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- TargetGroupArn: !Ref MyApplicationLoadBalancerTargetGrp
Type: forward
LoadBalancerArn: !Ref MyAppLoadBalancer
Port: 80
Protocol: HTTP
#auto scaling launch config
WebServersLaunchConfiguration:
Type: 'AWS::AutoScaling::LaunchConfiguration'
Properties:
ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", HVM64]
InstanceType: !Ref InstanceType
#KeyName: dez.pem
LaunchConfigurationName: NginxWebServerLaunchConfig
SecurityGroups:
- !GetAtt SampleSecurityGroup.GroupId
UserData:
Fn::Base64:
Fn::Sub: |
#!/bin/bash -xe
yum install -y aws-cfn-bootstrap
# Install the files and packages from the metadata
/opt/aws/bin/cfn-init -v \
--stack ${AWS::StackName} \
--resource WebServersLaunchConfiguration \
--configsets All \
--region ${AWS::Region}
# Signal the status from cfn-init
/opt/aws/bin/cfn-signal -e $? \
--stack ${AWS::StackName} \
--resource WebServersLaunchConfiguration \
--region ${AWS::Region}
Metadata:
'AWS::CloudFormation::Init':
configSets:
All:
- ConfigureStelligentProject
ConfigureStelligentProject:
packages:
yum:
nginx: []
files:
/usr/share/nginx/html/index.html:
content: '<p>Automation for the People</p>'
mode: '000644'
owner: root
group: root
services:
sysvinit:
nginx:
enabled: 'true'
ensureRunning: 'true'
Outputs:
LoadBalancerDNSName:
Description: load balancer DNS endpoint
Value: !GetAtt MyAppLoadBalancer.DNSName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment