If you create an EIP in your terraform modules, it will be destroyed when you destroy terraform modules.

To make EIP persistent, I usually create an AWS eip terraform module with S3 remote state support and I read it from another module.

Here is the basic example.

PS I will create an terraform module when I have more time

  1. Create EIP module

I created multiple files to maintain easily.

config.tf

This file contains a terraform backend configuration. if you don’t have it you can create it by using

$ cat config.tf
terraform {
backend "s3" {
bucket     = "my-tf-remote-state"
key        = "eip/terraform.tfstate"
region     = "us-east-2"
encrypt    = true
kms_key_id = "6e0b950f-1bce-49cd-xyz"
}
}

main.tf

This file creates an Elastic IP resource called elasticsearch You can choose any name, I used this EIP in my elastic search instance.

provider "aws" {
region = var.region
}
resource "aws_eip" "elasticsearch" {
vpc = true
tags = {
Name = "${var.namespace}-${var.stage}-${var.name}-elasticsearch-eip"
}
}

variables.tf

variable "region" {
}

variable "namespace" {
type        = string
description = "Namespace, which could be your organization name, e.g. 'eg' or 'cp'"
}
variable "stage" {
type        = string
description = "Stage, e.g. 'prod', 'staging', 'dev' or 'testing'"
}
variable "name" {
type        = string
description = "Solution name, e.g. 'app' or 'cluster'"
}

#remote-state #aws #eip #terraform

Terraform remote state for AWS Elastic IP
1.85 GEEK