Terraform-Backend

Venkateswara Reddy - Dec 9 '23 - - Dev Community

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "< 5.0.0"
    }
  }
  backend "s3" {
    bucket         = "venkat1-new" 
    key            = "global/s3/terraform.tfstate"    
    region         = "ap-south-1"           
    encrypt        = true                   
    dynamodb_table = "s3-backend"    
  }
}

provider "aws" {
  access_key = "AKIAXMZNJW6PVJQBTD5Z"
  secret_key = "o9eq86aMxJqNmM7NN7shz1peb7BM6hz8hyCf8y9u"
  region     = "ap-south-1"
}

resource "aws_vpc" "venky_vpc" {
  cidr_block           = "10.0.0.0/16"
  enable_dns_support   = true
  enable_dns_hostnames = true

  tags = {
    Name = "venky-vpc"
  }
}

resource "aws_subnet" "venky_subnet_1" {
  vpc_id           = aws_vpc.venky_vpc.id
  cidr_block       = "10.0.8.0/24"
  availability_zone = "ap-south-1a"

  tags = {
    Name = "venky_subnet_1"
  }
}

resource "aws_security_group" "venky_sg" {
  name_prefix = "venky-sg"

  vpc_id = aws_vpc.venky_vpc.id

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port   = 0
    to_port     = 65535
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "venky_sg"
  }
}

resource "aws_internet_gateway" "venky_IGW" {
  vpc_id = aws_vpc.venky_vpc.id

  tags = {
    Name = "venky_IGW"
  }
}

resource "aws_route_table" "venky_route_table" {
  vpc_id = aws_vpc.venky_vpc.id

  tags = {
    Name = "venky_route_table"
  }
}

resource "aws_route" "venky_internet_route" {
  route_table_id         = aws_route_table.venky_route_table.id
  destination_cidr_block = "0.0.0.0/0" # This is the default route for internet traffic
  gateway_id             = aws_internet_gateway.venky_IGW.id
}

resource "aws_route_table_association" "venky_subnet_association" {
  subnet_id      = aws_subnet.venky_subnet_1.id
  route_table_id = aws_route_table.venky_route_table.id
}

resource "aws_instance" "venky_ec2" {
  ami           = "ami-0f5ee92e2d63afc18"
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.venky_subnet_1.id
  vpc_security_group_ids = [aws_security_group.venky_sg.id]
  associate_public_ip_address = true

  tags = {
    Name = "venky_ec21"
  }
}


Enter fullscreen mode Exit fullscreen mode

1st we have to create main.tf then we have to apply the command terraform.apply then backend state file create in the local.
In the cloud we have to create the (we can use same user what ever we are using for terraform) user for accessing the s3 bucket with ‘put object’ and ‘get object’…….
Create one dynamodb table with primary key “LockID”

Image description

In the terraform file we have to maintain s3 bucket name then path in the s3 then dynamodb name.

Image description

Here if anyone try to apply “terraform apply” then it will throw error like above why because already some one is using from different laptop…..
In the script we have to mention “encrypt=true” then s3 bucket name and dynamodb name…… then lock will enable..
Without dynamodb we cant set lock to the terraform.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Modules:

First In any directory create main.tf, variables.tf, variables.tfvars, providers.tf, backend.tf, output.tf
Based on the requirements we can create above file. Ofter try to play with these filea are correct or not by applying “terraform apply --auto-approve”.
If every thing is fine then we can use this directory as a module.

Create new file with main.tf in the other location where ever we want and mention the module path then intialise that then we can use what ever the resources we mentioned in the main.tf we can create by running this main.tf.



provider "aws"{

}
module "ec2_instance"{
    #source= "./modules/ec2_instance"
    #source= "C:\Users\yeduru.vr\Desktop\New folder"
    source= "C:/Users/yeduru.vr/Desktop/New folder"
    region     = "ap-south-1"
    access_key = "AKIAXMZNJW6PVJQBTD5Z"
    secret_key = "o9eq86aMxJqNmM7NN7shz1peb7BM6hz8hyCf8y9u"
    
}


Enter fullscreen mode Exit fullscreen mode

source= "./modules/ec2_instance"

Here if we are in the current directory then we have to mention like this


    source= "C:/Users/yeduru.vr/Desktop/New folder"
Enter fullscreen mode Exit fullscreen mode

if we are using the different directory module we have mention like this.
Here mainly we have to mention the module path.


module "ec2_instance"{
Enter fullscreen mode Exit fullscreen mode

Here we cn use any name in place of ec2_instance like “venkatesh” that is just module name.
Mostly we will mention like ec2 and vpc then eks then s3 like that to understsnd.

Image description

Rahul wagh blogs for modules: https://jhooq.com/terraform-module/

1.In this file only we can use any no.of modules..like for eks or any other services
2.Here all modules are already create for like to create eks,vpc,s3,rds,ec2… so many modules will be there just we have to mention those modules what ever we are planning tho create then apply.
3.

Image description

4.We can inbuild modules also for creating any services like ec2,vpc….. we have to search in the hasicorp organization for modules.
5.It is just like docker images if we want to use existing modules we can use and we can create custom modules also based on the requirements.
6.

. . . . . . . . . . . . .