I'm creating Azure VM extension resources azurerm_virtual_machine_extension
using terraform. However, I'm stuck with below use-case whether I can dynamically create settings
block?
variable "test_string" {
description = "boolean var to attach VM nics to lb"
type = string
default = ""
}
variable "test_script" {
description = "script locally"
type = string
default = ""
}
variable "file_uri" {
description = "script to download"
type = string
default = "https://example.com/azure-tests/custom_script.sh"
}
resource "azurerm_virtual_machine_extension" "ama_vm_extension" {
name = "tst-vm-extension"
virtual_machine_id = "xxxxx-xxxx-xxxxx"
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.0"
dynamic settings = <<SETTINGS
{
count = var.file_uri != "" ? 1: 0
"commandToExecute": "sh ${basename(var.file_uri)}",
"fileUris": ["${var.file_uri}"]
}
SETTINGS
dynamic settings = <<SETTINGS
{
count = var.test_string != "" ? 1: 0
content {
"commandToExecute": "${var.test_string}"
}
}
SETTINGS
dynamic settings = <<SETTINGS
{
count = var.test_script != "" ? 1: 0
content {
"script": "${base64encode(var.test_string)}"
}
}
SETTINGS
}
In the above code, I would like to control the resource behavior with test_string
, test_script
, file_uri
vars. However, terraform allows only one settings
block in the resource and I don't know how I can use dynamic block feature here. As we have SETTINGS
string in the end.
Your help is much appreciated here.
Thanks, Harsha