Created
May 20, 2022 15:57
-
-
Save KinzP/23bcd48d4bf11cc49479badc51c2798c to your computer and use it in GitHub Desktop.
Template_CloudFormation
This file contains 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 | |
Resources: | |
# Creating the VPC | |
VPC: | |
Type: 'AWS::EC2::VPC' | |
Properties: | |
CidrBlock: 10.10.0.0/16 | |
EnableDnsSupport: true | |
EnableDnsHostnames: true | |
Tags: | |
- Key: Name | |
Value: !Join | |
- '' | |
- - !Ref 'AWS::StackName' | |
- '-VPC' | |
# Creating an internetGateway | |
InternetGateway: | |
Type: 'AWS::EC2::InternetGateway' | |
DependsOn: VPC | |
# InternetGatewayAttachment to VPC | |
InternetGatewayAttachment: | |
Type: 'AWS::EC2::VPCGatewayAttachment' | |
Properties: | |
VpcId: !Ref VPC | |
InternetGatewayId: !Ref InternetGateway | |
# Create Public Subnet One | |
PublicSubnet1: | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: !Ref VPC | |
AvailabilityZone: !Select [0, !GetAZs ''] | |
CidrBlock: 10.10.0.0/17 | |
MapPublicIpOnLaunch: true | |
# Create Public Subnet Two | |
PublicSubnet2: | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: !Ref VPC | |
AvailabilityZone: !Select [0, !GetAZs ''] | |
CidrBlock: 10.10.128.0/17 | |
MapPublicIpOnLaunch: true | |
# Create Route Table | |
PublicRouteTable: | |
Type: AWS::EC2::RouteTable | |
Properties: | |
VpcId: !Ref VPC | |
# Create PublicRoute | |
PublicRoute: | |
Type: AWS::EC2::Route | |
Properties: | |
RouteTableId: !Ref PublicRouteTable | |
DestinationCidrBlock: 0.0.0.0/0 | |
GatewayId: !Ref InternetGateway | |
# Associate Public Subnet One | |
PublicSubnet1RouteTableAssociation: | |
Type: AWS::EC2::SubnetRouteTableAssociation | |
Properties: | |
RouteTableId: !Ref PublicRouteTable | |
SubnetId: !Ref PublicSubnet1 | |
# Associate Public Subnet Two | |
PublicSubnet2RouteTableAssociation: | |
Type: AWS::EC2::SubnetRouteTableAssociation | |
Properties: | |
RouteTableId: !Ref PublicRouteTable | |
SubnetId: !Ref PublicSubnet2 | |
# Create Security Group | |
InstanceSecurityGroup: | |
Type: 'AWS::EC2::SecurityGroup' | |
Properties: | |
GroupName: SecurityGroup-07 | |
GroupDescription: Open HTTP (port 80) and SSH (port 22) | |
VpcId: !Ref VPC | |
SecurityGroupIngress: | |
- IpProtocol: tcp | |
FromPort: 80 | |
ToPort: 80 | |
CidrIp: 0.0.0.0/0 | |
- IpProtocol: tcp | |
FromPort: 22 | |
ToPort: 22 | |
CidrIp: 0.0.0.0/0 | |
# Create Launch Template | |
LaunchTemplate: | |
Type: 'AWS::EC2::LaunchTemplate' | |
Properties: | |
LaunchTemplateName: !Sub '${AWS::StackName}-launchTemplate4ASG' | |
LaunchTemplateData: | |
NetworkInterfaces: | |
- DeviceIndex: 0 | |
AssociatePublicIpAddress: true | |
DeleteOnTermination: true | |
Groups: | |
- !Ref InstanceSecurityGroup | |
ImageId: ami-0022f774911c1d690 | |
InstanceType: t2.micro | |
UserData: | |
Fn::Base64: !Sub | | |
#!/bin/bash | |
yum update -y | |
yum install httpd -y | |
systemctl start httpd | |
systemctl enable httpd | |
amazon-linux-extras install epel -y | |
yum install stress -y | |
#SecurityGroupIds: | |
# - !Ref InstanceSecurityGroup | |
# Create AutoScaling Group | |
AutoScalingGroup: | |
Type: 'AWS::AutoScaling::AutoScalingGroup' | |
Properties: | |
LaunchTemplate: | |
LaunchTemplateId: !Ref LaunchTemplate | |
Version: !GetAtt LaunchTemplate.LatestVersionNumber | |
MaxSize: '5' | |
MinSize: '2' | |
DesiredCapacity: '2' | |
VPCZoneIdentifier: | |
- !Ref PublicSubnet1 | |
- !Ref PublicSubnet2 | |
MetricsCollection: | |
- Granularity: 1Minute | |
# Create a Scaling Policy | |
ScalingPolicy07: | |
Type: 'AWS::AutoScaling::ScalingPolicy' | |
Properties: | |
AdjustmentType: ChangeInCapacity | |
AutoScalingGroupName: !Ref AutoScalingGroup | |
ScalingAdjustment: '1' | |
# CPU Threshold Alarm 50% | |
CloudWatchAlarm: | |
Type: 'AWS::CloudWatch::Alarm' | |
Properties: | |
EvaluationPeriods: '1' | |
Statistic: Average | |
Threshold: '50' | |
AlarmDescription: Alarm if CPU higher than 50% | |
Period: '60' | |
AlarmActions: | |
- !Ref ScalingPolicy07 | |
Namespace: AWS/EC2 | |
Dimensions: | |
- Name: AutoScalingGroupName | |
Value: | |
Ref: AutoScalingGroup | |
ComparisonOperator: GreaterThanThreshold | |
MetricName: CPUUtilization |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment