Reputation: 2997
I'm trying to changed the value in a terraform map. In my terraform.auto.tfvars
file:
dependencies = {
win_chocolatey = {
name = "chocolatey"
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
virtual_machine_id = ""
type_handler_version = "2.0"
auto_upgrade_minor_version = "false"
extensions_custom_script_fileuris = ""
extensions_custom_command = "powershell -ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) exit 0"
tags = {
purpose = "software"
}
}
}
And in my main.tf file, I want to update the value of virtual_machine_id
before passing the variable to the resources.
I have tried this:
data "azurerm_virtual_machine" "wininstaller" {
name = "vm-wininstall"
resource_group_name = "rg-zephyr-devops"
}
locals {
dependencies = {
for x in var.dependencies : { win_chocolatey.virtual_machine_id = data.azurerm_virtual_machine.wininstaller.id }
}
}
but I get an error in the for loop:
Error: Invalid 'for' expression
on main.tf line 19, in locals: 19: dependencies = {for x in var.dependencies : { win_chocolatey.virtual_machine_id = data.azurerm_virtual_machine.wininstaller.id }}
Key expression is required when building an object.
Does anyone know how to fix this or a better way to do this?
Upvotes: 1
Views: 2031
Reputation: 28739
When you iterate through a map and want to access a value, you need to split the temporary lambda variables between key and value. When you want to access the attribute of an object, you have to provide the full and correct namespace of the attribute in the scope. When you construct a map, you need to specify the key value pair with the =>
operator. Maps are initialized with {}
, and so you would be creating a nested map with no outer key. Combining these four fixes, we would arrive at:
locals {
dependencies = {
for key, value in var.dependencies : value.virtual_machine_id => data.azurerm_virtual_machine.wininstaller.id
}
}
to fix your syntax and functional errors.
Upvotes: 3