在创建我的基础架构的其余部分之前,如何使用 Terraform 设置远程后端存储桶? (全球控制点)

How to set up a remote backend bucket with Terraform before creating the rest of my infrastructure? (GCP)

我如何首先使用 Gitlab 的管道在 GCP 上初始化 Terraform 的后端状态桶,然后然后我的基础设施的其余部分?我发现 this 但不确定 Gitlab 的管道意味着什么。

这一直是个难题。我的 post 不会直接回答您的问题,但会给出我对该主题的看法。 (太长无法评论)

这有点像要求使用相同的 CI 工具管理您拥有 CI 工具的服务器(例如:gitlab 服务器自行管理)。

如果您使用 gitlab CI 创建您的存储库,您将无法保留状态,因为您没有远程状态来为这个 specific 任务存储它。这意味着您将拥有与 tf 不一致的资源,但没有状态。

如果您想将它与您的 CI 集成,我建议您在 ci 中使用 gcloud cli,检查 gcs 是否存在,如果不创建它。

如果您真的想使用 terraform,可以使用带有远程后端的 terraform cloud 免费层,仅用于此 specific 资源。像这样,您拥有由 tf 管理的所有资源,并且都具有 tfstate。

您现在有另一个选择,不涉及 GCP,GitLab 13.0 (May 2020)

GitLab HTTP Terraform state backend

Users of Terraform know the pain of setting up their state file (a map of your configuration to real-world resources that also keeps track of additional metadata).
The process includes starting a new Terraform project and setting up a third party backend to store the state file that is reliable, secure, and outside of your git repo.

Many users wanted a simpler way to set up their state file storage without involving additional services or setups.

Starting with GitLab 13.0, GitLab can be used as an HTTP backend for Terraform, eliminating the need to set up state storage separately for every new project.

The GitLab HTTP Terraform state backend allows for a seamless experience with minimal configuration, and the ability to store your state files in a location controlled by the GitLab instance.
They can be accessed using Terraform’s HTTP backend, leveraging GitLab for authentication.
Users can migrate to the GitLab HTTP Terraform backend easily, while also accessing it from their local terminals.

The GitLab HTTP Terraform state backend supports:

  • Multiple named state files per project
  • 锁定
  • 对象存储
  • 静态加密

It is available both for GitLab Self-Managed installations and on GitLab.com.

documentation and issue


此外,该提供商将在可预见的未来得到支持,GitLab 13.4(2020 年 9 月):

Taking ownership of the GitLab Terraform provider

We’ve recently received maintainer rights to the GitLab Terraform provider and plan to enhance it in upcoming releases.
In the past month we’ve merged 21 pull requests and closed 31 issues, including some long outstanding bugs and missing features, like supporting instance clusters.

You can read more about the GitLab Terraform provider in the Terraform documentation.

See Documentation and Issue.