DEV Community

Cover image for 6.Create Elastic IP Using Terraform
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

6.Create Elastic IP Using Terraform

Lab Information

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 devops-eip using Terraform.

The Terraform working directory is /home/bob/terraform. Create the main.tf file (do not create a different .tf file) to accomplish this task.

Note: Right-click under the EXPLORER section in VS Code and select Open in Integrated Terminal to launch the terminal.

Lab Solutions

Create main.tf file:

# main.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

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

resource "aws_eip" "devops_eip" {
  domain = "vpc"

  tags = {
    Name = "devops-eip"
  }
}
Enter fullscreen mode Exit fullscreen mode

This configuration:

Provider Configuration: Sets up the AWS provider (defaults to us-east-1 region, which is commonly used if not specified, but I've explicitly set it for clarity).

Elastic IP Resource: Creates the Elastic IP with:

Domain: vpc - This specifies that the EIP is for use in VPC (required for modern AWS)

Name tag: devops-eip as specified in the requirements

Important Notes:

The domain = "vpc" parameter is essential as it allocates the EIP for use in EC2-VPC (the current standard)

Without specifying a region, it would default to us-east-1, but I've included it explicitly for clarity

The EIP will be allocated to your AWS account and remain associated until you explicitly release it

To deploy this configuration:

Navigate to the Terraform directory:

cd /home/bob/terraform
Enter fullscreen mode Exit fullscreen mode

Initialize Terraform:

terraform init
Enter fullscreen mode Exit fullscreen mode

Plan the deployment to verify the configuration:

terraform plan
Enter fullscreen mode Exit fullscreen mode

Apply the configuration:

terraform apply
Enter fullscreen mode Exit fullscreen mode

Then type yes when prompted to confirm the creation of the Elastic IP.

After successful creation, Terraform will output the allocated Elastic IP address. You can also check the allocation in the AWS Management Console under EC2 → Elastic IPs.

This Elastic IP can now be used by the Nautilus DevOps team for various purposes in their incremental migration, such as:

Associating with NAT Gateways

Assigning to EC2 instances that need static public IPs

Load balancers or other resources requiring persistent public IP addresses


Resources & Next Steps
📦 Full Code Repository: KodeKloud Learning Labs
📖 More Deep Dives: Whispering Cloud Insights - Read other technical articles
💬 Join Discussion: DEV Community - Share your thoughts and questions
💼 Let's Connect: LinkedIn - I'd love to connect with you

Credits
• All labs are from: KodeKloud
• I sincerely appreciate your provision of these valuable resources.

Top comments (0)