How to create AWS VPC with Terraform

Sabina Akter
4 min readJul 21, 2022

Terraform is an open-source infrastructure as code software tool that enables you to safely and predictably create, change, and improve infrastructure. Terraform manages low-level components like compute, storage and networking resources and also manages high-level components like DNS entries and SaaS features.

Let’s create AWS VPC with two privates and public subnets along with Internet Gateway and NAT Gateway.

We will create below resources:

  1. AWS VPC with CIDR 10.0.0.0/16
  2. 2 public subnets and 2 private subnets
  3. Internate Gateway
  4. Routing Table for public and private
  5. Elastic IP for NAT gateway
  6. NAT Gateway
  7. Associate Route Tables
  8. Default security group for VPC

Please see the GitHub repository here:

Requirements:

  • AWS Account
  • VS Code install in the computer and know how to use it

Step 1:

  • Open VS Code and open cli
  • Create a desktop folder name terraform-vpc

cli command: mkdir terraform-vpc

Step 2:

Change dir and create 3 files using touch command

cli command: touch main.tf vars.tf output.tf

Note:

  • main.tf is the main file that we define cloud provider and resources we want to create
  • output.tf file will stores outputs from terraform apply
  • vars.tf file will store variables

Step 3:

Edit main.tf file and write the below configuration and save it.

Step 4:

Edit vars.tf file and write below configuration and save it.

Step 5:

Edit output.tf file and write below configuration and save it.

That’s all we need to do to create all the necessary files. Now we need to create resources in AWS

Step 6:

Run terraform init to initialize the dir.

Step 7:

Run terraform plan to check resources before creating.

Step 8:

Run terraform apply to create resources in your AWS.

Step 9:

Check the output’s of terraform apply run terraform output.

Now you can see all the details like VPC id, NAT Gateway, Subnet id, Security group id etc.

Step 10:

Finally we need to log in our AWS console and verify all the resources has been created successfully in AWS. You can see the below VPC dashboard.

Congratulations!! Great job :)

“You have successfully created AWS VPC , 2 public subnets, 2 private subnets,( on the dashboard, I have 2 other subnets, that is why it shows total 6 subnets), Internate Gateway, Routing Table for public and private, Elastic IP for NAT gateway, NAT Gateway, Associate Route Tables & Default security group for VPC”. Don’t forget to run terraform destroy to clean up your work :)

Happy Learning!!

Troubleshooting:

When I have run the command terraform apply in the VS Code terminal, it didn’t recognize terraform, error was “ Terraform Command Not Found”. I was not aware of it, I didn’t install terraform in this Mac laptop. Then I have installed terraform plugin for VS Code and also in the terminal update home brew and install terraform. You can follow the docs here.

--

--

Sabina Akter
Sabina Akter

Written by Sabina Akter

Infrastructure Engineer/Cloud Engineer at Fox

No responses yet