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 variables.tf. Add your region as default value

 

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

 Then create another file called main.tf 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"
}

#create_a_vpc
resource "aws_vpc" "dev-vpc" {
cidr_block = "192.168.0.0/16"
instance_tenancy = "default"
enable_dns_hostnames = "true"
tags = {
Name = "dev-vpc"
}
}

output "vpc_id" {
value = aws_vpc.dev-vpc.id
}

#create a private subnet for aws using the vpc_id
resource "aws_subnet" "dev-subnet" {
vpc_id = aws_vpc.dev-vpc.id
cidr_block = "192.168.0.0/20"
availability_zone = "us-east-2a"
tags = {
Name = "dev-subnet-1"
}
}

output "subnet_id" {
value = aws_subnet.dev-subnet.id
}

Then Run 

terraform init

terraform plan

terraform apply

 

 


 

 

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

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

0 comments:

Post a Comment