How to Create a VPC and a Subnet in AWS using Terraform 2021


At first create a IAM user in AWS and give it administrative access. Collect the access and secret key. Then create a file named Add your region as default value


variable "aws_region" {
description = "AWS region"
type = string
default = "us-east-2"

 Then create another file called and add the aws_vpc resource and aws_subnet resource. Change the value according to your configuration.

provider "aws" {
region = var.aws_region
access_key = "your-iam-user-access-key"
secret_key = "your-iam-user-secret-key"

resource "aws_vpc" "dev-vpc" {
cidr_block = ""
instance_tenancy = "default"
enable_dns_hostnames = "true"
tags = {
Name = "dev-vpc"

output "vpc_id" {
value =

#create a private subnet for aws using the vpc_id
resource "aws_subnet" "dev-subnet" {
vpc_id =
cidr_block = ""
availability_zone = "us-east-2a"
tags = {
Name = "dev-subnet-1"

output "subnet_id" {
value =

Then Run 

terraform init

terraform plan

terraform apply





Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment


Post a Comment