125 lines
3.6 KiB
Markdown
125 lines
3.6 KiB
Markdown
# Assignment
|
|
|
|
The Nautilus DevOps team is automating VPC creation using Terraform to manage networking efficiently. As part of this task, they need to create a VPC with specific requirements.
|
|
|
|
For this task, create an AWS VPC using Terraform with the following requirements:
|
|
|
|
The VPC name datacenter-vpc should be stored in a variable named KKE_vpc.
|
|
|
|
The VPC should have a CIDR block of 10.0.0.0/16.
|
|
|
|
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.
|
|
|
|
The Terraform working directory is /home/bob/terraform.
|
|
|
|
Right-click under the EXPLORER section in VS Code and select Open in Integrated Terminal to launch the terminal.
|
|
|
|
# Solution
|
|
|
|
# VPC with Variables — `datacenter-vpc` (`KKE_vpc`)
|
|
|
|
This task requires a **two-file** Terraform structure: `variables.tf` holds the
|
|
configuration values, and `main.tf` references them. The VPC name must be stored in a
|
|
variable named exactly `KKE_vpc`.
|
|
|
|
## `variables.tf`
|
|
|
|
```hcl
|
|
variable "KKE_vpc" {
|
|
description = "Name of the VPC"
|
|
type = string
|
|
default = "datacenter-vpc"
|
|
}
|
|
|
|
variable "KKE_vpc_cidr" {
|
|
description = "CIDR block for the VPC"
|
|
type = string
|
|
default = "10.0.0.0/16"
|
|
}
|
|
```
|
|
|
|
## `main.tf`
|
|
|
|
```hcl
|
|
terraform {
|
|
required_providers {
|
|
aws = {
|
|
source = "hashicorp/aws"
|
|
version = "~> 6.0"
|
|
}
|
|
}
|
|
}
|
|
|
|
provider "aws" {
|
|
region = "us-east-1"
|
|
}
|
|
|
|
resource "aws_vpc" "datacenter_vpc" {
|
|
cidr_block = var.KKE_vpc_cidr
|
|
|
|
tags = {
|
|
Name = var.KKE_vpc
|
|
}
|
|
}
|
|
```
|
|
|
|
## How to run
|
|
|
|
```bash
|
|
cd /home/bob/terraform
|
|
terraform init
|
|
terraform apply -auto-approve
|
|
```
|
|
|
|
## How it works
|
|
|
|
### Splitting config from logic
|
|
|
|
Terraform loads **every** `.tf` file in the working directory and merges them into one
|
|
configuration, so splitting declarations across files is purely organizational — there
|
|
are no imports to wire up. The convention this task asks for:
|
|
|
|
- **`variables.tf`** — declares input variables (the "what": names, CIDRs, sizes).
|
|
- **`main.tf`** — declares resources (the "how"), referencing the variables with
|
|
`var.<name>`.
|
|
|
|
This separation is standard practice: it centralizes the values people tweak, keeps
|
|
resource blocks generic and reusable, and makes the config easier to parameterize
|
|
later (per-environment `.tfvars`, overrides, etc.).
|
|
|
|
### The variables
|
|
|
|
- **`variable "KKE_vpc"`** — holds the VPC name, exactly as required. The `default`
|
|
of `datacenter-vpc` means no value needs to be passed at apply time; Terraform uses
|
|
the default.
|
|
- **`variable "KKE_vpc_cidr"`** — holds the CIDR. Putting it here (rather than
|
|
hardcoding in `main.tf`) satisfies the "configuration values should be stored in
|
|
variables.tf" note. Its default is the required `10.0.0.0/16`.
|
|
- **`type = string`** constrains each to a string, so Terraform validates the input
|
|
type at plan time.
|
|
|
|
### Referencing variables in `main.tf`
|
|
|
|
- **`cidr_block = var.KKE_vpc_cidr`** — resolves to `10.0.0.0/16`.
|
|
- **`tags = { Name = var.KKE_vpc }`** — resolves the VPC's `Name` tag to
|
|
`datacenter-vpc`. A VPC has no native name field, so the `Name` tag is what the
|
|
console and graders read as its name.
|
|
|
|
The `var.` prefix is how Terraform interpolates a declared variable's value. Because
|
|
both variables have defaults, `terraform apply` runs non-interactively; without
|
|
defaults, Terraform would prompt for the values (or you'd pass `-var` / a `.tfvars`
|
|
file).
|
|
|
|
## Verify
|
|
|
|
```bash
|
|
aws ec2 describe-vpcs \
|
|
--filters Name=tag:Name,Values=datacenter-vpc \
|
|
--query 'Vpcs[0].{Id:VpcId,Cidr:CidrBlock,Name:Tags[?Key==`Name`]|[0].Value}'
|
|
```
|
|
|
|
Expected — the VPC ID, `CidrBlock: 10.0.0.0/16`, and `Name: datacenter-vpc`. |