DEV Community

Cover image for AWS: Create VPC using Terraform CLI
Oladipupo Abeeb Olanrewaju
Oladipupo Abeeb Olanrewaju

Posted on

AWS: Create VPC using Terraform CLI

Amazon Virtual Private Cloud (VPC)

Amazon Web Services (AWS) provides Virtual Private Cloud (VPC) as a service to create a private, isolated network within the AWS Cloud. Using a VPC, you can launch resources such as EC2 instances, RDS databases, and S3 buckets within a virtual network that you control. In this blog, we will walk you through creating an AWS VPC using Terraform on VSCode.

  1. It is a private Network
  2. It can launch resources like EC2 instances in VPC
  3. VPC can isolate and protect resources
  4. It spans across availability zones in a region

Configure Terraform CLI on VScode

Download the Terraform CLI from VScode extension or download it manually through this HashiCorp Terraform. After the installation; create a folder 📂 Terraform where all your terraform files will be accessed and saved on. In your folder, create a file named Providers.tf this is where your Aws resources will be accessed by Aws provider.
The region contains the location where your data center will be.
Profile is the name of your AWS account

Configure AWS
After creating the file, run terraform init in your terminal to initialize Terraform configuration.

Terraform init

After this process; create a new file with the .tf extension, e.g., main.tf. In the file, write the Terraform code to create a VPC
Terraform code
In the code above, we defined the provider aws with the region set to us-east-1, which is the region where we want to create the VPC. We also created a VPC resource with the cidr_block set to 10.0.0.0/16 and the Name tag set to main_vpc.

Save the file.

Open the terminal in VSCode and run the command terraform apply to create the VPC. Terraform will show you a preview of the changes that will be made, and if you're happy with them, type yes to confirm and apply the changes.

Terraform Apply
VPC confirmed

Once the command finishes executing, you will have a VPC created on your AWS account.

We started by configuring the Terraform CLI and then created a new Terraform project to create a VPC. We hope that you found this blog helpful and that it serves as a starting point for creating more complex infrastructures on AWS using Terraform.

Top comments (0)