From cf4400911b4e019d601b5016f8b26ead84d4e6b8 Mon Sep 17 00:00:00 2001 From: Rishabh <125804877+MercyShark@users.noreply.github.com> Date: Thu, 7 Aug 2025 22:53:43 +0530 Subject: [PATCH] =?UTF-8?q?Add=20exercise=20and=20solution=20for=20creatin?= =?UTF-8?q?g=20a=20custom=20VPC=20and=20subnets=20with=20=E2=80=A6=20(#105?= =?UTF-8?q?97)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add exercise and solution for creating a custom VPC and subnets with Terraform * Add exercise and solution for creating a custom VPC and subnets with Terraform --- topics/terraform/README.md | 1 + .../exercises/vpc_subnet_creation/exercise.md | 19 +++++ .../exercises/vpc_subnet_creation/solution.md | 78 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 topics/terraform/exercises/vpc_subnet_creation/exercise.md create mode 100644 topics/terraform/exercises/vpc_subnet_creation/solution.md diff --git a/topics/terraform/README.md b/topics/terraform/README.md index c656baa..c1b76cc 100644 --- a/topics/terraform/README.md +++ b/topics/terraform/README.md @@ -53,6 +53,7 @@ |--------|--------|------|----|----| | Launch EC2 instance | EC2 | [Exercise](exercises/launch_ec2_instance/exercise.md) | [Solution](exercises/launch_ec2_instance/solution.md) | | | Rename S3 bucket | S3 | [Exercise](exercises/s3_bucket_rename/exercise.md) | [Solution](exercises/s3_bucket_rename/solution.md) | | +| Create Custom VPC and Subnets | VPC | [Exercise](exercises/vpc_subnet_creation/exercise.md) | [Solution](exercises/vpc_subnet_creation/solution.md) | | ## Questions diff --git a/topics/terraform/exercises/vpc_subnet_creation/exercise.md b/topics/terraform/exercises/vpc_subnet_creation/exercise.md new file mode 100644 index 0000000..45c5252 --- /dev/null +++ b/topics/terraform/exercises/vpc_subnet_creation/exercise.md @@ -0,0 +1,19 @@ +# Creating Custom VPC and Subnets with Terraform + +## Requirements +* An existing AWS account with permissions to create VPCs and subnets. +* Terraform installed on your local machine. +* AWS CLI configured with your credentials. + + +## Objectives +1. Create a custom VPC with a specified CIDR block. + For example, you can use `10.0.0.0/16`. +2. Create two subnets within the VPC, each with a different CIDR block. + For example, you can use `10.0.0.0/20` for the first subnet and `10.0.16.0/20` for the second subnet. + + Both subnets should be in different availability zones to ensure high availability. +3. Ensure that the VPC and subnets are tracked by Terraform. + +## Solution +Click [here to view the solution](solution.md) diff --git a/topics/terraform/exercises/vpc_subnet_creation/solution.md b/topics/terraform/exercises/vpc_subnet_creation/solution.md new file mode 100644 index 0000000..1c047c1 --- /dev/null +++ b/topics/terraform/exercises/vpc_subnet_creation/solution.md @@ -0,0 +1,78 @@ +# Creating Custom VPC and Subnets with Terraform + + +## Objectives +1. Create a custom VPC with a specified CIDR block. + For example, you can use `10.0.0.0/16`. +2. Create two subnets within the VPC, each with a different CIDR block. + For example, you can use `10.0.0.0/20` for the first subnet and `10.0.16.0/20` for the second subnet. + + Both subnets should be in different availability zones to ensure high availability. +3. Ensure that the VPC and subnets are tracked by Terraform. + + +## Solution + + + +```sh +# Create a directory for the Terraform configuration +mkdir vpc_subnet_creation && cd vpc_subnet_creation +``` + +```sh +# Create the main.tf file with the VPC and subnets configuration +touch main.tf +``` + +```terraform +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.0" + } + } +} + +provider "aws" { + region = "your-region" # e.g., ap-south-1 +} + +resource "aws_vpc" "my_custom_vpc" { + cidr_block = "10.0.0.0/16" + tags = { + Name = "my_custom_vpc_made_with_terraform" + } +} + +resource "aws_subnet" "Subnet_A" { + cidr_block = "10.0.0.0/20" + vpc_id = aws_vpc.my_custom_vpc.id + availability_zone = "your-availability-zone-a" # e.g., ap-south-1a + tags = { + "Name" = "Subnet A" + } +} +resource "aws_subnet" "Subnet_B" { + cidr_block = "10.0.16.0/20" + vpc_id = aws_vpc.my_custom_vpc.id + availability_zone = "your-availability-zone-b" # e.g., ap-south-1b + tags = { + "Name" = "Subnet B" + } +} +``` + +```sh +# Initialize Terraform to download the AWS provider +terraform init +``` + +```sh +# Apply the Terraform configuration to create the VPC and subnets +terraform apply -auto-approve +``` + + +