# 11. Meta-Arguments

### depends\_on

* If two resources depend on each other (but not on each others data), depends\_on specifies that dependency to enforce ordering

***

### count

* allows for creation of multiple resources/modules from a single block
* usefil when multiple necessary resources are nearly identical
* example is 4 ec2 instances with tag name as `${count.index}`

***

### for\_each

* allows for creation of multiple resources/modules from single block
* allows more control to customize each resource than count

```hcl
locals {
  subnet_ids = toset([
    "subnet-123",
	  "subnet-456",
  ])
}

resource "aws_instance" "server" {
  for_each = local.subnet_ids

  ami = "aaa1"
  instance_type = "t2.micro"
  subnet_id = each.key

  tags = {
    Name = "Server ${each.key}"
  }
}
```

***

### lifecycle

* set of meta atguments to control terraform behaviour for specific resources
* `create_before_destroy` can help with zero downtime deployments
* `ignore_changes` prevents terraform from trying to revert metadata being set elsewhere
* `prevent_destroy` causes terraform to reject any plan which would destroy the resource

```hcl
resource "aws_instance" "server" {
  ami = "aaa1"
  instance_type = "t2.micro"

	lifecycle {
	  create_before_destroy = true
	  ignore_changes = [
	    # Some resources have metadata
		  # modified automatically outside
		  # of Terraform
		  tags
	  ]
	}
}
```

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.nomanaziz.me/devops/infrastructure-as-a-code-iac/terraform/11.-meta-arguments.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
