|
| 1 | +# Creating VPCs and Subnets with Terraform and IaC |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +In this exercise, we will walk through the process of creating a basic infrastructure on AWS using Terraform, an open-source Infrastructure as Code (IaC) tool. Specifically, we'll create a Virtual Private Cloud (VPC) with two subnets, a public and a private one. We will also create an Internet gateway and a route table, which will be associated with our public subnet. This setup mimics a typical cloud infrastructure configuration, separating resources into public and private subnets for improved security and control. |
| 6 | + |
| 7 | +## Desired Outcome |
| 8 | + |
| 9 | +If you wish to give it a shot before looking into the detailed step-by-step and the solution videos, here is an overview of what the created solution should deploy: |
| 10 | + |
| 11 | +1. A VPC with a CIDR block of `10.0.0.0/16`. |
| 12 | +2. One public subnet with a CIDR block of `10.0.0.0/24`. |
| 13 | +3. One private subnet with a CIDR block of `10.0.1.0/24`. |
| 14 | +4. One Internet Gateway. |
| 15 | +5. One public route table with a route to the Internet Gateway, and the correct association between the public subnet and the public route table. |
| 16 | + |
| 17 | +### Useful Resources |
| 18 | + |
| 19 | +- AWS Terraform Provider: [https://registry.terraform.io/providers/hashicorp/aws](https://registry.terraform.io/providers/hashicorp/aws) |
| 20 | + |
| 21 | +## Step-by-Step Guide |
| 22 | + |
| 23 | +1. Begin by initializing your Terraform configuration with the `terraform` block. This block sets up the necessary details regarding the providers that will be used in your configuration. In this case, the AWS provider is required. |
| 24 | + |
| 25 | + ``` |
| 26 | + terraform { |
| 27 | + required_providers { |
| 28 | + aws = { |
| 29 | + source = "hashicorp/aws" |
| 30 | + version = "~> 5.0" |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + ``` |
| 35 | +
|
| 36 | +2. Next, set up your AWS provider with the `provider` block. Here, you need to specify the region in which your resources will be created. |
| 37 | +
|
| 38 | + ``` |
| 39 | + provider "aws" { |
| 40 | + region = "eu-west-1" |
| 41 | + } |
| 42 | + ``` |
| 43 | +
|
| 44 | +3. Create a VPC using the `aws_vpc` resource. You need to specify a CIDR block for your VPC, as well as the `Name` tag with the value `Terraform VPC`. |
| 45 | +
|
| 46 | + ``` |
| 47 | + resource "aws_vpc" "demo_vpc" { |
| 48 | + cidr_block = "10.0.0.0/16" |
| 49 | +
|
| 50 | + tags = { |
| 51 | + Name = "Terraform VPC" |
| 52 | + } |
| 53 | + } |
| 54 | + ``` |
| 55 | +
|
| 56 | +4. Then, create two subnets within this VPC using the `aws_subnet` resource. Make sure to reference your VPC ID and set an appropriate CIDR block for each subnet. |
| 57 | +
|
| 58 | + ``` |
| 59 | + resource "aws_subnet" "public_subnet" { |
| 60 | + vpc_id = aws_vpc.demo_vpc.id |
| 61 | + cidr_block = "10.0.0.0/24" |
| 62 | + } |
| 63 | +
|
| 64 | + resource "aws_subnet" "private_subnet" { |
| 65 | + vpc_id = aws_vpc.demo_vpc.id |
| 66 | + cidr_block = "10.0.1.0/24" |
| 67 | + } |
| 68 | + ``` |
| 69 | +
|
| 70 | +5. Next, create an Internet gateway and attach it to your VPC with the `aws_internet_gateway` resource. |
| 71 | +
|
| 72 | + ``` |
| 73 | + resource "aws_internet_gateway" "igw" { |
| 74 | + vpc_id = aws_vpc.demo_vpc.id |
| 75 | + } |
| 76 | + ``` |
| 77 | +
|
| 78 | +6. Create a route table for your public subnet using the `aws_route_table` resource. This table will direct all traffic (`0.0.0.0/0`) to the Internet gateway. |
| 79 | +
|
| 80 | + ``` |
| 81 | + resource "aws_route_table" "public_rtb" { |
| 82 | + vpc_id = aws_vpc.demo_vpc.id |
| 83 | +
|
| 84 | + route { |
| 85 | + cidr_block = "0.0.0.0/0" |
| 86 | + gateway_id = aws_internet_gateway.igw.id |
| 87 | + } |
| 88 | + } |
| 89 | + ``` |
| 90 | +
|
| 91 | +7. Finally, associate the route table with your public subnet using the `aws_route_table_association` resource. |
| 92 | +
|
| 93 | + ``` |
| 94 | + resource "aws_route_table_association" "public_subnet" { |
| 95 | + subnet_id = aws_subnet.public_subnet.id |
| 96 | + route_table_id = aws_route_table.public_rtb.id |
| 97 | + } |
| 98 | + ``` |
| 99 | +
|
| 100 | +## Congratulations on Completing the Exercise! |
| 101 | +
|
| 102 | +Well done on successfully creating a basic infrastructure on AWS using Terraform. You've shown great understanding of Infrastructure as Code (IaC) and how to use Terraform to create and manage a VPC with subnets and Internet gateway on AWS. Keep up the good work and continue to expand your knowledge in this area. You're doing fantastic! |
0 commit comments