CloudFormation Template-Auto Scaling Group ,EC2 CPU utilization If exceeds 80%
Introduction :-
AWS CloudFormation is a powerful service that allows you to define and provision your #AWS infrastructure as code. With CloudFormation, you can create, update, and manage a collection of AWS resources in a safe and predictable manner. This CloudFormation template showcases the creation of an Auto Scaling Group that dynamically manages #EC2 instances to ensure efficient resource utilization while responding to changes in #CPU utilization. Let’s delve into the details of the template.
#CloudFormation template written in YAML format that creates an AWS #CloudFormation stack with various AWS resources. Let me explain the key parts of the template:
AWSTemplateFormatVersion
: This specifies the #AWS CloudFormation template version.Description
: A brief description of the CloudFormation stack.Resources
: This section defines the AWS resources that will be created when the stack is launched.
MyVPC
: An #Amazon Virtual Private Cloud (VPC) resource with a specific CIDR block and DNS settings.MySubnet
: An EC2 subnet resource that is associated withMyVPC
, with a specific CIDR block and availability zone.MyLaunchTemplate
: An #EC2 launch template resource that defines the configuration for launching instances, including the #Amazon Machine Image (AMI), instance type, and user data.MyAutoScalingGroup
: An Auto Scaling Group resource that launches and manages EC2 instances. It uses the launch template defined inMyLaunchTemplate
and is associated with the VPC and subnet.MetricsCollection
: Specifies which metrics should be collected for the Auto Scaling Group, including CPU utilization.MyScalingPolicy
: An Application Auto Scaling policy that is used to adjust the Auto Scaling Group's desired capacity based on the CPU utilization metric.
4. Outputs
: This section defines the outputs of the CloudFormation stack. In this case, it provides the name of the Auto Scaling Group created in the AutoScalingGroupName
output.
The template essentially sets up an #Auto Scaling Group that maintains a specified number of #EC2 instances based on #CPU utilization. If the average #CPU utilization exceeds 80%, the Auto Scaling Group will scale out (add more instances), and if it falls below 80%, it will scale in (remove instances).
The stack can be launched using #AWS CloudFormation to create the specified resources and configure them based on the defined parameters and properties.Deployment Steps
Deployment Steps :-
Follow these steps to upload and create the CloudFormation stack using the AWS Management Console:
- Sign in to the #AWS Management Console: Log in to your AWS account if you haven’t already.
2. Navigate to #CloudFormation: Go to the #AWS CloudFormation service from the #AWS Management Console.
3. Click the “#Create stack” button.
4. Upload the #CloudFormation template file (YAML).
Yamlfile :
AWSTemplateFormatVersion: "2010-09-09"
Description: Auto Scaling Group with Target Tracking Policy for CPU Utilization and Subnet
Resources:
MyVPC:
Type: "AWS::EC2::VPC"
Properties:
CidrBlock: "10.0.0.0/16"
EnableDnsSupport: true
EnableDnsHostnames: true
MySubnet:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref MyVPC
CidrBlock: "10.0.0.0/24"
AvailabilityZone: "us-east-1a"
MyLaunchTemplate:
Type: "AWS::EC2::LaunchTemplate"
Properties:
LaunchTemplateName: "my-launch-template"
LaunchTemplateData:
ImageId: "ami-04cb4ca688797756f"
InstanceType: "t2.micro"
UserData:
Fn::Base64: !Sub |
#!/bin/bash
echo "Hello, world!"
MyAutoScalingGroup:
Type: "AWS::AutoScaling::AutoScalingGroup"
Properties:
AutoScalingGroupName: "my-auto-scaling-group"
LaunchTemplate:
LaunchTemplateName: !Ref MyLaunchTemplate
Version: !GetAtt MyLaunchTemplate.LatestVersionNumber
MinSize: 2
MaxSize: 5
DesiredCapacity: 2
AvailabilityZones:
- "us-east-1a"
MetricsCollection:
- Granularity: "1Minute"
Metrics:
- "GroupMinSize"
- "GroupMaxSize"
- "GroupDesiredCapacity"
- "GroupInServiceInstances"
- "GroupPendingInstances"
- "GroupTerminatingInstances"
- "GroupTotalInstances"
- "CPUUtilization"
VPCZoneIdentifier:
- !Ref MySubnet
MyScalingPolicy:
Type: "AWS::ApplicationAutoScaling::ScalingPolicy"
Properties:
PolicyName: "MyScalingPolicy"
PolicyType: "TargetTrackingScaling"
ScalingTargetId: !Ref MyAutoScalingGroup
TargetTrackingScalingPolicyConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: "ASGAverageCPUUtilization"
TargetValue: 80.0
Outputs:
AutoScalingGroupName:
Description: "Name of the Auto Scaling Group"
Value: !Ref MyAutoScalingGroup
5. Specify Stack Details:
Enter a #Stack name for your deployment.
Provide parameter values as needed.
Review and acknowledge the capabilities .
You can set additional stack options or tags if necessary.
6. Review and Create:
Review the stack details and configuration.
Click “#Create stack” to initiate the #deployment.
7. Monitor Stack Creation:
The #CloudFormation stack creation process will begin.
Monitor the stack events in the #AWS Management Console.
Conclusion :-
#CloudFormation template demonstrates how #AWS infrastructure can be defined, created, and managed in a declarative and automated way. It sets up a robust environment where an #Auto Scaling Group monitors CPU utilization and automatically scales the number of instances to meet the desired capacity target. This template is just one example of how #CloudFormation empowers users to provision and manage resources efficiently, ensuring #scalability and #cost optimization in #AWS environments. By embracing #infrastructure as code, you can streamline your #AWS deployments and make them more reliable and scalable.