Terraform state files Migaration between aws and Gcp Cloud
Migrating Terrafrom State files from one cloud to other cloud
“Terraform knows wheter a resource is created or destroyed or change from the statefile.just like human has heart terraform has state files.
step1: configure aws and Gcp cloud providers
create the aws user and get credentials,and get create a service account in gcp and get credentails .
provider “aws” {
region = “us-east-1”
access_key = “”
secret_key = “”
}
provider “google” {
project = “kethan-3915”
region = “europe-west-2”
credentials = file(“./key.json”)
}
Create s3 and Gcs buckets
resource “aws_s3_bucket” “hello” {
bucket = “kenade77777”
acl = “private”
tags = {
Name = “My bucket”
Environment = “Dev”
}
}
resource “google_storage_bucket” “mybucket” {
name = “kenade999999”
location = “asia”
storage_class = “COLDLINE”
}
create a folder and intilize it as project folder
Do terraform init
when we do terraform init
the statefile will be present in terrform.tfstate
Lets move the state file to remote locations
we can see in the below image the statefile will be moved from Local Machine to S3 bucket in Remote backend
terraform {
backend “s3” {
bucket = “kenade77777”
key = “remotestate”
region = “us-east-1”
access_key = “”
secret_key = “”
dynamodb_tablename = “mytable777”
}
}
Do terraform init
when we do terraform init we can see the statefile will be migarated to remote backend as shown in above sceenshot
now the statefile is present in s3 bucket .we can move this statefile to gcp bucket.we cannot have the same state file at a time in two cloud enviroments so comment the previous remote backend with /* */
now add the new remote backend that is GCS
DO Terraform init
terraform {
backend “gcs” {
bucket = “kenade999999”
prefix = “myfile/state”
credentials = “key.json”
}
}
Now we can see the statefile from s3 will be copied to GCS bucket
Now we can see the satefile in GCS bcuket.thats how we can migrate the statefiles between two clouds remote backends.