Open In App

AWS Load Balancing Using Terraform

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

AWS load balancing is a service that splits incoming network traffic across multiple resources. These resources can be EC2 instances, Containers, and IP addresses. AWS provides various types of load balancers. Terraform can be used for easy and hassle-free deployment of AWS load balancers. Let’s see how we can deploy the AWS load balancer using Terraform.

Primary Components Of AWS Load Balancer With Terraform

  • Terraform: It is Infrastructure as an infrastructure-as-a-service tool that allows the deployment of resources to multiple cloud providers through code.
  • Load Balancing: Service that transfers traffic across multiple resources in AWS.
  • IaaC: Infrastructure as a Code allows to representation of cloud infrastructure in the form of code.

Steps To Setup AWS Load Balancer Using Terraform

Step 1: Set Up Terraform

  • Download the Terraform zip from the installation page of the Terraform website.
  • Extract and paste the terraform folder to the required location and add the path to runnable in environment variables.

Step 2: Set Up AWS CLI

  • Download the AWS CLI setup from official website.
  • Run the installer and follow the steps to install.

Step 3: Configure AWS CLI

  • Copy or create an AWS access key from the AWS console it is required in the next step.
  • Open the terminal or cmd and run the below command to configure the credentials of AWS as it will be required for Terraform.
aws configure
  • Provide access keys copied in the previous step and also give default region if different.

Configure the AWS Credentials

Step 4: Create Terraform Code

  • Goto your project folder and create main.tf file.
  • Add terraform block to code with aws as required provider with latest version. You can find the latest version at hashicorp registry.

Terraform Registry

  • Terraform block should look like below. You can add required version to avoid invalidation.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.37.0"
}
}
required_version = ">= 1.2.0"
}
  • Now add provider as aws like below. Specify other details as required.
provider "aws" {
region = "us-east-1"
}
  • Add configuration for load balancer. For this article we will set up a network load balancer. Similar procedure can be followed for other load balancer by replacing type parameter.
resource "aws_lb" "gfg_lb" {
name = "gfg-network-loadbalancer"
internal = false
load_balancer_type = "network"
subnets = ["subnet-07de5988fef95802d","subnet-042133d5c32b3d4af"]
}
  • We have specified name for load balancer. We have made “internal” as false for indicating that it is not an internal load balancer.
  • As we can see type parameter specifies type of load balancer and subnets are used for deployment.
  • After specifying these many details, we are ready for deployment. You can further provide more option as requirement.
  • The complete code will look like below.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.37.0"
}
}
required_version = ">= 1.2.0"
}

provider "aws" {
region = "us-east-1"
}

resource "aws_lb" "gfg_lb" {
name = "gfg-network-loadbalancer"
internal = false
load_balancer_type = "network"
subnets = ["subnet-07de5988fef95802d","subnet-042133d5c32b3d4af"]
}

Step 5: Apply The Terraform Code

  • Once the code is ready you can apply it.
  • First init the terraform by running below command in project folder where main.tf is present.
terraform init

Initializing the terraform

  • After successful output apply the changes using below command.
terrraform apply
  • After verifying type “yes” to confirm and apply.

Applying the terraform

  • Terraform will start creating load balancer.

Creating the load balancer

View the created load balancers

Conclusion

We have successfully deployed an amazon load balancer with the help of terraform in this article. the configuration described can be further modified to make changes to the load balancer and its type in aws. This is how terraform allows reusable and modifiable configuration of infrastructure.

AWS Load Balancing Using Terraform – FAQ’s

What Is Terraform, And Why Use It For AWS Load Balancing?

Terraform is an Infrastructure as Code ( IaaC ) tool that allows you to define and provision infrastructure using a declarative configuration language. Using Terraform for AWS load balancing ensures that your infrastructure is version-controlled, reproducible, and can be managed as code.

How Do I Create A Target Group And Associate It With An ALB Using Terraform?

Use the aws_lb_target_group resource to create a target group and associate it with the Application Load Balancer. Define health checks, protocol, port, and other settings.

Can I Configure Auto-Scaling With Terraform For Instances Behind An ALB?

Yes, you can use the aws_autoscaling_group resource in Terraform to configure auto-scaling for instances behind an ALB. Specify launch configurations, desired capacity, minimum and maximum sizes, etc.

Is It Possible To Configure SSL/TLS Termination On An ALB Using Terraform?

Yes, you can use the ssl_policy attribute in the aws_lb_listener resource to specify an SSL policy. Additionally, configure SSL certificates using the certificate_arn attribute.

Can Terraform Be Used To Update An Existing Load Balancer Configuration?

Yes, Terraform is designed for managing infrastructure as code, including updates to existing resources. Apply changes using terraform apply and Terraform will update the resources accordingly.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads