|
1 | 1 | --- |
2 | | -page_title: Forcing Re-creation of Resources (Tainting) - Terraform CLI |
| 2 | +page_title: Forcing Re-creation of Resources - Terraform CLI |
3 | 3 | description: Commands that allow you to destroy and re-create resources manually. |
4 | 4 | --- |
5 | 5 |
|
6 | | -# Forcing Re-creation of Resources (Tainting) |
| 6 | +# Forcing Re-creation of Resources |
7 | 7 |
|
8 | | -When a resource declaration is modified, Terraform usually attempts to update |
9 | | -the existing resource in place (although some changes can require destruction |
10 | | -and re-creation, usually due to upstream API limitations). |
| 8 | +During planning, by default Terraform retrieves the latest state of each |
| 9 | +existing object and compares it with the current configuration, planning |
| 10 | +actions only against objects whose current state does not match the |
| 11 | +configuration. |
11 | 12 |
|
12 | | -In some cases, you might want a resource to be destroyed and re-created even |
13 | | -when Terraform doesn't think it's necessary. This is usually for objects that |
14 | | -aren't fully described by their resource arguments due to side-effects that |
15 | | -happen during creation; for example, a virtual machine that configures itself |
16 | | -with `cloud-init` on startup might no longer meet your needs if the cloud-init |
17 | | -configuration changes. |
| 13 | +However, in some cases a remote object may become damaged or degraded in a |
| 14 | +way that Terraform cannot automatically detect. For example, if software |
| 15 | +running inside a virtual machine crashes but the virtual machine itself is |
| 16 | +still running then Terraform will typically have no way to detect and respond |
| 17 | +to the problem, because Terraform only directly manages the machine as a whole. |
18 | 18 |
|
19 | | -- [The `terraform taint` command](/cli/commands/taint) tells Terraform to |
20 | | - destroy and re-create a particular resource during the next apply, regardless |
21 | | - of whether its resource arguments would normally require that. |
| 19 | +If you know that an object is damaged, or if you want to force Terraform to |
| 20 | +replace it for any other reason, you can override Terraform's default behavior |
| 21 | +using [the `-replace=...` planning option](/cli/commands/plan#replace-address) |
| 22 | +when you run either `terraform plan` or `terraform apply`: |
22 | 23 |
|
23 | | -- [The `terraform untaint` command](/cli/commands/untaint) undoes a |
24 | | - previous taint, or can preserve a resource that was automatically tainted due |
25 | | - to failed [provisioners](/language/resources/provisioners/syntax). |
| 24 | +```shellsession |
| 25 | +$ terraform apply -replace=aws_instance.example |
| 26 | +# ... |
| 27 | + |
| 28 | + # aws_instance.example will be replaced, as requested |
| 29 | +-/+ resource "aws_instance" "example" { |
| 30 | + # ... |
| 31 | + } |
| 32 | +``` |
| 33 | + |
| 34 | +## The "tainted" status |
| 35 | + |
| 36 | +Sometimes Terraform is able to infer automatically that an object is in an |
| 37 | +incomplete or degraded state. For example, if creation of a complex object |
| 38 | +fails in such a way that parts of it already exist in the remote system, or |
| 39 | +if object creation succeeded but a provisioner step subsequently failed, |
| 40 | +Terraform must remember that the object exists but may not be fully-functional. |
| 41 | + |
| 42 | +Terraform represents this situation by marking an object in the state as |
| 43 | +"tainted". When an object is marked with this status, the next plan will force |
| 44 | +replacing that object in a similar way to if you had specified that object's |
| 45 | +address using `-replace=...` as described above. |
| 46 | + |
| 47 | +``` |
| 48 | + # aws_instance.example is tainted, so must be replaced |
| 49 | +-/+ resource "aws_instance" "example" { |
| 50 | + # ... |
| 51 | + } |
| 52 | +``` |
| 53 | + |
| 54 | +If Terraform has marked an object as tainted but you consider it to be working |
| 55 | +correctly and do not want to replace it, you can override Terraform's |
| 56 | +determination using [the `terraform untaint` command](/cli/commands/untaint), |
| 57 | +after which Terraform will consider the object to be ready for use by any |
| 58 | +downstream resource declarations. |
| 59 | + |
| 60 | +You can also _force_ Terraform to mark a particular object as tainted using |
| 61 | +[the `terraform taint` command](/cli/commands/taint), but that approach is |
| 62 | +deprecated in favor of the `-replace=...` option, which avoids the need to |
| 63 | +create an interim state snapshot with a tainted object. |
0 commit comments