Terraform是否在状态文件中包含足够的信息以跟踪配置更改?

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

Does terraform contain sufficient information in statefile to track changes to config?

问题

The terraform statefile contains the state of input from the last run. Specifically, it allows:

  1. Calculation of the graph based on changed files (known functionality).
  2. Comparison of the input (not the cloud target state) to the last run, determining changes in input.

This capability doesn't require connecting to the cloud. It's similar to how Kubernetes saves the last applied manifest on an annotation for independent diffing, regardless of the cloud state.

英文:

Does the terraform statefile contain the state of input from the last run?

Specifically, I’m trying to determine if terraform has sufficient information to:

  1. Calculate the graph based on changed files (I know it does this)
  2. Compare the input (not the cloud target state) to the last run and determine which resources input has changed

I know it can run a plan and use the statefile and current cloud state to see which resources should be changed.

I’m looking for something at an earlier stage, which wouldn’t even require connecting to the cloud.

It’s a diff between config input last time it was run and this time.

I think the closest example I could give is how kubernetes, when applying a resource, saves the last applied manifest on an annotation, so you can run a diff independent of the state of the cloud.

答案1

得分: 1

Short answer, yes.

The statefile tracks all config you have done in Terraform. Whether the input is from tfvars, environment variables, cmd arguments or console input does not matter. All config is tracked and any change will trigger a modify.

You can make terraform ignore changes by using lifecycle management.

And what you are asking about the "earlier stage", yes. That's what terraform plan is doing. You seem to misunderstand the workings of Plan. terraform plan checks your current config against the statefile. Terraform actually never checks what exists in the environment before runtime. On runtime it will notice it exists if the provider throws a "Resource exists" error on attempt to create and you'll get a message telling you to import the resource into the state.

The connecting to the cloud part, if your backend statefile is not stored safely in a remote state, then you don't need to connect to the cloud. But I highly recommend setting up some cloud storage with backup and soft delete to safely store your statefile.

英文:

Short answer, yes.

The statefile tracks all config you have done in Terraform. Whether the input is from tfvars, environment variables, cmd arguments or console input does not matter. All config is tracked and any change will trigger a modify.

You can make terraform ignore changes by using lifecycle management.

And what you are asking about the "earlier stage", yes. That's what terraform plan is doing. You seem to misunderstand the workings of Plan. terraform plan check your current config against the statefile. Terraform actually never checks what exists in the environment before runtime. On runtime it will notice it exists if the provider throws an "Resource exists" error on attempt to create and you'll get a message telling you to import the resource into the state.

The connecting to the cloud part, if your backend statefile is not stored safely in a remote state, then you don't need to connect to the cloud. But i highly recomend setting up some cloud storage with backup and soft delete to safely store your statefile.

答案2

得分: 1

以下是翻译好的内容:

在规划 Terraform 时,考虑了几个不同的信息部分:

  • 上一次运行状态:这是在您最近的 terraform apply 结束时写入的状态快照,捕捉了在该应用期间由所有提供程序执行的操作后立即报告的状态。
  • 当前状态:Terraform 将每个资源实例的上一次运行状态发送给提供程序,提供程序在检查了真实远程系统后返回一个更新的对象。(Terraform 在其用户界面中将此操作描述为“刷新”。)
  • 期望状态:Terraform 评估您配置中的表达式以生成“期望状态”,Terraform 假定这是您希望远程系统具有的设置的描述。

在这三个构件之间,Terraform 可以生成两种类型的信息:

  • 在 Terraform 外部已经进行的更改:如果“当前状态”与“上一次运行状态”不同,那表明自上次操作以来在 Terraform 外部发生了变化。Terraform 不总是在用户界面中显示这些变化,但如果它们似乎与下一项的更改有关,它将这样做。
  • 建议的更改:如果“期望状态”与“当前状态”不同,那表明您希望更改远程系统中的某些内容,因此 Terraform(实际上:Terraform 提供程序)提出了应该更改远程系统以使当前状态与期望状态匹配的操作,这些操作就是计划描述中的内容。

如果选择应用计划,那么计划中执行的操作将每个产生一个“最终状态”,Terraform 将保存它以便成为下次创建计划时的“上一次运行状态”。

Terraform 并不严格跟踪配置本身在运行之间的变化,但由于最终状态是从期望状态派生出来的,而期望状态是从配置派生出来的,因此它间接地保留了有关在运行之间配置内容的一些信息。

然而,Terraform 并不提供直接比较上一次运行状态与期望状态的方法。所有针对资源实例状态的操作实际上都是由提供程序而不是 Terraform 本身执行的,并且在执行这些操作之前,必须初始化提供程序,大多数提供程序都期望能够在其初始化期间连接到远程系统。

如果使用 terraform plan -refresh=false,则会禁用计算“当前状态”的步骤 —— Terraform 将仅使用上一次运行状态的精确副本作为当前状态 —— 但通过提供程序实现将期望状态与当前状态进行比较以决定是否需要任何操作的过程是在线操作,提供程序通常期望能够访问远程系统。

英文:

While planning Terraform considers several different pieces of information:

  • Previous run state: this is the state snapshot that was written at the end of your most recent terraform apply, capturing the state reported by all of those providers immediately after they performed whatever actions they took during that apply.
  • Current state: Terraform sends the previous run state for each resource instance to the provider and the provider returns an updated object after checking the real remote system. (Terraform describes this operation in its UI as "Refreshing".)
  • Desired state: Terraform evaluates the expressions in your configuration to produce the desired state, which Terraform assumes is a description of the settings you would like the remote system to have.

Between these three artifacts Terraform can produce two kinds of information:

  • Changes already made outside of Terraform: if "current state" is different from "previous run state" then that suggests that something changed outside of Terraform since the last operation. Terraform doesn't always display these in the UI, but it will do so if they seem related to changes from the next item.
  • Proposed changes: If "desired state" is different from "current state" then that suggests you want to change something in the remote system, and so Terraform (actually: a Terraform provider) proposes an action that should change the remote system so that the current state matches the desired state, and those actions are what appear in as the description of the plan.

If you choose to apply a plan then the actions taken in the plan will each produce a "final state", which Terraform saves so it can become the "previous run state" for the next time you create a plan.

Terraform does not strictly track the configuration itself between runs, but since the final state is derived from the desired state and the desired state is derived from the configuration it does indirectly retain some information about what was in the configuration between runs, indirectly.

However, Terraform does not offer any way to just compare the previous run state with the desired state. All actions against a resource instance state are really performed by a provider rather than by Terraform itself, and providers must be initialized before they can perform those operations and most providers expect to be able to connect to the remote system during their initialization.

If you use terraform plan -refresh=false then you would disable the step of calculating the "current state" -- Terraform will just use an exact copy of the previous run state as the current state -- but the process of comparing the desired state with the current state to decide if any actions is required is implemented by providers as an online operation, where the provider typically expects to be able to access the remote system.

huangapple
  • 本文由 发表于 2023年6月29日 10:49:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577787.html
匿名

发表评论

匿名网友

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

确定