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

Packer Environment Variable

Note:  1011  export AWS_ACCESS=AKGQ                         do this on console not on editor  1012  export AWS_SECRET=KfVkt1aurid  1013  echo $AWS_ACCESS  1014  AKVUGQ   vim moon.json {     "variables": {       "aws_access_key": "{{env `AWS_ACCESS`}}",       "aws_secret_key": "{{env `AWS_SECRET`}}"     },        "builders": [            {            "type": "amazon-ebs",            "region": "us-east-1",            "access_key": "{{user `aws_access_key`}}",            "secret_key": "{{user `aws_secret_key`}}",            "instance_type": "t2.micro",            "source_ami": "ami-02e136e904f3da870", ...

copying snap from one REGION to another

 while copying snap from one to another we need to change region provider "aws" {   region     = "us-west-1"   access_key = "AGQ"   secret_key = "Kurid" } resource "aws_ebs_snapshot_copy" "example_copy" {   source_snapshot_id = "snap-08bc5c27dad8e82b3"   source_region      = "us-east-1"   tags = {     Name = "HelloWorld_copy_snap"   } }

File Provisioner

 Note: file provisioner is used to copy file we have created one file vim clod now how to checck its been copied  go to ami and launch image and create instance  vi pro.json {        "builders": [            {            "type": "amazon-ebs",            "region": "us-east-1",            "access_key": "AUGQ",            "secret_key": "Kurid",            "instance_type": "t2.micro",            "source_ami": "ami-02e136e904f3da870",            "ssh_username": "ec2-user",            "ami_name": "moon-amiii"            }        ],        "provisioners": [                {   ...