About Compute, we have an Auto Scaling Group. Before we can launch an Auto Scaling Group, we need to define our launch template
In the Compute folder, create four files and name them respectively as main.tf, variables.tf, outputs.tf and install_apache_and_stress.sh.
First, define some variable we are going to use in the variables.tf file.
variable "instance_type" {
}
variable "image_id" {
}
variable "keypair_name" {
}
variable "web_server_sg_id" {
}
variable "public_subnet_1_id" {
}
variable "public_subnet_2_id" {
}
variable "alb_tg_arn" {
}
The launch template also runs a script to automatically install an Apache Web Server and stress. We will install the commands to install Apache Web Server and stress in the file install_apache_and_stress.sh
stress is a simple powerful tool designed to impose a configurable amount of CPU, memory, I/O, or disk stress on a Linux system. This tool is valuable for identifying potential weaknesses and ensuring that the system can handle demanding tasks without compromising performance.
#!/bin/bash
sudo yum update -y
sudo yum install -y httpd.x86_64
sudo systemctl start httpd.service
sudo systemctl enable httpd.service
echo "Hello First Cloud Journey Program from $(hostname -f)" > /var/www/html/index.html
sudo amazon-linux-extras install epel -y
sudo yum install stress -y
In the main.tf file, use resources aws_launch_template to create a launch template for Auto Scaling Group.
resource "aws_launch_template" "one_tier_web_server" {
name = "Launch-Template-Web-Server"
instance_type = var.instance_type
image_id = var.image_id
vpc_security_group_ids = [ var.web_server_sg_id ]
key_name = var.keypair_name
user_data = filebase64("${path.module}/install_apache_and_stress.sh")
tags = {
"Name" = "Web Server Launch Template"
}
}
We can create our Auto Scaling Group after we define our launch template.
In the main.tf file, use resources aws_autoscaling_group to create an Auto Scaling Group.
Here, we define the minimum size of the Auto Scaling Group as 2 and the maximum size as 4, which represents the number of EC2 instances in our Auto Scaling Group.
We launch our Auto Scaling Group on 2 public subnets so that we use vpc_zone_identifier to define the subnet IDs to launch resource in.
resource "aws_autoscaling_group" "one_tier_web_server" {
name = "ASG-Web-Server"
min_size = 2
max_size = 4
desired_capacity = 2
vpc_zone_identifier = [ var.public_subnet_1_id, var.public_subnet_2_id ]
target_group_arns = [ var.alb_tg_arn ]
health_check_grace_period = 150
health_check_type = "ELB"
lifecycle {
create_before_destroy = true
}
launch_template {
id = aws_launch_template.one_tier_web_server.id
version = "$Latest"
}
}
Plus, we also need to create Target Tracking Policy to check if the CPU Utilization is above 50 then Auto Scaling Group will automatically scale out our EC2 instance. We can use resources aws_autoscaling_policy to perform this action.
resource "aws_autoscaling_policy" "average_cpu_policy_greater" {
name = "CPUAveragePolicyGreater"
policy_type = "TargetTrackingScaling"
autoscaling_group_name = aws_autoscaling_group.one_tier_web_server.name
# If the CPU Utilization is above 50
target_tracking_configuration {
predefined_metric_specification {
predefined_metric_type = "ASGAverageCPUUtilization"
}
target_value = 50.0
}
}
We only need to output the name of the Auto Scaling Group in the outputs.tf file for further purposes.
output "asg_name" {
value = aws_autoscaling_group.one_tier_web_server.name
}
Next, we will continue with LoadBalancing module.