我应该将.tfstate文件提交到Git吗? [英] Should I commit .tfstate files to Git?

查看:70
本文介绍了我应该将.tfstate文件提交到Git吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于是否将 .tfstate 文件提交给Git的问题,我有些困惑。 Terraform文档指出:

I am a little bit puzzled on the question whether to commit .tfstate files to Git or not. The Terraform documentation states:


Terraform也会默认将某些状态放入 terraform.tfstate 文件中。这个状态文件非常重要。它将各种资源元数据映射到实际资源ID,以便Terraform知道它正在管理什么。该文件必须保存并分发给可能运行Terraform的任何人。我们建议您简单地将其放入版本控制中,因为它通常不会太大。

Terraform also put some state into the terraform.tfstate file by default. This state file is extremely important; it maps various resource metadata to actual resource IDs so that Terraform knows what it is managing. This file must be saved and distributed to anyone who might run Terraform. We recommend simply putting it into version control, since it generally isn't too large.

现在,另一方面,已接受并在使用Terraform时的最佳做法中给出了答案:

Now, on the other hand, the accepted and upvoted answer on Best practices when using Terraform states:


Terraform配置可用于在不同基础架构上置备许多盒子,每个盒子可能具有不同的状态。由于它也可以由多个人运行,因此该状态应位于集中位置(例如S3),但不是 git。

(由原作者强调,而不是由我强调)

(Emphasis by the original author, not by me)

谁是正确的,如果是,为什么?

Who is right, and if so, why?

推荐答案

TL; DR:

重要! 存储在源代码管理中可能会暴露潜在敏感数据和风险针对旧版本的状态运行Terraform。

Important! Storing in source control could expose potentially sensitive data and risks running Terraform against an old version of state. Don't do it.

Terraform不再建议将状态存储在源代码管理中。您的好选项是远程的还是本地的。

Terraform no longer recommends storing state in source control. Your 'good' options are remote or local.

相对于本地和存储在源代码管理中,远程状态会带来明显的好处。这些详细信息如下。

Remote state grants significant benefits vs both local and storing in source control. Details of these are below.

原始答案:

叶夫根尼的答案很不错。现在,由于Terraform更新了文档以声明状态,因此该问题的争议性有所降低。

Yevgeniy's answer is a good one. The issue is somewhat less controversial now as Terraform have updated their docs to state:


Terraform还通过默认
。这个状态文件非常重要。它将各种
资源元数据映射到实际资源ID,以便Terraform知道它正在管理的
。该文件必须保存并分发给可能运行
的任何人。通常建议在使用Terraform时设置远程状态
这意味着状态文件中存储的所有潜在机密
都不会检入版本控制中

因此,既定的最佳做法与官方建议之间不再存在分歧。

So there is no longer a disagreement between established best practice and official recommendations.

2019年更新-05-17

文档的最新版本已更改为:


...此状态已存储默认情况下,该文件位于名为
terraform.tfstate的本地文件中,但也可以远程存储,这在团队环境中的工作效果更好。 ...

... This state is stored by default in a local file named "terraform.tfstate", but it can also be stored remotely, which works better in a team environment. ...

我不希望建议会恢复为源代码管理作为存储状态的首选方法。

I don't expect the advice will ever revert to source control being the preferred method of storing state.

尽管上面的文档引用了作为独立开发人员,远程状态仍然是有益的

Despite the docs quote above remote state is still beneficial as a solo developer

远程状态允许单独的开发人员执行以下操作:

Remote state allows the solo developer to:


  • 在多个设备上运行/运行Terraform代码

  • 根据所选的后端,轻松备份并防止丢失状态文件

  • 通过输出

  • 自动加密静态状态文件,具体取决于所选的后端

  • Work on/run their Terraform code from several devices
  • Easily backup and protect against losing the state file, depending on backend chosen
  • Segregate sections of their architecture via outputs
  • Automatically encrypt state file at rest, depending on backend chosen

这篇关于我应该将.tfstate文件提交到Git吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆