英文:
Separate GCS terraform backends to deploy multiple resources in respective projects
问题
我有三个GCP项目,我们称之为project-01
、project-02
和project-03
,我正在尝试使用单个terraform模块在这些项目中创建计算实例。下面是terraform.tfvars
文件:
compute_instances = {
compute_instance_01 = {
project_id = "project-01"
instance_name = "instance_01"
zone = "asia-south1-a"
machine_type = "e2-small"
}
compute_instance_02 = {
project_id = "project-02"
instance_name = "instance_02"
zone = "asia-south1-a"
machine_type = "e2-medium"
}
compute_instance_03 = {
project_id = "project-03"
instance_name = "instance_03"
zone = "asia-south1-a"
machine_type = "e2-large"
}
}
如何在单个backends.tf
文件中将terraform状态文件存储在各自项目中的远程GCS存储桶中?
英文:
I have three GCP projects let's say project-01
, project-02
and project-03
and I'm trying to create Compute Instance in those respective projects using single terraform module. Below is the terraform.tfvars
file
compute_instances = {
compute_instance_01 = {
project_id = "project-01"
instance_name = "instance_01"
zone = "asia-south1-a"
machine_type = "e2-small"
}
compute_instance_02 = {
project_id = "project-02"
instance_name = "instance_02"
zone = "asia-south1-a"
machine_type = "e2-medium"
}
compute_instance_03 = {
project_id = "project-03"
instance_name = "instance_03"
zone = "asia-south1-a"
machine_type = "e2-large"
}
}
How do I keep terraform state file in remote GCS bucket in their respective projects in single backends.tf
file ?
答案1
得分: 1
我不确定我是否正确理解您最终想要实现的目标,我猜想完全准确可能是不可能的,但可以做一些事情。
根据我最好的理解,可以通过运行terraform init
命令来定义Terraform状态文件的位置。
例如:
terraform init \
-backend-config=bucket="<<gcs bucket name>>" \
-backend-config=prefix="<<a path prefix inside the bucket>>"
假设backend.tf
文件如下所示:
terraform {
backend "gcs" {
}
}
因此,虽然可能不可能从一个terraform init/apply
命令集中创建多个Terraform状态文件,但可以可能:
- 在一个或另一个Terraform执行中选择要部署和不要部署的内容,或者可能更好的是
- 在一些变量中定义资源的名称(包括要部署的目标项目的标识符、要部署的资源的名称以及用于存储Terraform状态文件的目标存储桶/前缀)(在Terraform文件之外的bash中或在Terraform文件内部),因此在执行
init
时会选择正确的状态文件位置;在执行apply
时会计算正确的资源名称。
如果您想了解如何实现后者的更多提示,请告诉我,或者以上信息是否足够。
英文:
Not sure I understand correctly what you would like to achieve ultimately, and I guess it might not be possible to get that 100% accurately, but something can be done.
From the best of my understanding, one can define a Terraform state file location running the terraform init
command.
For example:
terraform init \
-backend-config=bucket="<<gcs bucket name>>" \
-backend-config=prefix="<<a path prefix inside the bucket>>"
Under assumption, that the backend.tf
file looks like:
terraform {
backend "gcs" {
}
}
So, while it might not be possible to create many terraform state files from one terraform init/apply
command set, it may be possible to
- choose what is to be deployed and what is not (deployed) in one or another terraform execution, or probably better to
- define names of resources (including identifiers of the target projects for deployment, names of resources to be deployed, and target buckets/prefixes for the terraform state files storage) in some variables (in bash outside of the terraform files and/or inside terraform files), so when the
init
is executed, the correct state file location is chosen; and when theapply
is executed, the correct resource names are calculated.
Let me know if you would like further hints on how to achieve the later, or the above information is enough.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论