Create and Manage AWS VPC as Infrastructure as Code with Terraform with Static Data

Step1 :Download and Install terraform from https://www.terraform.io/downloads.html

Step2 :Verify that the installation worked by opening a new terminal

terraform -help

Step3 : Download and install AWS CLI from https://awscli.amazonaws.com/AWSCLIV2.msi

Step4 :Configure the AWS CLI from your terminal,Follow the prompts to input your AWS Access Key ID and Secret Access Key.

aws configure

Step 6:provider.tf

# Configure the AWS Provider
provider "aws" {
  #region = "ap-south-1"
  region=var.region
}

# Create a VPC
resource "aws_vpc" "main" {
  #cidr_block = "190.160.0.0/16"
  cidr_block=var.vpc_cidr
  instance_tenancy="default"
  tags= {
  Name="main"
  Location="ND"
  } 
}

# Create a SUBNET
resource "aws_subnet" "subnets" {
  #count=length(var.azs)
  count=length(data.aws_availability_zones.azs.names)
  availability_zone=element(data.aws_availability_zones.azs.names,count.index)
  vpc_id = aws_vpc.main.id
  #cidr_block = "190.160.1.0/24"
  #cidr_block=var.subnet_cidr
  cidr_block=element(var.subnet_cidr,count.index)
  tags= {
  Name=format("Subnet-%03d", count.index + 1)
 
  }
}

Step 6: Initialize the directory

Initializing a configuration directory downloads and installs the providers defined in the configuration, which in this case is the aws provider.

terraform init

Step 7:Create infrastructure

terraform apply