Open In App

How To Create Elastic IP In AWS Using Terraform ?

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

Using Terraform, creating an Elastic IP (EIP) on AWS entails specifying the resources required in a Terraform configuration file in order to create the EIP. A static IPv4 address intended for dynamic cloud computing is called an elastic IP. It is helpful in situations when you require a public IP address that is reliable and simple to link to or disconnect from instances within your AWS infrastructure.

What Is Elastic IP In AWS?

In Amazon Web Services (AWS), an Elastic IP (EIP) is a static IPv4 address designed for dynamic cloud computing. It is primarily used to provide a fixed, public IP address to an AWS resource, such as an EC2 instance, NAT gateway, or a Network Load Balancer (NLB). Unlike the standard public IP addresses assigned to EC2 instances, which may change if the instance is stopped and restarted, an Elastic IP remains associated with the account until it is explicitly released.

Step-by-step To Create Elastic IP in AWS using Terraform

Step 1: As seen in the image below, create a file with the name eip.tf and paste the following code into it.

 required_providers {
aws = {
source = "hashicorp/aws"
}
}
}

provider "aws" {
region = "us-east-1"
access_key = "<Provide Your Key"
secret_key = "Provide Ypur Key"

}

resource "aws_eip" "lb" {
instance = "172.31.40.250"
domain = "vpc"
}

Script Explanation

  • provider “aws” declare that you’re using the AWS provider for this Terraform configuration.
  • region = “us-east-1” specifies the AWS region where the resources will be provisioned. Change this to your desired region.
  • access_key and secret_key are AWS access and secret keys respectively. Replace <Provide Your Key> with your actual AWS access and secret keys. Note that it’s generally recommended to use IAM roles or AWS credentials file instead of hardcoding access and secret keys in Terraform configurations for security reasons.
  • resource “aws_eip” “lb” declares an Elastic IP (EIP) resource named “lb”.
  • instance = “172.31.40.250” specifies the instance ID to associate the Elastic IP with. In this case, it’s set to an IP address (172.31.40.250). Note that this should be the ID of the instance, not its private IP address. You might want to replace this with the actual instance ID.
  • domain = “vpc” specifies the domain of the Elastic IP. “vpc” indicates that it’s a VPC (Virtual Private Cloud) EIP. This is the default value, and it’s appropriate for most VPC scenarios.

Step 2: When you run Terraform init in your Terraform project directory, it initializes your working directory and sets up Terraform to work with your configuration. Here’s what happens when you run “terraform init”.

terraform init

Terraform init

Step 3: When you run terraform plan in your Terraform project directory, Terraform analyzes your configuration files and generates an execution plan. Here’s what happens when you run terraform plan.

terraform plan

Terraform plan

Step 4: The terraform apply command is used to apply the changes defined in your Terraform configuration to your infrastructure. When you run terraform apply, Terraform compares the current state of your infrastructure with the desired state defined in your configuration files, and then makes the necessary changes to bring the infrastructure into the desired state. Here’s what happens when you run “terraform apply”.

terraform apply

EC2 Instance Before Allocating the Elastic IP

Public IP

AWS Elastic IP In Console Before Applying “Terraform init”

AWS console before creating

You may observe the changes by using the terraform apply command on the terminal.

terraform apply

As you can see, the IP was linked to the necessary instance and the elastic IP addresses.Screenshot-(683)

The Elastic IP was created and linked to the necessary EC2 instance, as you can see below.

Elastic IP – FAQ’s

What is elastic IP and dynamic IP?

  • Elastic IP (EIP): An Elastic IP address is a static IPv4 address designed for dynamic cloud computing. It is provided by cloud service providers such as Amazon Web Services (AWS).
  • Dynamic IP Address: A dynamic IP address is one that is assigned to a device (typically a computer or router) dynamically by a DHCP (Dynamic Host Configuration Protocol) server.

Why is elastic IP called elastic?

The term “elastic” in “Elastic IP” refers to its flexibility and elasticity in terms of allocation and association with cloud resources.

What is a NAT gateway?

A NAT (Network Address Translation) gateway is a network device that enables multiple devices within a private network to share a single public IP address when accessing resources on the internet. It achieves this by translating the private IP addresses of devices within the local network into a single public IP address that is used for communication with external networks such as the internet. NAT gateways are commonly used in scenarios where multiple devices within a private network need to access the internet but only have a limited number of public IP addresses available.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads