我应该将 .tfstate 文件提交到 Git 吗?
Should I commit .tfstate files to Git?
关于是否将.tfstate
文件提交到Git 的问题,我有点疑惑。 Terraform documentation 状态:
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 config can be used to provision many boxes on different infrastructure, each of which could have a different state. As it can also be run by multiple people this state should be in a centralised location (like S3) but not git.
(原作者强调,非本人强调)
谁是对的,如果是,为什么?
这可能会归结为偏好,但我会说 git(或任何其他源代码控制)不是存储状态文件的特别好的选择,因为它们是您代码的输出写的很像编译的二进制文件,甚至是最小化的 JS 或 LESS 编译为 CSS.
最重要的是,状态文件中的内容可能会迅速变化,作为内容的输出 运行 而不是代码中实际更改的内容,这使得整个事情变得相当尴尬。
但是,如果您在不同 laptops/machines 上开发,您确实需要某种方式与任何远程团队成员甚至其他设备共享这些状态文件。您还需要某种方式来存储和备份这些文件,因为如果您丢失状态文件,您将会感到非常痛苦,因为 Terraform 使用状态文件来计算它正在管理的内容,以免踩到脚趾其他工具。
我想说 S3 可能是您现在可以放置它们的最佳位置。它几乎是免费的,耐用性和可用性都非常好,在没有 Terraform 的情况下首先使用 remote state resource. And probably most importantly you only have to create an S3 bucket to get started. Having to build a Consul or etcd 集群在 Terraform 中对它有很好的本地支持(否则你会遇到先有鸡还是先有蛋的问题,你在哪里存储用于创建的状态那些?)即使您打算使用这些产品中的任何一种,也会有点痛苦。
显然,如果您使用的是 OpenStack,那么 Swift should make a good alternative (although I've not used it). I've also not used Hashicorp's Atlas 但如果您愿意为该服务付费,它可能同样有用。
不将 .tfstate
文件存储在 Git 中有几个原因:
- 您可能会在 运行 宁
terraform apply
之后忘记提交和推送您的更改,因此您的队友将拥有过时的 .tfstate
文件。此外,如果没有对这些状态文件进行任何锁定,如果两个团队成员 运行 Terraform 同时处理相同的 .tfstate
文件,您可能会覆盖彼此的更改。您可以通过以下方式解决这两个问题:a) 使用 Terraform remote state, which will push/pull the .tfstate
files automatically every time you run terraform apply
and b) using a tool like terragrunt 将 .tfstate
文件存储在 S3 存储桶中,为您的 .tfstate
文件提供锁定。
.tfstate
文件可能包含秘密。例如,如果您使用 aws_db_instance resource, you have to specify a database password, and Terraform will store that, in plaintext, in the .tfstate
file. This is a bad practice on Terraform's behalf to begin with and storing unencrypted secrets in version control only makes it worse. At least if you store .tfstate
files in S3, you can enable encryption at rest (SSL provides encryption while in motion) and configure IAM policies to limit who has access. It's very far from ideal and we'll have to see if the see open issue discussing this problem about 它会得到修复。
有关详细信息,请查看 How to manage Terraform state and Terraform: Up & Running。
TL;DR:
重要! 存储在源代码管理中可能会暴露 potentially sensitive data 并存在 运行 Terraform 针对旧版本状态的风险。别这样。
Terraform 不再建议在源代码管理中存储状态。您的 'good' 选项是远程或本地。
与本地和存储在源代码管理中相比,远程状态具有显着优势。详情如下。
原回答:
Yevgeniy 的回答很好。由于 Terraform 已将其文档更新为声明:
,因此该问题现在争议较小
Terraform also puts 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. It is generally recommended to setup remote state
when working with Terraform. This will mean that any potential secrets
stored in the state file, will not be checked into version control
因此,既定最佳实践与官方建议之间不再存在分歧。
更新2019-05-17
在 the most recent version of the docs 中已更改为:
... 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. ...
我不希望建议会恢复到源代码管理是存储状态的首选方法。
尽管上面引用了文档作为独立开发者,远程状态仍然是有益的
远程状态允许独立开发者:
- 从多个设备on/run 处理他们的 Terraform 代码
- 轻松备份并防止丢失状态文件,具体取决于选择的后端
- 通过 outputs
分隔其体系结构的各个部分
- Automatically encrypt state file at rest,取决于选择的后端
我发现通过其他方式分享 terraform.tfstate 比 Git.
更有优势
例如:S3、Dropbox 等(打开版本控制)
然后可以回滚到以前的基础结构状态。
例如,您从提交 B 回滚存储库,回滚到提交 A。如果 terraform.tfstate 未更改 - terraform 将考虑如何回滚您的所有内容在提交 B 期间添加。回滚将很容易。
如果 terraform.tfstate 也回滚到提交 A - 那么 terraform 会认为 terraform.tfstate与所需配置同步,不会将回滚应用到您的基础架构。
关于是否将.tfstate
文件提交到Git 的问题,我有点疑惑。 Terraform documentation 状态:
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 config can be used to provision many boxes on different infrastructure, each of which could have a different state. As it can also be run by multiple people this state should be in a centralised location (like S3) but not git.
(原作者强调,非本人强调)
谁是对的,如果是,为什么?
这可能会归结为偏好,但我会说 git(或任何其他源代码控制)不是存储状态文件的特别好的选择,因为它们是您代码的输出写的很像编译的二进制文件,甚至是最小化的 JS 或 LESS 编译为 CSS.
最重要的是,状态文件中的内容可能会迅速变化,作为内容的输出 运行 而不是代码中实际更改的内容,这使得整个事情变得相当尴尬。
但是,如果您在不同 laptops/machines 上开发,您确实需要某种方式与任何远程团队成员甚至其他设备共享这些状态文件。您还需要某种方式来存储和备份这些文件,因为如果您丢失状态文件,您将会感到非常痛苦,因为 Terraform 使用状态文件来计算它正在管理的内容,以免踩到脚趾其他工具。
我想说 S3 可能是您现在可以放置它们的最佳位置。它几乎是免费的,耐用性和可用性都非常好,在没有 Terraform 的情况下首先使用 remote state resource. And probably most importantly you only have to create an S3 bucket to get started. Having to build a Consul or etcd 集群在 Terraform 中对它有很好的本地支持(否则你会遇到先有鸡还是先有蛋的问题,你在哪里存储用于创建的状态那些?)即使您打算使用这些产品中的任何一种,也会有点痛苦。
显然,如果您使用的是 OpenStack,那么 Swift should make a good alternative (although I've not used it). I've also not used Hashicorp's Atlas 但如果您愿意为该服务付费,它可能同样有用。
不将 .tfstate
文件存储在 Git 中有几个原因:
- 您可能会在 运行 宁
terraform apply
之后忘记提交和推送您的更改,因此您的队友将拥有过时的.tfstate
文件。此外,如果没有对这些状态文件进行任何锁定,如果两个团队成员 运行 Terraform 同时处理相同的.tfstate
文件,您可能会覆盖彼此的更改。您可以通过以下方式解决这两个问题:a) 使用 Terraform remote state, which will push/pull the.tfstate
files automatically every time you runterraform apply
and b) using a tool like terragrunt 将.tfstate
文件存储在 S3 存储桶中,为您的.tfstate
文件提供锁定。 .tfstate
文件可能包含秘密。例如,如果您使用 aws_db_instance resource, you have to specify a database password, and Terraform will store that, in plaintext, in the.tfstate
file. This is a bad practice on Terraform's behalf to begin with and storing unencrypted secrets in version control only makes it worse. At least if you store.tfstate
files in S3, you can enable encryption at rest (SSL provides encryption while in motion) and configure IAM policies to limit who has access. It's very far from ideal and we'll have to see if the see open issue discussing this problem about 它会得到修复。
有关详细信息,请查看 How to manage Terraform state and Terraform: Up & Running。
TL;DR:
重要! 存储在源代码管理中可能会暴露 potentially sensitive data 并存在 运行 Terraform 针对旧版本状态的风险。别这样。
Terraform 不再建议在源代码管理中存储状态。您的 'good' 选项是远程或本地。
与本地和存储在源代码管理中相比,远程状态具有显着优势。详情如下。
原回答:
Yevgeniy 的回答很好。由于 Terraform 已将其文档更新为声明:
,因此该问题现在争议较小Terraform also puts 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. It is generally recommended to setup remote state when working with Terraform. This will mean that any potential secrets stored in the state file, will not be checked into version control
因此,既定最佳实践与官方建议之间不再存在分歧。
更新2019-05-17
在 the most recent version of the docs 中已更改为:
... 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. ...
我不希望建议会恢复到源代码管理是存储状态的首选方法。
尽管上面引用了文档作为独立开发者,远程状态仍然是有益的
远程状态允许独立开发者:
- 从多个设备on/run 处理他们的 Terraform 代码
- 轻松备份并防止丢失状态文件,具体取决于选择的后端
- 通过 outputs 分隔其体系结构的各个部分
- Automatically encrypt state file at rest,取决于选择的后端
我发现通过其他方式分享 terraform.tfstate 比 Git.
更有优势例如:S3、Dropbox 等(打开版本控制)
然后可以回滚到以前的基础结构状态。
例如,您从提交 B 回滚存储库,回滚到提交 A。如果 terraform.tfstate 未更改 - terraform 将考虑如何回滚您的所有内容在提交 B 期间添加。回滚将很容易。
如果 terraform.tfstate 也回滚到提交 A - 那么 terraform 会认为 terraform.tfstate与所需配置同步,不会将回滚应用到您的基础架构。