Azure / aztfexport

A tool to bring existing Azure resources under Terraform's management
https://azure.github.io/aztfexport/
Mozilla Public License 2.0
1.63k stars 190 forks source link

Invalid 'for' expression; For expression requires variable name after 'for' #557

Closed apae89 closed 2 months ago

apae89 commented 3 months ago

Hey, I'm running into an issue while generating HCL code from existing infrastructure. The generation process cancels with the error message shown below. The main.tf file has not been generated.

CLI command: aztfexport.exe resource-group --use-azure-cli-cred --non-interactive --hcl-only --continue <rg_name>

Error message: Error: generating Terraform configuration: converting from state to configurations: converting terraform state to config: generate for one resource: tune template for azurerm_monitor_autoscale_setting: parsing the generated template for azurerm_monitor_autoscale_setting: :24,12-13: Invalid 'for' expression; For expression requires variable name after 'for'.

Could you please analyze this issue? Thanks for your work!

Best regards, Andreas

magodo commented 3 months ago

@apae89 Would you please share the state file, especially only for the azurerm_monitor_autoscale_setting resource?

apae89 commented 3 months ago

Thanks for quick reaction. Sure:

{
      "mode": "managed",
      "type": "azurerm_monitor_autoscale_setting",
      "name": "res-1051",
      "provider": "provider[\"registry.terraform.io/hashicorp/azurerm\"]",
      "instances": [
        {
          "schema_version": 2,
          "attributes": {
            "enabled": true,
            "id": "/subscriptions/<removed>/resourceGroups/<removed>/providers/Microsoft.Insights/autoScaleSettings/<removed>",
            "location": "northeurope",
            "name": "<removed>",
            "notification": [],
            "predictive": [],
            "profile": [
              {
                "capacity": [
                  {
                    "default": 2,
                    "maximum": 2,
                    "minimum": 2
                  }
                ],
                "fixed_date": [],
                "name": "Week",
                "recurrence": [
                  {
                    "days": [
                      "Monday",
                      "Tuesday",
                      "Wednesday",
                      "Thursday",
                      "Friday"
                    ],
                    "hours": [
                      8
                    ],
                    "minutes": [
                      0
                    ],
                    "timezone": "UTC"
                  }
                ],
                "rule": []
              },
              {
                "capacity": [
                  {
                    "default": 1,
                    "maximum": 1,
                    "minimum": 1
                  }
                ],
                "fixed_date": [],
                "name": "{\"name\":\"Auto created default scale condition\",\"for\":\"Week\"}",
                "recurrence": [
                  {
                    "days": [
                      "Monday",
                      "Tuesday",
                      "Wednesday",
                      "Thursday",
                      "Friday"
                    ],
                    "hours": [
                      17
                    ],
                    "minutes": [
                      0
                    ],
                    "timezone": "UTC"
                  }
                ],
                "rule": []
              }
            ],
            "resource_group_name": "<removed>",
            "tags": {},
            "target_resource_id": "/subscriptions/<removed>/resourceGroups/<removed>/providers/Microsoft.Web/serverfarms/<removed>",
            "timeouts": null
          },
          "sensitive_attributes": [],
          "private": "<removed>"
        }
      ]
    }
magodo commented 3 months ago

The issue comes from "name": "{\"name\":\"Auto created default scale condition\",\"for\":\"Week\"}",, which is converted to following HCL:

    name = jsonencode({
      for  = "Week"
      name = "Auto created default scale condition"
    })

This somehow violates the forExpr in HCL spec, making HCL failed to parse it anymore.

Technically, we need to conditionally do the jsonencode wrapping, only if the content inside it is still HCL parsable. I'll try to make a fix then.

magodo commented 3 months ago

Fixed in https://github.com/magodo/tfadd/commit/397c16ed83f22aa64db542990274905970c449c5, you can install tfadd and run tfadd state from within the output directory that contains the exported state.

apae89 commented 3 months ago

Tried it, but running into an error (it's probably obvious, I'm not experienced with Go)

go install github.com/magodo/tfadd@latest
tfadd state (in directory containing terraform.tfstate)

produces the following error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x12 pc=0xc38627]

goroutine 1 [running]:
github.com/magodo/tfadd/schema/legacy.tuneForBlock(0xc003726000, 0xc001f14060, {0x0, 0x0, 0x0})
        C:/Users/<user>/go/pkg/mod/github.com/magodo/tfadd@v0.10.0/schema/legacy/tune_tpl.go:149 +0x5e7
github.com/magodo/tfadd/schema/legacy.(*ProviderSchema).TuneTpl(0xcbbee0?, {0xc000a0d200, 0x370, 0x480}, {0xc00202d140, 0x1c})
        C:/Users/<user>/go/pkg/mod/github.com/magodo/tfadd@v0.10.0/schema/legacy/tune_tpl.go:31 +0x28b
github.com/magodo/tfadd/tfadd.State({0x1756f00, 0x1ab0460}, 0xc0001f00a0, {0xc0011c5d48, 0x1, 0xd3edc0?})
        C:/Users/<user>/go/pkg/mod/github.com/magodo/tfadd@v0.10.0/tfadd/tfadd_state.go:93 +0x7f4
main.(*stateCommand).Run(0xc0001fa070?, {0xc00005c3b0, 0x0, 0x0})
        C:/Users/<user>/go/pkg/mod/github.com/magodo/tfadd@v0.10.0/main.go:111 +0x438
github.com/mitchellh/cli.(*CLI).Run(0xc0007e3a40)
        C:/Users/<user>/go/pkg/mod/github.com/mitchellh/cli@v1.1.2/cli.go:262 +0x55f
main.main()
        C:/Users/<user>/go/pkg/mod/github.com/magodo/tfadd@v0.10.0/main.go:132 +0x1a5
apae89 commented 3 months ago

My bad, installed it now from your specific commit via go install github.com/magodo/tfadd@397c16e Now it has successfully generated the HCL code.

Thank you very much for the quick fix!