TFNFR8 - Resource & Data Block Orders
There are 3 types of assignment statements in a resource
or data
block: argument, meta-argument and nested block. The argument assignment statement is a parameter followed by =
:
location = azurerm_resource_group.example.location
or:
tags = {
environment = "Production"
}
Nested block is a assignment statement of parameter followed by {}
block:
subnet {
name = "subnet1"
address_prefix = "10.0.1.0/24"
}
Meta-arguments are assignment statements can be declared by all resource
or data
blocks. They are:
count
depends_on
for_each
lifecycle
provider
The order of declarations within resource
or data
blocks is:
All the meta-arguments SHOULD be declared on the top of resource
or data
blocks in the following order:
provider
count
for_each
Then followed by:
- required arguments
- optional arguments
- required nested blocks
- optional nested blocks
All ranked in alphabetical order.
These meta-arguments SHOULD be declared at the bottom of a resource
block with the following order:
depends_on
lifecycle
The parameters of lifecycle
block SHOULD show up in the following order:
create_before_destroy
ignore_changes
prevent_destroy
parameters under depends_on
and ignore_changes
are ranked in alphabetical order.
Meta-arguments, arguments and nested blocked are separated by blank lines.
dynamic
nested blocks are ranked by the name comes after dynamic
, for example:
dynamic "linux_profile" {
for_each = var.admin_username == null ? [] : ["linux_profile"]
content {
admin_username = var.admin_username
ssh_key {
key_data = replace(coalesce(var.public_ssh_key, tls_private_key.ssh[0].public_key_openssh), "\n", "")
}
}
}
This dynamic
block will be ranked as a block named linux_profile
.
Code within a nested block will also be ranked following the rules above.
PS: You can use
avmfix
tool to reformat your code automatically.