Meta Arguments
Terraform meta arguments like depends_on that give you additional control over how resources are managed.
These arguments provide additional control over how resources are managed and interacted with and can be used with any type of resource.
depends_on
Used to declare dependencies between resources, ensuring that the action (create or destroy) is only done after the specified dependant resource have been successfully applied.
It is very useful for managing resource dependencies that isn't automatically detected by Terraform's implicit dependency analysis.
By using depends_on, you can manage the order of which the resources are created,
modified or destroyed - especially useful in infrastructure scenarios, where you have a complex
setup and certain resources must exist or be configured before other resources can be managed.
resource "aws_s3_bucket" "data" {
bucket = "my-data-bucket"
}
resource "aws_instance" "app" {
ami = "ami-123456"
instance_type = "t2.micro"
depends_on = [aws_s3_bucket.data]
}
count
This argument allows you to select the number of instances to create of a resource.
Terraform will dynamically generate multiple instances of the resource(s), index from 0 to
count-1, which why you often see in resource naming scenarios using count, the
argument ending in count.index + 1.
Otherwise mostly used for creating multiple VM's or storage buckets that are all identical or similar.
resource "aws_instance" "server" {
count = 3
ami = "ami-123456"
instance_type = "t2.micro"
tags = {
Name = "server-${count.index + 1}"
}
}
for_each
The iterative loop enables us to create multiple instances, based on a set or map.
Whereas count uses a simple integer, for_each allows for a much more
dynamic resource creation, since each instance can be associated with a specific key:value pair,
taken from the set or map structure.
However, you need to stick with either count or for_each, since both
cannot be used in the same resource.
Use for_each when you need a more customizable way of generating resources
iteratively.
resource "aws_instance" "server" {
for_each = toset(["dev", "staging", "prod"])
ami = "ami-123456"
instance_type = "t2.micro"
tags = {
Name = "server-${each.key}"
}
}
provider
Per default terraform will use the local name of the provider, taken from the first word in the resource type.
But for multiple provider and geolocations, you can use this meta argument to define a different
provider, then use the provider argument to a definition within a resource to pin it
to a specific one.
provider "some_provider" {
region = "region_1"
}
provider "some_provider" {
alias = "other_region"
region = "region_2"
}
lifecycle
A nested block used to customize how Terraform handles resource creation, updates, and destruction.
Common arguments:
create_before_destroy- creates the replacement resource before destroying the old one (avoids downtime).prevent_destroy- blocksterraform destroy/replace on this resource; errors out instead.ignore_changes- tells Terraform to ignore changes to specific attributes (orall) after creation, useful when an attribute is modified outside Terraform.replace_triggered_by- forces replacement of the resource when a referenced resource/attribute changes.precondition/postcondition- custom validation checks run before/after apply.
resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = "t2.micro"
lifecycle {
create_before_destroy = true
prevent_destroy = true
ignore_changes = [tags, ami]
}
}