3.7 KiB
Assignment
The Nautilus DevOps team is strategizing the migration of a portion of their infrastructure to the AWS cloud. Recognizing the scale of this undertaking, they have opted to approach the migration in incremental steps rather than as a single massive transition. To achieve this, they have segmented large tasks into smaller, more manageable units. This granular approach enables the team to execute the migration in gradual phases, ensuring smoother implementation and minimizing disruption to ongoing operations. By breaking down the migration into smaller tasks, the Nautilus DevOps team can systematically progress through each stage, allowing for better control, risk mitigation, and optimization of resources throughout the migration process.
Create a VPC named datacenter-vpc in region us-east-1 with any IPv4 CIDR block through terraform.
The Terraform working directory is /home/bob/terraform. Create the main.tf file (do not create a different .tf file) to accomplish this task.
Note: Right-click under the EXPLORER section in VS Code and select Open in Integrated Terminal to launch the terminal.
Solution
Terraform VPC — datacenter-vpc
Create a VPC in us-east-1 with any IPv4 CIDR block.
Create main.tf (heredoc → file)
cd /home/bob/terraform
cat > main.tf <<'EOF'
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
resource "aws_vpc" "datacenter_vpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "datacenter-vpc"
}
}
EOF
How to run
cd /home/bob/terraform
terraform init
terraform apply -auto-approve
How it works
The heredoc
cat > main.tf <<'EOF' writes the file in one shot. The quoted 'EOF' disables shell expansion,
so any $ or backticks in HCL reach the file literally rather than being interpreted by bash — the
right default whenever writing config files this way.
Note the task says main.tf only — don't split the provider block into a separate
provider.tf or versions.tf.
The provider block
region = "us-east-1"— pins the deployment region as required. A VPC is a regional resource, so this determines where it's created.required_providerswith a version constraint keepsterraform initfrom pulling an unexpected major version.
The aws_vpc resource
One resource does the whole job:
-
cidr_block = "10.0.0.0/16"— the task allows any IPv4 block, so this uses a standard RFC 1918 private range. A/16gives 65,536 addresses, plenty of room to carve subnets from later. Any valid private CIDR (172.16.0.0/16,192.168.0.0/24, …) would satisfy the requirement equally;10.0.0.0/16is the conventional default. -
tags = { Name = "datacenter-vpc" }— this is what "named" means for a VPC. AWS VPCs have no native name field; the console and any grader read theNametag. Omit the tag and the VPC still exists but shows as unnamed — the most common way to fail this task.
The Terraform resource label (datacenter_vpc) is just the internal identifier used for references
within the config; it's unrelated to the AWS-visible name, which comes from the tag.
Verify
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.
You can also confirm from Terraform's own state:
terraform state show aws_vpc.datacenter_vpc
If
describe-vpcsreturns nothing, theNametag is missing or misspelled — the filter matches on that tag, not on the resource label in the config.