Skip to content

Commit cf44009

Browse files
authored
Add exercise and solution for creating a custom VPC and subnets with … (bregman-arie#10597)
* 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
1 parent 2a9bfcb commit cf44009

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

topics/terraform/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
|--------|--------|------|----|----|
5454
| Launch EC2 instance | EC2 | [Exercise](exercises/launch_ec2_instance/exercise.md) | [Solution](exercises/launch_ec2_instance/solution.md) | |
5555
| Rename S3 bucket | S3 | [Exercise](exercises/s3_bucket_rename/exercise.md) | [Solution](exercises/s3_bucket_rename/solution.md) | |
56+
| Create Custom VPC and Subnets | VPC | [Exercise](exercises/vpc_subnet_creation/exercise.md) | [Solution](exercises/vpc_subnet_creation/solution.md) | |
5657

5758
## Questions
5859

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Creating Custom VPC and Subnets with Terraform
2+
3+
## Requirements
4+
* An existing AWS account with permissions to create VPCs and subnets.
5+
* Terraform installed on your local machine.
6+
* AWS CLI configured with your credentials.
7+
8+
9+
## Objectives
10+
1. Create a custom VPC with a specified CIDR block.
11+
For example, you can use `10.0.0.0/16`.
12+
2. Create two subnets within the VPC, each with a different CIDR block.
13+
For example, you can use `10.0.0.0/20` for the first subnet and `10.0.16.0/20` for the second subnet.
14+
15+
Both subnets should be in different availability zones to ensure high availability.
16+
3. Ensure that the VPC and subnets are tracked by Terraform.
17+
18+
## Solution
19+
Click [here to view the solution](solution.md)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Creating Custom VPC and Subnets with Terraform
2+
3+
4+
## Objectives
5+
1. Create a custom VPC with a specified CIDR block.
6+
For example, you can use `10.0.0.0/16`.
7+
2. Create two subnets within the VPC, each with a different CIDR block.
8+
For example, you can use `10.0.0.0/20` for the first subnet and `10.0.16.0/20` for the second subnet.
9+
10+
Both subnets should be in different availability zones to ensure high availability.
11+
3. Ensure that the VPC and subnets are tracked by Terraform.
12+
13+
14+
## Solution
15+
16+
17+
18+
```sh
19+
# Create a directory for the Terraform configuration
20+
mkdir vpc_subnet_creation && cd vpc_subnet_creation
21+
```
22+
23+
```sh
24+
# Create the main.tf file with the VPC and subnets configuration
25+
touch main.tf
26+
```
27+
28+
```terraform
29+
terraform {
30+
required_providers {
31+
aws = {
32+
source = "hashicorp/aws"
33+
version = "~> 5.0"
34+
}
35+
}
36+
}
37+
38+
provider "aws" {
39+
region = "your-region" # e.g., ap-south-1
40+
}
41+
42+
resource "aws_vpc" "my_custom_vpc" {
43+
cidr_block = "10.0.0.0/16"
44+
tags = {
45+
Name = "my_custom_vpc_made_with_terraform"
46+
}
47+
}
48+
49+
resource "aws_subnet" "Subnet_A" {
50+
cidr_block = "10.0.0.0/20"
51+
vpc_id = aws_vpc.my_custom_vpc.id
52+
availability_zone = "your-availability-zone-a" # e.g., ap-south-1a
53+
tags = {
54+
"Name" = "Subnet A"
55+
}
56+
}
57+
resource "aws_subnet" "Subnet_B" {
58+
cidr_block = "10.0.16.0/20"
59+
vpc_id = aws_vpc.my_custom_vpc.id
60+
availability_zone = "your-availability-zone-b" # e.g., ap-south-1b
61+
tags = {
62+
"Name" = "Subnet B"
63+
}
64+
}
65+
```
66+
67+
```sh
68+
# Initialize Terraform to download the AWS provider
69+
terraform init
70+
```
71+
72+
```sh
73+
# Apply the Terraform configuration to create the VPC and subnets
74+
terraform apply -auto-approve
75+
```
76+
77+
78+

0 commit comments

Comments
 (0)