Bootstrap FreeKB - Amazon Web Services (AWS) - List Elastic IP Addresses (EIP) using Terraform
Amazon Web Services (AWS) - List Elastic IP Addresses (EIP) using Terraform


An Elastic IP address is a static public IP address that is typically associated with one of your EC2 instances as a way to have a static public IP address that can be used to route traffic into your Amazon Web Services cluster. Typically, you will create a DNS A record to bind a hostname such as www.example.com to the elastic IP address. The DNS can be done using Amazon Web Services Route 53.

Let's say you have the following files on your Terraform server.

├── required_providers.tf
├── elastic_ips (directory)
│   ├── data.tf
│   ├── outputs.tf
│   ├── provider.tf

 

required_providers.tf will almost always have this.

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

 

Let's say provider.tf has the following. In this example, the "default" profile in /home/username/.aws/config and /home/username/.aws/credentials is being used. This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.

provider "aws" {
  alias   = "default"
  profile = "default"
  region  = "default"
}

 

Let's say data.tf has the following to capture the JSON for all of the Elastic IPs you've already created.

data "aws_eips" "my-eips" {}

 

Or, to fetch the JSON based on different tags.

data "aws_eips" "staging-eips" {
  tags = {
    environment = "staging"
  }
}

data "aws_eips" "production-eips" {
  tags = {
    environment = "production"
  }
}

 

Let's say outputs.tf has the following

output "staging_eips" {
  value = data.aws_eips.staging-eips
}

output "production_eips" {
  value = data.aws_eips.production-eips
}

 

The terraform refresh or terraform output command should return something like this.

staging_eips = {
  "allocation_ids" = tolist([
    "eipalloc-0a5c72c343bc546b0",
  ])
  "filter" = toset(null) /* of object */
  "id" = "us-east-1"
  "public_ips" = tolist([
    "177.26.21.19",
  ])
  "tags" = tomap(null) /* of string */
  "timeouts" = null /* object */
}

production_eips = {
  "allocation_ids" = tolist([
    "eipalloc-0a5c72c343bc546b0",
  ])
  "filter" = toset(null) /* of object */
  "id" = "us-east-1"
  "public_ips" = tolist([
    "54.226.22.119",
  ])
  "tags" = tomap(null) /* of string */
  "timeouts" = null /* object */
}

 

You probably just want the public_ips. outputs.tf could include public_ips.

output "staging_ips" {
  value = data.aws_eips.staging-eips.public_ips
}

output "production_ips" {
  value = data.aws_eips.production-eips.public_ips
}

 

Which should return the following.

staging_ips = tolist([
  "177.26.21.19",
])

production_ips = tolist([
  "54.226.22.119",
])

 

If you have a single IP per environment, then you can do something like this.

output "staging-ip" {
  value = data.aws_eips.staging-eips.public_ips[0]
}

output "production-ip" {
  value = data.aws_eips.production-eips.public_ips[0]
}

 

Which should return the following.

staging-ip = "177.26.21.19"
production-ip = "54.226.22.119"

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter f25e6e in the box below so that we can be sure you are a human.