简体   繁体   English

Terraform 计划取 terraform 配置后

[英]Terraform plan after fetching terraform configuration

I use terraform with aws provider.我将 terraform 与 aws 提供程序一起使用。

It seems like terraform should has state file for updating resources.似乎 terraform 应该有 state 文件来更新资源。 When I commit my terraform configuration to git from one PC, and fetch it from another PC, terraform plan wants to create all resources, albeit resources were actually created on another PC.当我将我的 terraform 配置从一台 PC 提交到 git 并从另一台 PC 获取它时, terraform plan想要创建所有资源,尽管资源实际上是在另一台 PC 上创建的。 How to fix this without committing state file to git?如何在不将 state 文件提交到 git 的情况下解决此问题?

Terraform is Infra-as-Code which requires management of statefile. Terraform 是基础设施即代码,需要管理状态文件。 For AWS Cloud, the most popular choice is AWS S3 bucket.对于 AWS Cloud,最受欢迎的选择是 AWS S3 存储桶。 Here is more on terraform state management这是有关 terraform state 管理的更多信息

  1. Create an S3 bucket to store State files.创建一个 S3 存储桶来存储 State 文件。
  2. Ensure it has appropriate permissions确保它具有适当的权限
  3. In your Terraform code, reference the S3 bucket as backend在您的 Terraform 代码中, 将 S3 存储桶引用为后端

You could use Remote State in such a situation.在这种情况下,您可以使用远程 State If you are using AWS, you can use s3 backend to store the state in a bucket of your choice.如果您使用 AWS,则可以使用s3 后端将 state 存储在您选择的存储桶中。 Example from docs:来自文档的示例:

terraform {
  backend "s3" {
    bucket = "mybucket"
    key    = "path/to/my/key"
    region = "us-east-1"
  }
}

Terraform provides a lot of examples and use-case scenarios on how to setup and use such backend, and most importantly how to Protect Access to Workspace State . Terraform 提供了很多关于如何设置和使用此类后端的示例和用例场景,最重要的是如何保护对工作区的访问 State

Other backend are also possible, such as Terraform Cloud, Azure Blob Storage, Google Cloud Storage, Alibaba Cloud OSS, and more.其他后端也是可能的,例如 Terraform Cloud、Azure Blob Storage、Google Cloud Storage、阿里云 OSS 等。

Managing the backend state is very important.管理后端 state 非常重要。 Sounds like your.gitignore file is excluding your local state file?听起来您的 .gitignore 文件不包括您的本地 state 文件? Typically these lines are in.gitignore for terraform:通常这些行在 terraform 的 in.gitignore 中:

# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

If you take out the state file lines above, that means you will check them out on another PC & maintain the same state.如果您取出上面的 state 文件行,这意味着您将在另一台 PC 上检查它们并保持相同的 state。 But as mentioned, remotely managing your state files is ideal.但如前所述,远程管理您的 state 文件是理想的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM