V6-9 Terraform State - Terraform Lifecycle Rules -Importing Resources

V6-9 Terraform State - Terraform Lifecycle Rules -Importing Resources

 V6-9 Terraform State - Terraform Lifecycle Rules -Importing Resources






Terraform State

Creating an instance  

  provider.tf file contents
  provider "aws" {
  region     = "us-west-2"
  access_key = "xyz"
  secret_key = "xyz"
}

main.tf file contents
resource "aws_instance" "web1" {
  ami           = "ami-083ac7c7ecf9bb9b0"
  instance_type = "t2.micro"
}

resource "aws_instance" "web2" {
  ami           = "ami-083ac7c7ecf9bb9b0"
  instance_type = "t2.micro"
}

resource "aws_s3_bucket" "bucket1" {
  bucket = "my-tf-test-bucketxyzdf"            #give the unique name of bucket
}


Terraform Remote State

Creating an instance & Storing state in remote backed  

  provider.tf file contents
  
 
 provider "aws" {
  region     = "us-west-2"
  access_key = "xyz"
  secret_key = "xyz"
}

terraform {
  backend "s3" {
    bucket = "urbacketname"                          #ur created bucket name                  
    key    = "terraform.tfstate"
    region = "us-west-2"
    access_key = "xyz"
    secret_key = "xyz"
    dynamodb_table ="tf_state_lock"
  }
}

main.tf file contents
resource "aws_instance" "web1" {
  ami           = "ami-083ac7c7ecf9bb9b0"
  instance_type = "t2.micro"
}

resource "aws_s3_bucket" "bucket1" {
  bucket = "my-tf-test-bucket345672"              #unique name
}

Create another folder dev2/dev3 as showed in the video , copy same conf files, and add the web2 instance code and test it

resource "aws_instance" "web2" {
  ami           = "ami-083ac7c7ecf9bb9b0"
  instance_type = "t2.micro"
}

Terraform Lifecycle Rules

provider.tf file contents
  
 
 provider "aws" {
  region     = "us-west-2"
  access_key = "xyz"
  secret_key = "xyz"
}
main.tf file contents
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"

}

resource "aws_subnet" "Subnet1" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"

  tags = {
    Name = "Subnet1"
  }

}

resource "aws_subnet" "Subnet2" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.3.0/24"

  tags = {
    Name = "Subnet2"
  }
  lifecycle {
    create_before_destroy = true
       ignore_changes = [

      tags,
    ]
  }
}

Importing Resources into tf state

you can manually create instance and try to import using tf import command, which we have discussed in the video

Task1

main.tf file contents
  
 resource "aws_instance" "web" {

}

resource "aws_s3_bucket" "mybucket" {
  
}
 

Task2

main.tf file contents
  
resource "aws_instance" "web1" {
  ami           = "ami-083ac7c7ecf9bb9b0"
  instance_type = "t2.micro"
}

resource "aws_instance" "web2" {
  ami           = "ami-083ac7c7ecf9bb9b0"
  instance_type = "t2.micro"
}
 

Presentation Link : Terraform_V6-9_TFState_Lifecyclemgmt_Import

Post a Comment

1 Comments

  1. Casino City Offers Free Spins & Bonus Codes December 2021
    With so many casinos accepting players from Australia, the best 하남 출장안마 free spins and casino bonuses are available to play right 서귀포 출장안마 now. 하남 출장샵 This casino 구미 출장샵 offers 구리 출장샵

    ReplyDelete