How to add identity id to existing automation account using terraform?

Tanul 1,296 Reputation points
2023-07-11T20:18:24.1966667+00:00

How to add user assigned identity id to an existing azure automation account using terraform.

Can someone share the script please?

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
{count} votes

1 answer

Sort by: Most helpful
  1. Vahid Ghafarpour 23,600 Reputation points Volunteer Moderator
    2023-07-11T20:38:26.9033333+00:00

    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
    }
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.