Create Elastic IP Using Terraform


The Nautilus DevOps team is strategizing the migration of a portion of their infrastructure to the AWS cloud. Recognizing the scale of this undertaking, they have opted to approach the migration in incremental steps rather than as a single massive transition. To achieve this, they have segmented large tasks into smaller, more manageable units. This granular approach enables the team to execute the migration in gradual phases, ensuring smoother implementation and minimizing disruption to ongoing operations. By breaking down the migration into smaller tasks, the Nautilus DevOps team can systematically progress through each stage, allowing for better control, risk mitigation, and optimization of resources throughout the migration process.
For this task, allocate an Elastic IP address named nautilus-eip using Terraform.
We are going to create a Terraform configuration in the file /home/bob/terraform/main.tf.
The task is to allocate an Elastic IP address named "nautilus-eip".
We are using AWS as the cloud provider.
Steps:
We need to specify the AWS provider. However, the problem does not specify a region, so we can either set a default region or make it configurable.
We will create an aws_eip resource with the tag "Name" set to "nautilus-eip".
the main.tf will look like:
provider "aws" {
region = "us-east-1"
}
resource "aws_eip" "nautilus_eip" {
tags = {
Name = "nautilus-eip"
}
}
However, note: the resource name in Terraform is nautilus_eip (using underscores) and the tag "Name" is set to "nautilus-eip". This matches the requirement.
But wait:
the requirement is to allocate an Elastic IP address named "nautilus-eip". The naming in AWS is done via the tag. So this should be correct.
Alternatively, we can use the vpc = true attribute if we want the EIP to be in a VPC. The default behavior of the aws_eip resource without vpc is to create an EC2-Classic EIP. However, since EC2-Classic is deprecated, and we are in a VPC, we should set vpc = true. But the problem doesn't specify.
But wait …after you run Terraform Plan command there is chances of getting the below mentioned error.
So to solve this error we will use domain instead.
Replace the deprecated argument vpc = true with domain = “vpc”.
Since the requirement is to have the Elastic IP named (tagged) as nautilus_eip, we keep the tag.
Here's the main.tf configuration:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "6.6.0"
}
}
}
provider "aws" {
region = "us-east-1" # Update region if needed
}
resource "aws_eip" "nautilus_eip" {
domain = "vpc"
tags = {
Name = "nautilus-eip"
}
}
Now Run these commands in your terminal:
cd /home/bob/terraform
terraform init
terraform apply
The Elastic IP will now be properly allocated in your VPC with the name "nautilus-eip".
As we have covered some of the basics in Next Blog we will see how we can create a EC2 instance with the help of learning we have done so far.
So, Stay tuned to next blog .
Subscribe to my newsletter
Read articles from Kunal Kumar Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Kunal Kumar Singh
Kunal Kumar Singh
I am a DevOps Engineer working in MNC. Where I automate Infrastructure using various DevOps tools and AWS Cloud.