Skip to main content

TERRAFORM STATE MANAGEMENT S3

NOTE:

make three directory 

mkdir project1  - handled by harry

mkdir project1     - danish
mkdir project1    - moon
vim aws,tf
make three instances
multiple ppl are working on project1
harry wrote script
now danish want to add more so he will copy code from harry and do changes   
cp /root/project_a/aws.tf .
once danish will try to add some changes but it didnt got backup file. 
so it will craete new setup because in his local system state file was NOT existed
if danish want to work on same file then he need tfstate file as well
cp /root/project_a/aws.tf .
cp /root/project_a/terraform.tfstate .
store tfstste file in remote 

search terraform s3 remote

create s3 bucket
terraform {
  backend "s3" {
    bucket = "mybucket"--- bucketname
    key    = "abid/moon" inside bucket it will create two folders  abid -- inisde -- moon
    region = "us-east-1"
    access_key =
    secret_key =
    dynamodb_table   = "lockup"
  }
}


now use locking fearture 
craete yable in dynamo bd
table name : moon
primary key: LockID    --- capital L  ID
all users should update in code dynamo code line 
again do terraform init - we have done changes


provider "aws" {

  region     = "us-east-1"

  access_key = "AUGQ"

  secret_key = "Kt1aurid"

}




terraform {

  backend "s3" {

    bucket = "mybucket2022"

    key    = "mybucket2022pro/moon"

    region = "us-east-1"

    access_key = "AKIARUPJBF7JN6BKVUGQ"

    secret_key = "KfU0tFpao0b+BODvc+GG63xN99jTMdfVkt1aurid"

    dynamodb_table   = "myproject2022"


  }

}



resource "aws_instance" "web" {

  ami           = "ami-02e136e904f3da870"

  instance_type = "t3.nano"


  tags = {

    Name = "imoon"

  }

}



resource "aws_instance" "tf" {

  ami           = "ami-02e136e904f3da870"

  instance_type = "t3.nano"


  tags = {

    Name = "diib"

  }

}





 


Comments

Popular posts from this blog

Jenkins Terraform Integration | How do you integrate Terraform with Jenkins | Automate Infrastructure setup using Terraform and Jenkins Pipeline

  Pre-requistes: Jenkins  is up and running Terraform  is installed in Jenkins Terraform files already created in your SCM Make sure you have necessary IAM role created with right policy and attached to Jenkins EC2 instance. see below for the steps to create IAM role. I have provided  my public repo  as an example which you can use. Create IAM role to provision EC2 instance in AWS  Select AWS service, EC2, Click on Next Permissions Type EC2 and choose AmazonEC2FullAccess as policy Click on Next tags, Next Review give some role name and click on Create role. Assign IAM role to EC2 instance Go back to Jenkins EC2 instance, click on EC2 instance, Security, Modify IAM role Type your IAM role name  my-ec2-terraform-role  and Save to attach that role to EC2 instance. Create a new Jenkins Pipeline Give a name to the pipeline you are creating. Add parameters to the pipeline Click checkbox - This project is parameterized, choose Choice Parameter Enter name...

TIME-STAMP

NOTE: whenver we start provisioner we have to define ami_name always other it will give error  example we have already defined to install git . later i need to install tree also once i define my command and  try to buid it it give give error ami_name exist  so the solution is timestamp that we need to define in  ami_name: "dev-team-{{timestamp}}" {        "builders": [            {            "type": "amazon-ebs",            "access_key": "AVUGQ",            "secret_key": "KfVkt1aurid",            "region": "us-east-1",            "instance_type": "t2.micro",            "source_ami": "ami-02e136e904f3da870",            "ssh_username": "ec2-user",            "ami_name": "moonu-amiii-...

AWS KEY PAIR , vpc, Elastic-key, Security-group

 NOTE: once we make-instance it doesn't provide elastic IP, security group and key pair. go to docs  copy key pair code  go to the main console type ssh-keygen  type key name afreen  enter twice ls vim afreen.pub copy key  and paste in code     vim key.tf  provider "aws" {   region     = "us-east-1"   access_key = "AKIARUPJBFN6BKVUGQ"   secret_key = "KfU0tFpao0bDvc+GG63xN99jTMdfVkt1aurid" } resource "aws_instance" "web" {   ami           = "ami-02e136e904f3da870"   instance_type = "t2.micro"   key_name      = "afreen"   vpc_security_group_ids  = [aws_security_group.apple.id]   tags = {     Name = "HelloWorld"   } } resource "aws_key_pair" "moon" {   key_name   = "afreen"   public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCms+0pf8D2EEtdnVA3v+zc/P7ztVP/DrmdKOB1ZRfy2NYQ7faxKTuvVrNdzKvEdyxmnwK3+/+PrYL...