TFNFR8 - Resource & Data Block Orders
ID: TFNFR8 - Category: Code Style - 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.locationor:
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:
countdepends_onfor_eachlifecycleprovider
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:
providercountfor_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_onlifecycle
The parameters of lifecycle block SHOULD show up in the following order:
create_before_destroyignore_changesprevent_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.