Terraform Taint and Terraform Loops

Ravindra Kumar
2 min readJan 15, 2025

--

1. Terraform Taint

Terraform taint is a command used to mark a resource in the state file as “tainted.” When you run `terraform apply` next, Terraform will destroy and recreate the tainted resource.

When to Use Terraform Taint?

  • Fixing a Broken Resource: If a resource is malfunctioning or needs to be reset.
  • Forcing a Replacement: If you want to recreate a resource to apply changes that aren’t directly managed by Terraform.
  • Testing Infrastructure: To test how your infrastructure reacts to the recreation of specific resources.

Usage:

terraform taint <resource_address>

Example:

terraform taint google_compute_instance.my_instance

This marks the `google_compute_instance\.my_instance` resource for recreation.

Key Points

  • Behavior: Marking a resource as tainted forces Terraform to plan its destruction and recreation.
  • State Update: terraform taint only updates the state file, not the actual infrastructure, until you run `terraform apply`.
  • Untainting: Use `terraform untaint` to reverse the taint:
terraform untaint <resource_address>

2. Terraform Loops

Loops in Terraform allow you to perform repetitive tasks like creating multiple resources or iterating through data structures. Terraform supports loops using:

  • count
  • for_each
  • for (within expressions)

Using `count`

  • Simplest way to create multiple instances of a resource.

example:

resource "google_compute_instance" "example" {
count = 3
name = "debian-cloud/debian-12"
machine_type = "n1-standard-2"
zone = us-central1-a
}

This creates 3 AWS EC2 instances.

Using `for_each`

  • Used for iterating over maps or sets.

Example (Iterating Over a Set):

resource "aws_s3_bucket" "example" {
for_each = toset(["bucket1", "bucket2", "bucket3"])
bucket = each.key
}

This creates 3 S3 buckets with names `bucket1`, `bucket2`, and `bucket3`.

Example (Iterating Over a Map):

resource "aws_instance" "example" {
for_each = {
instance1 = "t2.micro"
instance2 = "t2.small"
}
ami = "ami-123456"
instance_type = each.value
}

This creates 2 instances with different instance types.

Using `for` in Expressions

  • Used inside variables, outputs, and locals for transforming or filtering data.

Example:

variable "names" {
default = ["alpha", "beta", "gamma"]
}
output "upper_case_names" {
value = [for name in var.names : upper(name)]
}

This outputs: `[“ALPHA”, “BETA”, “GAMMA”]`.

Count vs. For_Each

Combining Taint and Loops

If you taint a resource created using a loop (e.g., `count` or `for_each`), specify the index or key:

  • With `count`:
terraform taint aws_instance.example[0]
  • With `for_each`:
terraform taint aws_s3_bucket.example["bucket1"]

This explains the terraform taints and loops. Please follow for more and subscribe my youtube channel — https://www.youtube.com/channel/UCfpf-vjGm_54leJWcAWTiYA.

--

--

Ravindra Kumar
Ravindra Kumar

Written by Ravindra Kumar

Hi, This is Ravi. I am Senior DevOps Engineer.. I love to write technical blogs.

No responses yet