104 lines
3.1 KiB
Markdown
104 lines
3.1 KiB
Markdown
# Assignment
|
|
|
|
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 xfusion-eip should be stored in a variable named KKE_eip. The Terraform working directory is /home/bob/terraform.
|
|
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.
|
|
|
|
# Solution
|
|
|
|
# Elastic IP with Variables — `xfusion-eip` (`KKE_eip`)
|
|
|
|
Two-file structure: `variables.tf` holds the config, `main.tf` references it. The
|
|
Elastic IP name must be stored in a variable named exactly `KKE_eip`.
|
|
|
|
## `variables.tf`
|
|
|
|
```hcl
|
|
variable "KKE_eip" {
|
|
description = "Name of the Elastic IP"
|
|
type = string
|
|
default = "xfusion-eip"
|
|
}
|
|
```
|
|
|
|
## `main.tf`
|
|
|
|
```hcl
|
|
terraform {
|
|
required_providers {
|
|
aws = {
|
|
source = "hashicorp/aws"
|
|
version = "~> 6.0"
|
|
}
|
|
}
|
|
}
|
|
|
|
provider "aws" {
|
|
region = "us-east-1"
|
|
}
|
|
|
|
resource "aws_eip" "xfusion_eip" {
|
|
domain = "vpc"
|
|
|
|
tags = {
|
|
Name = var.KKE_eip
|
|
}
|
|
}
|
|
```
|
|
|
|
## How to run
|
|
|
|
```bash
|
|
cd /home/bob/terraform
|
|
terraform init
|
|
terraform apply -auto-approve
|
|
```
|
|
|
|
## How it works
|
|
|
|
### Splitting config from logic
|
|
|
|
Terraform merges every `.tf` file in the directory into one configuration, so
|
|
`variables.tf` and `main.tf` work as a single unit with no imports. `variables.tf`
|
|
declares inputs; `main.tf` declares resources that consume them via `var.<name>`.
|
|
|
|
### The variable
|
|
|
|
- **`variable "KKE_eip"`** — holds the Elastic IP name, exactly as required. The
|
|
`default` of `xfusion-eip` lets `terraform apply` run without prompting.
|
|
- **`type = string`** validates the input type at plan time.
|
|
|
|
### The Elastic IP
|
|
|
|
- **`domain = "vpc"`** — allocates the EIP for use in a VPC. This is the modern
|
|
argument; the old `vpc = true` was removed in AWS provider v5+, so on `~> 6.0` you
|
|
must use `domain`. Since the task only asks to *allocate* the address (no instance or
|
|
ENI association), no `instance` or `network_interface` argument is set.
|
|
- **`tags = { Name = var.KKE_eip }`** — an Elastic IP has no native name field, so the
|
|
`Name` tag (resolved from the variable to `xfusion-eip`) is what the console and
|
|
graders read as its name.
|
|
|
|
The `var.` prefix interpolates the declared variable's value. Because the variable has
|
|
a default, `terraform apply` runs non-interactively.
|
|
|
|
> Cost note: an unassociated Elastic IP incurs an hourly charge, but that's the
|
|
> sandbox's concern — the task only requires the allocation to exist.
|
|
|
|
## Verify
|
|
|
|
```bash
|
|
aws ec2 describe-addresses \
|
|
--filters Name=tag:Name,Values=xfusion-eip \
|
|
--query 'Addresses[0].{IP:PublicIp,AllocId:AllocationId,Name:Tags[?Key==`Name`]|[0].Value}'
|
|
```
|
|
|
|
Expected — a public IP, an allocation ID, and `Name: xfusion-eip`. |