terraform backend - ghdrako/doc_snipets GitHub Wiki
GCS
- Create the bucket you’ll be storing the state in
REGION=us-west1
gsutil mb -p $(gcloud projects list --format="value(project_id)" --filter="yourprojectname") -l $REGION gs://name-of-bucket-to-store-state
- Enable object versioning to avoid any corruption with your state file
gsutil versioning set on gs://name-of-bucket-to-store-state
- create a backend.tfvars
cho -e "bucket = \"$(gsutil ls | grep yourprojectname | awk -F '[/:]' '{print $4}')"\" | tee backend.tfvars
echo -e "prefix = \"terraform/state\"" | tee -a backend.tfvars
- Add this block to your terraform code
terraform {
backend "gcs" {}
}
- Use backend
terraform init -backend-config backend.tfvar