gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
7.93k stars 964 forks source link

Make hclfmt format generated code #2072

Open alikhil opened 2 years ago

alikhil commented 2 years ago

Sometimes we want to generate terraform code and we write configurations like:


generate "data" {
  path      = "data_gen.tf"
  if_exists = "overwrite"
  contents  = <<EOF
data "yandex_dns_zone" "infra" {
  name = "${local.common.infra_dns_name}"
  folder_id = "${local.common.common_folder_id}"
}
}

And if we run terragrunt hclfmt and terraform fmt -recursive this config will not be formatted.

However, if we run terragrunt fmt -check=true it will produce errors like:

data_gen.tf
ERRO[0003] 1 error occurred:
        * exit status 3

I propose to add autoformatting of generated objects by setting some special flag (terragrunt hclfmt --format-generated)

yorinasub17 commented 2 years ago

This is a good point. I think the better approach is for terragrunt to format the generated code when it is terraform, e.g. an option on generate block:

generate "data" {
  # ... other args omitted for brevity ...
  tf_fmt = true
}

That way, the code is already formatted when terragrunt emits it out.