DEV Community

Cover image for 37.Elastic IP Variable Setup Using Terraform
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

37.Elastic IP Variable Setup Using Terraform

Lab Information

The Nautilus DevOps team is strategizing the migration of a portion of their infrastructure to the AWS cloud. As part of this phased migration approach, they need to allocate an Elastic IP address to support external access for specific workloads.

For this task, create an AWS Elastic IP using Terraform with the following requirement:

The Elastic IP name nautilus-eip should be stored in a variable named KKE_eip. The Terraform working directory is /home/bob/terraform.
Enter fullscreen mode Exit fullscreen mode

Note:

The configuration values should be stored in a variables.tf file.

The Terraform script should be structured with a main.tf file referencing variables.tf.

Right-click under the EXPLORER section in VS Code and select Open in Integrated Terminal to launch the terminal.
Enter fullscreen mode Exit fullscreen mode

Lab Solutions

Step 1: Create variables.tf

# variables.tf

# Define variable for Elastic IP name
variable "KKE_eip" {
  description = "The name of the Elastic IP"
  type        = string
  default     = "nautilus-eip"
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Create main.tf

# main.tf

# Create AWS Elastic IP with variable reference
resource "aws_eip" "this" {
  vpc = true

  tags = {
    Name = var.KKE_eip
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: 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

Output

bob@iac-server ~/terraform via πŸ’  default ➜  terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "5.91.0"...
- Installing hashicorp/aws v5.91.0...
- Installed hashicorp/aws v5.91.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
Enter fullscreen mode Exit fullscreen mode

Plan the configuration:

terraform plan
Enter fullscreen mode Exit fullscreen mode

Output

bob@iac-server ~/terraform via πŸ’  default ➜  terraform plan

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_eip.this will be created
  + resource "aws_eip" "this" {
      + allocation_id        = (known after apply)
      + arn                  = (known after apply)
      + association_id       = (known after apply)
      + carrier_ip           = (known after apply)
      + customer_owned_ip    = (known after apply)
      + domain               = (known after apply)
      + id                   = (known after apply)
      + instance             = (known after apply)
      + ipam_pool_id         = (known after apply)
      + network_border_group = (known after apply)
      + network_interface    = (known after apply)
      + private_dns          = (known after apply)
      + private_ip           = (known after apply)
      + ptr_record           = (known after apply)
      + public_dns           = (known after apply)
      + public_ip            = (known after apply)
      + public_ipv4_pool     = (known after apply)
      + tags                 = {
          + "Name" = "nautilus-eip"
        }
      + tags_all             = {
          + "Name" = "nautilus-eip"
        }
      + vpc                  = true
    }

Plan: 1 to add, 0 to change, 0 to destroy.
β•·
β”‚ Warning: Argument is deprecated
β”‚ 
β”‚   with aws_eip.this,
β”‚   on main.tf line 5, in resource "aws_eip" "this":
β”‚    5:   vpc = true
β”‚ 
β”‚ vpc is deprecated. Use domain instead.
β”‚ 
β”‚ (and one more similar warning elsewhere)
β•΅

─────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to
take exactly these actions if you run "terraform apply" now.
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 snapshot.

Output

bob@iac-server ~/terraform via πŸ’  default ➜  terraform apply

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_eip.this will be created
  + resource "aws_eip" "this" {
      + allocation_id        = (known after apply)
      + arn                  = (known after apply)
      + association_id       = (known after apply)
      + carrier_ip           = (known after apply)
      + customer_owned_ip    = (known after apply)
      + domain               = (known after apply)
      + id                   = (known after apply)
      + instance             = (known after apply)
      + ipam_pool_id         = (known after apply)
      + network_border_group = (known after apply)
      + network_interface    = (known after apply)
      + private_dns          = (known after apply)
      + private_ip           = (known after apply)
      + ptr_record           = (known after apply)
      + public_dns           = (known after apply)
      + public_ip            = (known after apply)
      + public_ipv4_pool     = (known after apply)
      + tags                 = {
          + "Name" = "nautilus-eip"
        }
      + tags_all             = {
          + "Name" = "nautilus-eip"
        }
      + vpc                  = true
    }

Plan: 1 to add, 0 to change, 0 to destroy.
β•·
β”‚ Warning: Argument is deprecated
β”‚ 
β”‚   with aws_eip.this,
β”‚   on main.tf line 5, in resource "aws_eip" "this":
β”‚    5:   vpc = true
β”‚ 
β”‚ vpc is deprecated. Use domain instead.
β”‚ 
β”‚ (and one more similar warning elsewhere)
β•΅

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_eip.this: Creating...
aws_eip.this: Creation complete after 1s [id=eipalloc-52002d8db990e6681]
β•·
β”‚ Warning: Argument is deprecated
β”‚ 
β”‚   with aws_eip.this,
β”‚   on main.tf line 5, in resource "aws_eip" "this":
β”‚    5:   vpc = true
β”‚ 
β”‚ vpc is deprecated. Use domain instead.
β•΅

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Enter fullscreen mode Exit fullscreen mode

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)