AWS VPC with Terraform
Provisioning an AWS VPC (Virtual Private Cloud) using Terraform involves defining the desired infrastructure as code and then using Terraform to create and manage those resources on AWS.

Perquisites:
Installed to be on your Local Machine.
For Installing All for this , Follow the Official Document of that tools.
Terraform VS Code Aws CLI Aws Account
Install Terraform
You have to install Terraform on your local machine. You can download it from the official Terraform website (https://www.terraform.io/downloads.html) and follow the installation instructions for your operating system.
Configure AWS Credentials:
Then we need to create a user on the AWS and after creating the user , generate the Access & Secrete Access Key, for the connecting the AWS with Terraform.
after Creating the user we need to some access/permissions to this user. creating & managing the terraform permissions or provide the aws Administrator permissions/access.
aws configure

provider.tf
The provider.tf
file is used to define the provider configuration. A Terraform provider is responsible for managing the lifecycle of a particular type of infrastructure, such as AWS, Azure, Google Cloud, etc. It specifies the details of the cloud provider you are using and any necessary authentication or configuration parameters.

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
# Configure the AWS Provider
provider "aws" {
region = "us-east-2"
}
main.tf
The main.tf
file is typically the main entry point for your Terraform configuration. It contains the resource definitions and configurations necessary for your infrastructure. In the context of provisioning AWS EKS with Terraform, it would include the definition of your EKS cluster, associated networking resources, and any other dependencies.


#Step 1 : create VPC
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name= "MyTerraform_VPC"
}
}
#Step 2 : create public subnet
resource "aws_subnet" "Public_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
}
#Step 3 : create private subnet
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.2.0/24"
}
#Step 4 : Create IGW
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.my_vpc.id
}
#Step 5 : route table for publc subnet
resource "aws_route_table" "public-rtb"{
vpc_id = aws_vpc.my_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
}
#Step 6 : route table association public subnet
resource "aws_route_table_association" "public_Association"{
subnet_id = aws_subnet.Public_subnet.id
route_table_id = aws_route_table.public-rtb.id
}
Terraform init
Terraform init
Run the command in your Terraform project directory to initialize the project. This downloads the necessary provider plugins and sets up the working directory.

terraform.lock.hcl
File:
terraform.lock.hcl
File:The terraform.lock.hcl
file is related to Terraform modules and is used to pin module versions. It's created automatically when you use the terraform init
command to initialize a Terraform configuration that uses modules.
terraform validate
terraform validate
The terraform validate
command checks the syntax and structure of your Terraform configuration files. It ensures that your configurations are correctly written and that all the necessary providers and modules are available.

terraform plan
terraform plan
The terraform plan
command is used to create an execution plan. It provides a preview of the changes that Terraform will make to your infrastructure based on your configuration. This is a critical step before actually applying any changes to your infrastructure.



terraform apply
terraform apply
Apply the Terraform configuration to create the AWS resources. This step might take some time as it provisions the EKS cluster, associated networking resources, and other dependencies.


Output- VPC Created

Created subnets

Created route table

Created Internet Gateway

terraform destroy
terraform destroy
This Command for destroy all of those resources , which was created by terraform.


Last updated