{"id":938,"date":"2023-08-07T16:43:34","date_gmt":"2023-08-07T16:43:34","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=938"},"modified":"2023-08-07T16:43:34","modified_gmt":"2023-08-07T16:43:34","slug":"terraform-autoscaling-group","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=938","title":{"rendered":"AWS Terraform Autoscaling Group With ALB [Tutorial]"},"content":{"rendered":"<p>In this blog, you will learn how to deploy a Terraform autoscaling group with an application load balancer using step-by-step guides<\/p>\n<p>We are going to build the following in this guide.<\/p>\n<ol>\n<li>AWS Autoscaling group spanning three subnets.<\/li>\n<li>IAM role attached to Autoscaling instances to access other AWS services<\/li>\n<li>Application Load Balancer attached to the Autoscaling group<\/li>\n<\/ol>\n<p>Throughout this article, we will be using the following short names.<\/p>\n<ol>\n<li><strong>ALB<\/strong> &#8211; Application load balancer<\/li>\n<li><strong>ASG<\/strong> &#8211; Autoscaling Group<\/li>\n<\/ol>\n<blockquote><p><strong>Note:<\/strong> If you are not aware of <a href=\"https:\/\/devopscube.com\/aws-load-balancers\/\" rel=\"noreferrer noopener\">AWS Load Balancer <\/a>and Autoscaling Group concepts, we suggest you understand it before following this setup.<\/p><\/blockquote>\n<h2 id=\"prerequisites\">Prerequisites<\/h2>\n<p>To follow this guide you need to have the following.<\/p>\n<ol>\n<li>The latest Terraform binary is installed and configured in your system.<\/li>\n<li><a href=\"https:\/\/devopscube.com\/install-configure-aws-cli-linux\/\" rel=\"noreferrer noopener\">AWS CLI<\/a> is installed and configured with a valid AWS account with permission to deploy the autoscaling group and application load balancer.<\/li>\n<li>If you are using an ec2 instance to run Terraform, ensure you attach an <a href=\"https:\/\/devopscube.com\/terraform-iam-role\/\">IAM role<\/a> with permission to create ASG and ALB.<\/li>\n<\/ol>\n<h2 id=\"setup-architecture-overview\">Setup Architecture &amp; Overview<\/h2>\n<p>Here is the high-level architecture of the setup we are going to create.<\/p>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/lb-asg-workflow-1.gif\" class=\"kg-image\" alt=\"Terraform Autoscaling Group with application load balancer\" loading=\"lazy\" width=\"1080\" height=\"1350\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/lb-asg-workflow-1.gif 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/lb-asg-workflow-1.gif 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/lb-asg-workflow-1.gif 1080w\" sizes=\"auto, (min-width: 720px) 720px\"><figcaption><span style=\"white-space: pre-wrap;\">Click to View in HD<\/span><\/figcaption><\/figure>\n<p>Here is the high-level overview of the AWS resources and components created by this setup.<\/p>\n<ol>\n<li><strong>IAM role<\/strong> with required policies and the role is attached to an <a href=\"https:\/\/devopscube.com\/aws-iam-role-instance-profile\/\">IAM instance profile<\/a> which will be then attached to every instance that is part of the autoscaling group.<\/li>\n<li>The <strong>auto-scaling group<\/strong> manages a specified number of instances and uses the <strong>launch template<\/strong> with the required configurations to launch an instance.<\/li>\n<li>Application load balancers send traffic to the ASG instances. It creates a <strong>target group<\/strong> and creates an <strong>LB listener<\/strong> that listens to port 80 for HTTP traffic and forwards it to the specified <strong>target group(ASG)<\/strong> to distribute traffic.<\/li>\n<li><strong>Health checks<\/strong> are added to instances in the target group to check the status of the instance. If the instance health check fails,  it destroys the instance and launches a new instance. Once the new instance is in a healthy state, the application load balancer will then forward the traffic to the newly launched instance.<\/li>\n<\/ol>\n<p>We have separate <strong>security groups<\/strong> for ALB and ASG EC2 instances. For ASG, traffic on <strong>port 8080<\/strong> will be accepted only from the ALB. We achieve this by adding the <strong>security group ID of the ALB as the source<\/strong> traffic for the ASG security group.  Also, we allow port 22 access only from a specific subnet.<\/p>\n<p>Here is a high-level view of how ALB and ASG security groups are designed.<\/p>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/asg-sg-workflow-1.gif\" class=\"kg-image\" alt=\"AWS ALB , ASG recommended security group workflow.\" loading=\"lazy\" width=\"666\" height=\"614\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/asg-sg-workflow-1.gif 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/asg-sg-workflow-1.gif 666w\"><figcaption><span style=\"white-space: pre-wrap;\">Click to View in HD<\/span><\/figcaption><\/figure>\n<h2 id=\"terraform-alb-and-asg-code-repository\">Terraform ALB and ASG Code Repository<\/h2>\n<p>ALB and ASG terraform code is a part of the <a href=\"https:\/\/github.com\/techiescamp\/terraform-aws.git?ref=devopscube.com\" rel=\"noreferrer noopener\">terraform AWS<\/a> repository. Clone it to your workstation to follow the guide.<\/p>\n<pre><code>git clone https:\/\/github.com\/techiescamp\/terraform-aws.git<\/code><\/pre>\n<p>Fork and clone the repository if you intend to reuse and make changes as per your requirements.<\/p>\n<blockquote><p><strong>Note:<\/strong> When using Terraform in production, it has to go through the infra-CI review process using tools like Tflint, Terratest, Checkov, etc.<\/p><\/blockquote>\n<h2 id=\"terraform-aws-alb-and-asg-provisioning-workflow\">Terraform AWS ALB and ASG Provisioning Workflow<\/h2>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/alb-asg-workflow-1.gif\" class=\"kg-image\" alt=\"Terraform AWS LB and ASG Provisioning Workflow\" loading=\"lazy\" width=\"1280\" height=\"800\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/alb-asg-workflow-1.gif 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/alb-asg-workflow-1.gif 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/alb-asg-workflow-1.gif 1280w\" sizes=\"auto, (min-width: 720px) 720px\"><figcaption><span style=\"white-space: pre-wrap;\">Click to View in HD<\/span><\/figcaption><\/figure>\n<p>The ALB and ASG terraform script is structured in the following way.<\/p>\n<pre><code>\u251c\u2500\u2500 apps\n\u2502   \u251c\u2500\u2500 alb-asg\n\u2502   \u2502   \u251c\u2500\u2500 main.tf\n\u2502   \u2502   \u251c\u2500\u2500 outputs.tf\n\u2502   \u2502   \u2514\u2500\u2500 variables.tf\n\u251c\u2500\u2500 infra\n\u2502   \u2514\u2500\u2500 iam-policies\n\u2502       \u2514\u2500\u2500 alb-asg.json\n\u251c\u2500\u2500 modules\n\u2502   \u251c\u2500\u2500 asg\n\u2502   \u2502   \u251c\u2500\u2500 main.tf\n\u2502   \u2502   \u251c\u2500\u2500 outputs.tf\n\u2502   \u2502   \u2514\u2500\u2500 variables.tf\n\u2502   \u251c\u2500\u2500 iam-policy\n\u2502   \u2502   \u251c\u2500\u2500 main.tf\n\u2502   \u2502   \u251c\u2500\u2500 outputs.tf\n\u2502   \u2502   \u2514\u2500\u2500 variables.tf\n\u2502   \u251c\u2500\u2500 alb\n\u2502   \u2502   \u251c\u2500\u2500 main.tf\n\u2502   \u2502   \u251c\u2500\u2500 outputs.tf\n\u2502   \u2502   \u2514\u2500\u2500 variables.tf\n\u2502   \u2514\u2500\u2500 security-group\n\u2502       \u251c\u2500\u2500 main.tf\n\u2502       \u251c\u2500\u2500 outputs.tf\n\u2502       \u2514\u2500\u2500 variables.tf\n\u2514\u2500\u2500 vars\n    \u2514\u2500\u2500 dev\n        \u2514\u2500\u2500 alb-asg.tfvars<\/code><\/pre>\n<p><code><strong>vars<\/strong><\/code> folder contains the variables file named <code>alb-asg.tfvars<\/code><\/p>\n<p><code><strong>appsalb-asg<\/strong><\/code> folder contains the parent terraform module (<code>main.tf<\/code>) that calls the child modules under the <strong><code>modules<\/code><\/strong> folder<\/p>\n<p><strong><code>infra\/iam-policies<\/code><\/strong> contains the IAM JSON policy document named <strong><code>alb-asg.json<\/code><\/strong> that will be added to the Instance Profile.<\/p>\n<p>The child modules contain the following resources<\/p>\n<ol>\n<li><strong>IAM Role:<\/strong> For ec2 instance in the autoscaling group to access other AWS services.<\/li>\n<li><strong>Security Group: <\/strong>To allow &amp; deny access to\/from the load balancer and ec2 instance.<\/li>\n<li><strong>Load Balancer:<\/strong> It distributes incoming traffic to EC2 instances using the <strong>Round Robin <\/strong>algorithm.<\/li>\n<li><strong>Target Group:<\/strong> To evenly distribute traffic throughout a collection of EC2 instances.<\/li>\n<li><strong>Listener: <\/strong>It monitors incoming requests on a certain port and notifies the target group.<\/li>\n<li><strong>Auto Scaling Group:<\/strong> It automatically scales EC2 instances based on demand, which maintains application availability and also keeps track of instance health, and replaces failing instances.<\/li>\n<li><strong>Launch Template:<\/strong> A template that contains the AMI  details, keypair, etc. This template will be applied to the autoscaling group instances.<\/li>\n<\/ol>\n<h2 id=\"application-ami\">Application AMI<\/h2>\n<p>For this demo, we will be using a Java application AMI that runs on port 8080. You need an AMI with some application running to deploy it in the autoscaling group.<\/p>\n<p>If you want to use the same AMI we have used in this guide, you can use the AMI id <strong><code>ami-020f3ca563c92097b<\/code><\/strong> in the us-west-2 region that we have made public.<\/p>\n<p>If you want to create the same AMI, you can refer to the <a href=\"https:\/\/devopscube.com\/build-aws-java-application-ami-packer\/\">Build Java Application AMI<\/a> blog for detailed steps to build the AMI using Packer.<\/p>\n<h2 id=\"provisioning-asg-and-alb-using-terraform\">Provisioning ASG and ALB Using Terraform<\/h2>\n<p>This demo is based on the following values<\/p>\n<ol>\n<li><strong>Region<\/strong>: us-west-2<\/li>\n<li><strong>Public AMI ID<\/strong> (Java Application): <code>ami-020f3ca563c92097b<\/code><\/li>\n<\/ol>\n<p>Follows the steps given below to provision the autoscaling group with an application load balancer.<\/p>\n<h3 id=\"step-1-modify-the-alb-and-asg-variables\">Step 1: Modify the ALB and ASG variables<\/h3>\n<p>Open the <strong><code>alb-asg.tfvars<\/code><\/strong> file present in the <strong>vars\/dev<\/strong> folder.<\/p>\n<p>You need to modify the variables marked in bold as per your requirements.<\/p>\n<pre><code>region                           = \"us-west-2\"\n\n# alb\ninternal                       = false\nloadbalancer_type              = \"application\"\nalb_subnets                    = [\"subnet-058a7514ba8adbb07\", \"subnet-0dbcd1ac168414927\", \"subnet-032f5077729435858\"]\n\n#alb-sg\nalb_ingress_cidr_from_port     = [80]\nalb_ingress_cidr_to_port       = [80]\nalb_ingress_cidr_protocol      = [\"tcp\"]\nalb_ingress_cidr_block         = [\"0.0.0.0\/0\"]\nalb_create_ingress_cidr        = true\n\nalb_ingress_sg_from_port       = [8080]\nalb_ingress_sg_to_port         = [8080]\nalb_ingress_sg_protocol        = [\"tcp\"]\nalb_create_ingress_sg          = false\n\nalb_egress_cidr_from_port      = [0]\nalb_egress_cidr_to_port        = [0]\nalb_egress_cidr_protocol       = [\"-1\"]\nalb_egress_cidr_block          = [\"0.0.0.0\/0\"]\nalb_create_egress_cidr         = true\n\nalb_egress_sg_from_port        = [0]\nalb_egress_sg_to_port          = [0]\nalb_egress_sg_protocol         = [\"-1\"]\nalb_create_egress_sg           = false\n\n# instance sg\ningress_cidr_from_port         = [22]\ningress_cidr_to_port           = [22]\ningress_cidr_protocol          = [\"tcp\"]\ningress_cidr_block             = [\"0.0.0.0\/0\"]\ncreate_ingress_cidr            = true\n\ningress_sg_from_port           = [8080]\ningress_sg_to_port             = [8080]\ningress_sg_protocol            = [\"tcp\"]\ncreate_ingress_sg              = true\n\negress_cidr_from_port          = [0]\negress_cidr_to_port            = [0]\negress_cidr_protocol           = [\"-1\"]\negress_cidr_block              = [\"0.0.0.0\/0\"]\ncreate_egress_cidr             = true\n\negress_sg_from_port            = [8080]\negress_sg_to_port              = [8080]\negress_sg_protocol             = [\"tcp\"]\ncreate_egress_sg               = false\n\n# target_group\ntarget_group_port              = 8080\ntarget_group_protocol          = \"HTTP\"\ntarget_type                    = \"instance\"\nload_balancing_algorithm       = \"round_robin\"\n\n# health_check\nhealth_check_path               = \"\/\"\nhealth_check_port               = 8080\nhealth_check_protocol           = \"HTTP\"\nhealth_check_interval           = 30\nhealth_check_timeout            = 5\nhealth_check_healthy_treshold   = 2\nhealth_check_unhealthy_treshold = 2\n\n#alb_listener\nlistener_port                   = 80\nlistener_protocol               = \"HTTP\"\nlistener_type                   = \"forward\"\n\n#launch_template\nami_id                           = \"ami-020f3ca563c92097b\"\ninstance_type                    = \"t2.medium\"\nkey_name                         = \"techiescamp\"\nvpc_id                           = \"vpc-0a5ca4a92c2e10163\"\nasg_subnets                      = [\"subnet-058a7514ba8adbb07\", \"subnet-0dbcd1ac168414927\", \"subnet-032f5077729435858\"]\npublic_access                    = true\n\n#user_data\nuser_data                        = &lt;&lt;-EOF\n                                    #!\/bin\/bash\n                                    bash \/home\/ubuntu\/start.sh\n                                   EOF\n\n#autoscaling_group\nmax_size                         = 2\nmin_size                         = 1\ndesired_capacity                 = 1\npropagate_at_launch              = true\ninstance_warmup_time             = 30\ntarget_value                     = 50\n\n#tags\nowner                            = \"techiescamp\"\nenvironment                      = \"dev\"\ncost_center                      = \"techiescamp-commerce\"\napplication                      = \"java-app\"\n<\/code><\/pre>\n<h3 id=\"step-2-initialize-terraform\">Step 2: Initialize terraform<\/h3>\n<p>Once the variables are modified as per your requirements, cd into <strong><code>apps\/alb-asg<\/code><\/strong> directory.<\/p>\n<pre><code>cd apps\/alb-asg<\/code><\/pre>\n<p>Inside the <strong>alb-asg<\/strong> folder, you can find the <strong>main.tf<\/strong> parent module where it calls the load balancer, auto-scaling group, and IAM policy child modules present in the modules directory as shown below.<\/p>\n<pre><code>provider \"aws\" {\n  region = var.region\n}\n\nmodule \"iam-policy\" {\n  source                          = \"..\/..\/..\/modules\/iam-policy\"\n  owner                           = var.owner\n  environment                     = var.environment\n  cost_center                     = var.cost_center\n  application                     = var.application\n}\n\nmodule \"alb-sg\" {\n  source                          = \"..\/..\/..\/modules\/security-group\"\n  region                          = var.region\n  tags                            = var.tags\n  name                            = \"${var.environment}-${var.application}\"\n  environment                     = var.environment\n  owner                           = var.owner\n  cost_center                     = var.cost_center\n  application                     = \"${var.application}-alb\"\n  vpc_id                          = var.vpc_id\n\n  ingress_cidr_from_port          = var.alb_ingress_cidr_from_port\n  ingress_cidr_to_port            = var.alb_ingress_cidr_to_port\n  ingress_cidr_protocol           = var.ingress_cidr_protocol\n  ingress_cidr_block              = var.alb_ingress_cidr_block\n  create_ingress_cidr             = var.alb_create_ingress_cidr\n\n  ingress_sg_from_port            = var.alb_ingress_sg_from_port\n  ingress_sg_to_port              = var.alb_ingress_sg_to_port\n  ingress_sg_protocol             = var.alb_ingress_sg_protocol\n  ingress_security_group_ids      = var.ingress_security_group_ids\n  create_ingress_sg               = var.alb_create_ingress_sg\n\n  egress_cidr_from_port           = var.alb_egress_cidr_from_port\n  egress_cidr_to_port             = var.alb_egress_cidr_to_port\n  egress_cidr_protocol            = var.alb_egress_cidr_protocol\n  egress_cidr_block               = var.alb_egress_cidr_block\n  create_egress_cidr              = var.alb_create_egress_cidr\n\n  egress_sg_from_port             = var.alb_egress_sg_from_port\n  egress_sg_to_port               = var.alb_egress_sg_to_port\n  egress_sg_protocol              = var.alb_egress_sg_protocol\n  egress_security_group_ids       = var.egress_security_group_ids\n  create_egress_sg                = var.alb_create_egress_sg\n}\n\nmodule \"alb\" {\n  source                          = \"..\/..\/..\/modules\/alb\"\n  region                          = var.region\n  internal                        = var.internal\n  loadbalancer_type               = var.loadbalancer_type\n  vpc_id                          = var.vpc_id\n  alb_subnets                     = var.alb_subnets\n  target_group_port               = var.target_group_port\n  target_group_protocol           = var.target_group_protocol\n  target_type                     = var.target_type\n  load_balancing_algorithm        = var.load_balancing_algorithm\n  health_check_path               = var.health_check_path\n  health_check_port               = var.health_check_port\n  health_check_protocol           = var.health_check_protocol\n  health_check_interval           = var.health_check_interval\n  health_check_timeout            = var.health_check_timeout\n  health_check_healthy_treshold   = var.health_check_healthy_treshold\n  health_check_unhealthy_treshold = var.health_check_unhealthy_treshold\n  listener_port                   = var.listener_port\n  listener_protocol               = var.listener_protocol\n  listener_type                   = var.listener_type\n  owner                           = var.owner\n  environment                     = var.environment\n  cost_center                     = var.cost_center\n  application                     = var.application\n  security_group_ids              = module.alb-sg.security_group_ids\n}\n\nmodule \"instance-sg\" {\n  source                          = \"..\/..\/..\/modules\/security-group\"\n  region                          = var.region\n  tags                            = var.tags\n  name                            = \"${var.environment}-${var.application}\"\n  environment                     = var.environment\n  owner                           = var.owner\n  cost_center                     = var.cost_center\n  application                     = var.application\n  vpc_id                          = var.vpc_id\n\n  ingress_cidr_from_port          = var.ingress_cidr_from_port\n  ingress_cidr_to_port            = var.ingress_cidr_to_port\n  ingress_cidr_protocol           = var.ingress_cidr_protocol\n  ingress_cidr_block              = var.ingress_cidr_block\n  create_ingress_cidr             = var.create_ingress_cidr\n\n  ingress_sg_from_port            = var.ingress_sg_from_port\n  ingress_sg_to_port              = var.ingress_sg_to_port\n  ingress_sg_protocol             = var.ingress_sg_protocol\n  ingress_security_group_ids      = module.alb-sg.security_group_ids\n  create_ingress_sg               = var.create_ingress_sg\n\n  egress_cidr_from_port           = var.egress_cidr_from_port\n  egress_cidr_to_port             = var.egress_cidr_to_port\n  egress_cidr_protocol            = var.egress_cidr_protocol\n  egress_cidr_block               = var.egress_cidr_block\n  create_egress_cidr              = var.create_egress_cidr\n\n  egress_sg_from_port             = var.egress_sg_from_port\n  egress_sg_to_port               = var.egress_sg_to_port\n  egress_sg_protocol              = var.egress_sg_protocol\n  egress_security_group_ids       = module.alb-sg.security_group_ids\n  create_egress_sg                = var.create_egress_sg\n}\n\nmodule \"asg\" {\n  source                          = \"..\/..\/..\/modules\/asg\"\n  ami_id                          = var.ami_id\n  instance_type                   = var.instance_type\n  key_name                        = var.key_name\n  vpc_id                          = var.vpc_id\n  asg_subnets                     = var.asg_subnets\n  public_access                   = var.public_access\n  user_data                       = var.user_data\n  max_size                        = var.max_size\n  min_size                        = var.min_size\n  desired_capacity                = var.desired_capacity\n  propagate_at_launch             = var.propagate_at_launch\n  owner                           = var.owner\n  environment                     = var.environment\n  cost_center                     = var.cost_center\n  application                     = var.application\n  instance_warmup_time            = var.instance_warmup_time\n  target_value                    = var.target_value\n  alb_target_group_arn            = module.alb.alb_target_group_arn\n  iam_role                        = module.iam-policy.iam_role\n  security_group_ids              = module.instance-sg.security_group_ids\n  tags = {\n    Owner                         = \"${var.owner}\"\n    Environment                   = \"${var.environment}\"\n    Cost_center                   = \"${var.cost_center}\"\n    Application                   = \"${var.application}\"\n  }\n}\n\n<\/code><\/pre>\n<p>Initialize Terraform using the following command<\/p>\n<pre><code>terraform init<\/code><\/pre>\n<p>This command initializes terraform. Make sure to run the init command inside the <strong>environments\/dev\/alb-asg<\/strong> directory.<\/p>\n<h3 id=\"step-3-validate-configurations\">Step 3: Validate Configurations<\/h3>\n<p>Validate terraform configs using the validate command.<\/p>\n<pre><code>terraform validate<\/code><\/pre>\n<h3 id=\"step-4-execute-the-configuration-plan\">Step 4: Execute the configuration plan<\/h3>\n<p>To verify the configurations, run terraform plan with the variable file.<\/p>\n<pre><code>terraform plan -var-file=..\/..\/..\/vars\/dev\/alb-asg.tfvars<\/code><\/pre>\n<h3 id=\"step-5-apply-the-configuration\"><strong>Step 5:<\/strong> Apply the configuration<\/h3>\n<p>After verifying, apply the configurations using the command given below.<\/p>\n<pre><code>terraform apply -var-file=..\/..\/..\/vars\/dev\/alb-asg.tfvars --auto-approve<\/code><\/pre>\n<p>Once the code is successfully executed, check if everything in the Terraform code is provisioned by visiting the AWS console.<\/p>\n<p>If you have used the AMI id we provided, the load balancer URL should give the webpage as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-2-42.png\" class=\"kg-image\" alt=\"AWS autoscaling ALB with springboot application webpage.\" loading=\"lazy\" width=\"829\" height=\"499\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-2-42.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-2-42.png 829w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>Check if the auto-scaling group is working by terminating the instance, if it launches a new instance automatically it means it is working as expected.<\/p>\n<p>It takes approximately 30 seconds to launch a new instance.<\/p>\n<h3 id=\"step-6-cleanup\">Step 6: Cleanup<\/h3>\n<p>To clean up the setup, use the following command.<\/p>\n<pre><code>terraform destroy -var-file=..\/..\/..\/vars\/dev\/alb-asg.tfvars<\/code><\/pre>\n<blockquote><p><strong>Note<\/strong>: There are many parameters supported by the autoscaling group and application load balancer resources. If you want to deploy these for production use cases, please refer to the official documentation and design a solution that complies with security and availability as per organizational standards. Refer to terraform official <a href=\"https:\/\/registry.terraform.io\/providers\/hashicorp\/aws\/latest\/docs\/resources\/autoscaling_group?ref=devopscube.com\" rel=\"noreferrer noopener\">aws_autoscaling_group<\/a> and <a href=\"https:\/\/registry.terraform.io\/providers\/hashicorp\/aws\/latest\/docs\/resources\/lb.html?ref=devopscube.com\" rel=\"noreferrer noopener\">aws_lb<\/a> to know about all the supported parameters.<\/p><\/blockquote>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this guide, we looked at terraform autoscaling groups and application load balancer provisioning.<\/p>\n<p>When using Autoscaling groups and load balancers for production, you must consider <strong>security, availability, <\/strong><a href=\"https:\/\/devopscube.com\/how-to-setup-and-push-serverapplication-logs-to-aws-cloudwatch\/\"><strong>cloudwatch logging<\/strong><\/a><strong>, scalability, and <\/strong><a href=\"https:\/\/devopscube.com\/devops-tools-for-infrastructure-automation\/\"><strong>monitoring<\/strong><\/a>. Whether you are using a community module or a custom terraform AWS module, ensure you follow the organization\u2019s standards.<\/p>\n<p>You can also check out our guide on <a href=\"https:\/\/devopscube.com\/terraform-aws-rds\/\">provisioning RDS using Terraform<\/a>.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/terraform-autoscaling-group\/\" target=\"_blank\" rel=\"noopener noreferrer\">AWS Terraform Autoscaling Group With ALB [Tutorial] \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/terraform-autoscaling-group\/<\/p>\n","protected":false},"author":1,"featured_media":939,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-938","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/938","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=938"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/938\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/939"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}