
If you are not familiar with AWS Application Load Balancers, check out my article FreeKB - Amazon Web Services (AWS) - Getting Started with Application Load Balancer (ALB).
Let's say you have two or more EC2 instances that are producing web apps. An application load balancer can be used to load balance the requests for the web app.
This assumes you have setup Terraform with the Amazon Web Services (AWS) provider. If not, check out my article Amazon Web Services (AWS) Getting Started with Terraform.
Let's say you have the following files on your Terraform server.
├── required_providers.tf
├── application_load_balancers (directory)
│ ├── provider.tf
│ ├── listeners.tf
│ ├── load_balancers.tf
│ ├── target_groups.tf
required_providers.tf will almost always have this.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
Let's say provider.tf has the following. In this example, the "default" profile in /home/username/.aws/config and /home/username/.aws/credentials is being used. This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.
provider "aws" {
alias = "default"
profile = "default"
region = "default"
}
This assume you have already:
- Created an Application Load Balancer using Terraform
- Created an Application Load Balancer Target Group using Terraform
listeners.tf could have the following to create an Application Load Balancer Listener
resource "aws_lb_listener" "my-application-load-balancer-listener" {
load_balancer_arn = aws_lb.my-application-load-balancer.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.my-application-load-balancer-target-group.arn
}
tags = {
Name = "my-listener"
}
}
The terraform plan command can be used to see what Terraform will try to do.
terraform plan
And the terraform apply command can be used to create the Elastic Load balancer Target Group.
terraform apply
Did you find this article helpful?
If so, consider buying me a coffee over at