Terraform
Meta-arguments
Meta-arguments are a class of arguments built into the Terraform configuration language that control how Terraform creates your infrastructure. This page provides an overview of the meta-arguments included with Terraform.
Introduction
You can use meta-arguments in any type of resource, including resources created with data
blocks. You can also use most meta-arguments in module
blocks.
The provider developer determines resource-specific arguments, but all resources support meta-arguments that let you manage resources' infrastructure lifecycle, including destruction behavior, preventing destruction, and establishing dependencies between resources. Terraform provides the following meta-arguments.
depends_on
The depends_on
meta-argument establishes dependencies between resources that do not directly reference one another. Use the depends_on
argument to explicitly set the order in which Terraform creates resources.
Tutorials
Examples
- Specify a dependency during validation
- Specify a dependency when querying a data source
- Specify a dependency when calling a module
- Specify a dependency when outputting values
- Specify a dependency during resource creation
Guidance
- Add dependencies when querying a data source
- Set dependencies in your module configuration
References
depends_on
argument incheck
blocksdepends_on
argument indata
blocksdepends_on
argument inephemeral
blocksdepends_on
argument inmodule
blocksdepends_on
argument inoutput
blocksdepends_on
argument inresource
blocks
count
By default, Terraform manages one infrastructure object for each resource
block. Terraform also creates single instances of a module per module
block. You can use the count
argument in resource
and module
blocks to create and manage multiple instances of each without writing a separate block for each instance.
How to choose between count
and for_each
The for_each
meta-argument performs a similar function to count
, but lets you reference different values for each instance you create. Use the count
argument when you want to create nearly identical instances. Use for_each
when some instance arguments must have distinct values that can't be directly derived from an
Integer index. You cannot use both a count
and for_each
argument in the same resource
or module
block.
Tutorials
Examples
- Create multiple ephemeral resources
- Create multiple instances of module resources
- Create multiple instances of a resource
Guidance
- Create multiple instances when querying a data source
- Create multiple instances when calling modules
References
count
argument indata
blockscount
argument inephemeral
blockscount
argument inmodule
blockscount
argument inresource
block
for_each
By default, Terraform configures one infrastructure object for each resource
, module
, and ephemeral
block. You can add the for_each
block to your resource
, data
, module
, and ephemeral
blocks to create and manage several similar objects, such as a fixed pool of compute instances, without writing a separate block for each instance.
The for_each
accepts a map or a set that resolves to string values, but you can specify pure functions, such as toset()
and tomap()
, to create a map or set of string key-value pairs. Each instance is associated with a distinct infrastructure object. Terraform creates, updates, or destroys each instance when applying changes to the configuration.
Whether iterating over the keys of a map or set of strings, all values must be known.
How to choose between for_each
and count
The count
meta-argument performs a similar operation to for_each
. Use for_each
when some instance arguments must have distinct values that can't be directly derived from an
integer. Use the count
argument when you want to create nearly identical instances.
You cannot use both a count
and for_each
argument in the same block.
Tutorials
Examples
- Create multiple ephemeral resources
- Import multiple existing resources
- Create multiple instances of module resources
- Create multiple instances of a resource
Guidance
- Creat multiple instances when querying a data source
- Create multiple instances when calling modules
References
for_each
argument indata
blocksfor_each
argument inephemeral
blocksfor_each
argument inmodule
blocksfor_each
argument in.resource
blocks
provider
By default, Terraform determines the local name of the provider from the first word in the resource type and uses that provider's default configuration to create the resource. For example, the resource type google_compute_instance
automatically uses the default configuration for the google
provider .
You can add multiple provider
blocks to your configuration and use the provider
argument to a resource definition to specify which provider it should use. Creating multiple configurations for a single provider is a common pattern for managing resources in different regions of multi-region services.
The provider
argument is distinct from the provider
block, which contains provider configuration settings. Refer to Providers for more information about the provider
block.
Examples
- Specify an alternate provider configuration for a data source
- Specify an alternate provider configuration for an ephemeral resource
- Select an alternate provider configuration for imported resources
- Select a provider without a default configuration
- Select an alternative provider configuration for resources
Guidance
- Providers
- Select an alternate provider configuration when querying a data source
References
provider
argument incheck
blocksprovider
argument indata
blocksprovider
argument inephemeral
blocksprovider
argument inimport
blockprovider
argument inresource
blocks
providers
By default, child modules inherit the default provider configurations of their parent module. You can specify an alternate provider configuration in the module
block using the providers
argument. The providers
argument instructs Terraform to use the reference provider configuration to create the module resources.
Some modules may contain resources that require different providers. As a result, you can specify alternate configurations for each provider supported by the module. Refer to the module documentation for details about the providers it supports.
When you add a providers
argument, child modules only have access to the provider configurations you specify. Use the providers
argument in the following scenarios:
- To use an alternate provider configuration for a child module.
- To configure a module that requires multiple configurations of the same provider.
Examples
- Apply different provider configurations for module resources
Guidance
Reference
lifecycle
Terraform performs the following operations when you apply a configuration:
- Creates resources defined in the configuration that are not associated with a real infrastructure object in the state.
- Destroys resources that exist in the state but not in the configuration.
- Updates in-place resources whose arguments have changed.
- Destroys and re-create resources whose arguments have changed but that Terraform cannot update in-place because of remote API limitations.
The lifecycle
block accepts a rule that customizes how Terraform performs the lifecycle stages for each resource. Support for each lifecycle
rule varies across Terraform configuration blocks. Refer to the reference documentation for the Terraform block you are adding to your configuration for details. The following table describes support for each lifecycle
rule:
Rule | Description | Terraform block |
---|---|---|
create_before_destroy | Terraform creates a replacement resource before destroying the current resource. | resource |
prevent_destroy | Terraform rejects operations to destroy the resource and returns an error. | resource |
ignore_changes | Specifies a list of resource attributes that Terraform ignores changes to. Otherwise, Terraform attempts to update the actual resource to match the configuration. | resource |
replace_triggered_by | Terraform replaces the resource when any of the referenced resources or specified attributes change. | resource |
precondition | Specifies a condition that Terraform evaluates before creating the resource. | data , ephemeral , resource |
postcondition | Specifies a condition that Terraform evaluates after creating the resource. | data , ephemeral , resource |
destroy | Set to false to remove a resource from state without destroying the actual infrastructure resource. You can only use this rule in the removed block. | removed |
Tutorials
Examples
- Apply custom conditions to data sources
- Ignore attribute changes
- Specify triggers that replace resource
- Apply custom conditions to resources