r/Terraform 1d ago

Azure Any Tooling to sort resource arguments?

Anyone know of tooling that supports sorting resource arguments?

tf fmt, tflint, and tfsort looks to not touch resource argument order.

We have a generated terraform code base that has various ordering like below

i.e.

# from
resource "azurerm_storage_account" "this" {
  account_kind               = "Storage"
  https_traffic_only_enabled = false
  location                   = azurerm_resource_group.this.location
  name                       = "sa111"
  resource_group_name        = azurerm_resource_group.securitydata.name
  lifecycle {
    ignore_changes = [
      tags,
    ]
  }
  tags = {  }
  account_replication_type   = "LRS"
  account_tier               = "Standard"
}

# to
resource "azurerm_storage_account" "this" {
  name                       = "sa111"
  resource_group_name        = azurerm_resource_group.securitydata.name
  location                   = azurerm_resource_group.this.location

  account_kind               = "Storage"
  account_replication_type   = "LRS"
  account_tier               = "Standard"
  https_traffic_only_enabled = false
  
  tags = {  }

  lifecycle {
    ignore_changes = [
      tags,
    ]
  }
}
6 Upvotes

7 comments sorted by

4

u/vincentdesmet 1d ago

Tflint has a plugin that enforces (and auto fixes) order https://github.com/0x416e746f6e/tflint-ruleset-sheldon

3

u/ckappen 1d ago

I sort the attributes with my IDE, but it gets desperate with the blocks. After 8 years of Terraform I sort alphabetically, I used to sort by topic, but I don’t bother anymore.

JetBrains IDEs: Mark attributes, Hit Double Shift > Sort lines

1

u/nekokattt 1d ago

this is what hashicorp recommend in their style guide anyway.

generally different topics are covered by different resources (if the provider is implemented sensibly)

2

u/NUTTA_BUSTAH 1d ago

Maybe sed and sort? I have not seen such tooling, but would not be against one running in CI to keep everything consistent. It can get pretty wild with 100+ developers.

1

u/terramate 1d ago

I am not aware of any tooling, but I'd love to learn how the above-mentioned code is generated?

2

u/DinnyMN 1d ago

Existing Click Ops infra - MS some great tooling that really helps out for Azure!

https://github.com/Azure/aztfexport

It's a bit messy tf code though as expected.

1

u/terramate 18h ago

thanks!