6

Manage Terraform State on Google Cloud Storage(GCS)

 2 years ago
source link: https://www.pkslow.com/archives/terraform-gcs-en
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
技术之前,先读诗书:

调角断清秋,征人倚戍楼。

It's a best pratice to manage Terraform state on central storage like GCS for GCP.

We need to create a Bucket to store the state files:

$ gsutil mb -p pkslow -l us-west1 -b on gs://pkslow-terraform
Creating gs://pkslow-terraform/...

$ gsutil ls gs://
gs://pkslow-terraform/

Define the backend "gcs" in Terraform code:

terraform {
  backend "gcs" {
    bucket  = "pkslow-terraform"
    prefix  = "state/gcp/pubsub"
  }
}

Initiate the Terraform project, it will help to create folder in the Bucket:

$ terraform init -plugin-dir=${TERRAFORM_PLUGIN}

Apply the change:

$ terraform apply -auto-approve

Check on the GCP Console, the state file is created:

What's more, we can also fetch the data from the remote state:

data "terraform_remote_state" "foo" {
  backend = "gcs"
  config = {
    bucket  = "terraform-state"
    prefix  = "prod"
  }
}

resource "template_file" "bar" {
  template = "${greeting}"

  vars {
    greeting = "${data.terraform_remote_state.foo.greeting}"
  }
}

Code on GitHub: https://github.com/LarryDpk/pkslow-samples


Reference:

Manage GCP Pubsub with Terraform

Terraform GCS


欢迎关注微信公众号<南瓜慢说>,将持续为你更新...

推荐阅读:
如何制定切实可行的计划并好好执行
容器技术(Docker-Kubernetes)
SpringBoot-Cloud相关
Https专题


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK