'Facing error with terraform code for importing multiple runbook to azure automation account

I have created the azure automation account using terraform code. I have multiple runbooks PowerShell scripts saved in my local. I am using for.each option to import all the runbooks at a time. But I am getting some errors while running the terraform file. Please find my code below:

resource "azurerm_automation_runbook" "example" {
  for_each = fileset(".", "./Azure_Runbooks/*.ps1")
  name                    = ["${split("/", each.value)}"][1]
  location                = var.location
  resource_group_name     = var.resource_group
  automation_account_name = azurerm_automation_account.example.name
  log_verbose             = var.log_verbose
  log_progress            = var.log_progress
  runbook_type            = var.runbooktype
  content                 = filemd5("${each.value}")
}

Error:

Error: Invalid index
│
│   on AutomationAccount\main.tf line 51, in resource "azurerm_automation_runbook" "example":
│   51:   name                    = ["${split("/", each.value)}"][1]
│     ├────────────────
│     │ each.value will be known only after apply
│
│ The given key does not identify an element in this collection value: the given index is greater than   
│ or equal to the length of the collection.

Can someone please help how I can upload all my existing runbook scrips to the newly created automation account using terraform code.



Solution 1:[1]

You don't need list in a list. So instead of

 name                    = ["${split("/", each.value)}"][1]

it should be

 name                    = split("/", each.value)[1]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcin