Is there any way to skip the terraform block/file if defined variable is empty or null. Instead of throwing error.
I have created tow terraform script for azure.
- For azure automation creation, runbook creation.
- For event grid creation.
After executing the first step I have to generate a webhook URL manually (there no such automation support for webhook generation). after generating of webhook URL I need to define it in second resource. If I defined empty/null or invalid URL then terraform throw the error.
below is the terraform code.
data "local_file" "runbook_script" {
filename = "${path.module}/envent-grid-runbook.ps1"
}
resource "azurerm_automation_runbook" "runbook" {
name = "event-gird-notification"
location = var.location
resource_group_name = var.resource_group_name
automation_account_name = azurerm_automation_account.CreateAutomation.name
log_verbose = true
log_progress = true
description = "This runbook is creted for event grid notification"
runbook_type = "PowerShell"
content = data.local_file.runbook_script.content
publish_content_link {
uri = ""
}
}
resource "azurerm_eventgrid_event_subscription" "key-vault" {
name = "test"
scope = "/subscriptions/xxxxxxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxx/name"
topic_name = "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/xxxxxx/vault"
webhook_endpoint {
url = var.webhook_url
}
included_event_types = [
"Microsoft.KeyVault.SecretNewVersionCreated",
"Microsoft.KeyVault.SecretNearExpiry",
"Microsoft.KeyVault.SecretExpired"
]
event_delivery_schema = "EventGridSchema"
}
If I defined null/empty var for webhook ULR variable. then getting below error.
Error: "webhook_endpoint.0.url": required field is not set
I have created a Jenkins job, where all terraform code to run in a single job. if the code fails, then entire job is getting failed. That's why looking for a solution to skip the specific block/file if var is empty or null.