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.
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 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
- After successful output apply the changes using below command.
terrraform apply
- After verifying type “yes” to confirm and apply.
- Terraform will start creating load balancer.
- You can also verify deployment by visiting console and opening Elastic load balancers.
AWS Load Balancing Using Terraform
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.