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
I created multiple files to maintain easily.
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"
}
}
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"
}
}
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