将GCS Terraform后端分开,以在各自的项目中部署多个资源。

huangapple go评论72阅读模式
英文:

Separate GCS terraform backends to deploy multiple resources in respective projects

问题

我有三个GCP项目,我们称之为project-01project-02project-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=&quot;&lt;&lt;gcs bucket name&gt;&gt;&quot; \
        -backend-config=prefix=&quot;&lt;&lt;a path prefix inside the bucket&gt;&gt;&quot;

Under assumption, that the backend.tf file looks like:

terraform {
  backend &quot;gcs&quot; {
  }
}

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 the apply 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.

huangapple
  • 本文由 发表于 2023年7月3日 17:59:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定