I hope this script can help you:
# Configure the Azure provider
provider "azurerm" {
features {}
}
# Define the resource group and automation account
resource "azurerm_resource_group" "example" {
name = "example-resource-group"
location = "West US"
}
resource "azurerm_automation_account" "example" {
name = "example-automation-account"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
sku_name = "Free"
}
# Create a user-assigned identity
resource "azurerm_user_assigned_identity" "example" {
name = "example-identity"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}
# Assign the identity to the automation account
resource "azurerm_role_assignment" "example" {
scope = azurerm_automation_account.example.id
role_definition_name = "Contributor"
principal_id = azurerm_user_assigned_identity.example.principal_id
}