通过Google Cloud Storage(GCS)管理Terraform的状态State - 南瓜慢说
source link: https://www.cnblogs.com/larrydpk/p/17052867.html
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.
通过Google Cloud Storage(GCS)管理Terraform的状态State
管理Terraform状态文件的最佳方式是通过云端的统一的存储,如谷歌云就用GCS。
首先要创建一个Bucket:
$ gsutil mb -p pkslow -l us-west1 -b on gs://pkslow-terraform
Creating gs://pkslow-terraform/...
$ gsutil ls gs://
gs://pkslow-terraform/
然后在Terraform文件中配置对应的信息:
terraform {
backend "gcs" {
bucket = "pkslow-terraform"
prefix = "state/gcp/pubsub"
}
}
初始化后,就会在Bucket上创建对应的目录:
$ terraform init -plugin-dir=${TERRAFORM_PLUGIN}
变更生效:
$ terraform apply -auto-approve
我们在浏览器查看一下,发现已经成功状态了对应的状态文件:
通过远程的云端,不仅可以存入状态文件,也可以从状态文件读取数据,如一些输出变量。比如模块A创建了一个VM,而我们可能通过这种方式获取它的IP,以便在其它模块使用。大致的配置如下:
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}"
}
}
代码请查看GitHub: https://github.com/LarryDpk/pkslow-samples
Reference:
Recommend
-
7
最近工作中用到了Terraform,权当学习记录一下,希望能帮助到其它人。 Terraform系列文章如下: Terraform入门教程,示例展示管理Docker和Kubernetes资源
-
2
《Terraform 101 从入门到实践》这本书只将在南瓜慢说官方网站和GitHub两个地方同步更新,如果你在其它地方看到,那应该就是抄...
-
2
Spring Boot通过Actuator显示git和build的信息 为了更好的版本控制和问题定位,我们...
-
7
Terraform是管理许多平台的基础设施的工具,如AWS、GCP和Azure。这篇文章将讲解如何通过Terraform来管理GCP Pub/Sub。 创建GCP项目请参考:初始化一个GCP项目并用gcloud访问操作...
-
5
DB2是IBM的一款优秀的关系型数据库,简单学习一下。 2 Docker安装DB2 为了快速启动,直接使用Docker来安装DB2。先下载镜像如下: docker pull ibmcom/db2:11.5.0.0 启动数据库如下:
-
3
《Terraform 101 从入门到实践》这本小册在南瓜慢说官方网站和GitHub两个地方同步更新,书中的示...
-
4
《Terraform 101 从入门到实践》这本小册在南瓜慢说官方网站和GitHub两个地方同步更新,书中的示...
-
4
《Terraform 101 从入门到实践》这本小册在南瓜慢说官方网站和GitHub两个地方同步更新,书中的示...
-
4
《Terraform 101 从入门到实践》这本小册在南瓜慢说官方网站和GitHub两个地方同步更新,书中的示...
-
3
开发了一个Java库的Google Bard API,可以自动化与AI对话了 Google Bard是Google提供的还在实验阶段的人工智能对话服务。这明显是对标ChatGPT来的,它可以提供更实...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK