DEV Community

Cover image for 1.Create VPC and Subnet Using Terraform
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

1.Create VPC and Subnet Using Terraform

Lab Information

To ensure proper resource provisioning order, the DevOps team wants to explicitly define the dependency between an AWS VPC and a Subnet. The objective is to create a VPC and then a Subnet that explicitly depends on it using Terraform's depends_on argument.

Please complete the following tasks:

Create a VPC named xfusion-vpc.

Create a Subnet named xfusion-subnet.

Ensure the Subnet uses the depends_on argument to explicitly depend on the VPC resource.

Create the main.tf file (do not create a separate .tf file) to provision a VPC and Subnet.

Use variables.tf, define the following variables:

KKE_VPC_NAME for the VPC name.
KKE_SUBNET_NAME for the Subnet name.
Use terraform.tfvars to input the names of the VPC and subnet.

In outputs.tf, output the following:

kke_vpc_name: The name of the VPC.
kke_subnet_name: The name of the Subnet.

Lab Solutions

✅ Step 1: Create the required Terraform files

Make sure you are in the lab working directory.

1️⃣ main.tf

Creates the VPC and Subnet, with an explicit dependency using depends_on.

resource "aws_vpc" "xfusion_vpc" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = var.KKE_VPC_NAME
  }
}

resource "aws_subnet" "xfusion_subnet" {
  vpc_id     = aws_vpc.xfusion_vpc.id
  cidr_block = "10.0.1.0/24"

  depends_on = [
    aws_vpc.xfusion_vpc
  ]

  tags = {
    Name = var.KKE_SUBNET_NAME
  }
}

Enter fullscreen mode Exit fullscreen mode

2️⃣ variables.tf

Defines variables for VPC and Subnet names.

variable "KKE_VPC_NAME" {
  description = "Name of the VPC"
  type        = string
}

variable "KKE_SUBNET_NAME" {
  description = "Name of the Subnet"
  type        = string
}
Enter fullscreen mode Exit fullscreen mode

3️⃣ terraform.tfvars

Provides values for the variables.

KKE_VPC_NAME     = "xfusion-vpc"
KKE_SUBNET_NAME = "xfusion-subnet"
Enter fullscreen mode Exit fullscreen mode

4️⃣ outputs.tf

Outputs the VPC and Subnet names.

output "kke_vpc_name" {
  value = var.KKE_VPC_NAME
}

output "kke_subnet_name" {
  value = var.KKE_SUBNET_NAME
}
Enter fullscreen mode Exit fullscreen mode

✅ Step 2: Initialize Terraform (MANDATORY in KodeKloud)

Run:

terraform init
Enter fullscreen mode Exit fullscreen mode

✔ Downloads the AWS provider
✔ Initializes backend
✔ Required for the lab to proceed

✅ Step 3: Validate configuration (optional but recommended)

Run:

terraform validate
Enter fullscreen mode Exit fullscreen mode

You should see:

Success! The configuration is valid.

✅ Step 4: Apply the Terraform configuration (REQUIRED to finish lab)

terraform apply
Enter fullscreen mode Exit fullscreen mode

When prompted:

Do you want to perform these actions?

Type:

yes

✅ Step 5: Verify Outputs (Lab Validation)

After apply completes, you should see:

Outputs:

kke_vpc_name = "xfusion-vpc"
kke_subnet_name = "xfusion-subnet"

This confirms:

VPC was created

Subnet was created

Dependency was honored

Outputs are correct


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)