AzureAzure Automationazurerm_automation_runbookPro

How to Import an Azure Automation Runbook into Terraform

To import an existing Azure Automation Runbook into Terraform, scan it with Terraback and run terraback azure import --method bulk. Terraback writes the matching azurerm_automation_runbook resource block and the Terraform 1.5+ import block for you, so you do not have to run terraform import by hand. The import ID is the full resource ID (.../providers/Microsoft.Automation/automationAccounts/<account>/runbooks/<name>).

Import Azure Automation Runbook with Terraback (recommended)

Terraback reverse-engineers your live infrastructure: it reads the Azure Automation resource with read-only credentials, generates the HCL, and produces the exact import block. Two commands take you from a live Azure Automation Runbook to managed Terraform.

1

Scan your Azure account

terraback scan all azure --subscription-id YOUR_ID
2

Generate import blocks and import into state

terraback azure import --method bulk

The Terraform import block

Terraback emits a Terraform 1.5+ import block like the one below. Because the block lives in your configuration, the import is reviewable in a pull request and repeatable across environments.

import {
  to = azurerm_automation_runbook.cleanup
  id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/prod-rg/providers/Microsoft.Automation/automationAccounts/prod-aa/runbooks/cleanup"
}

Example azurerm_automation_runbook configuration

Here is a realistic Azure Automation Runbook block. Terraback generates a fuller version from your actual resource attributes; this is a minimal, valid starting point.

resource "azurerm_automation_runbook" "cleanup" {
  name                    = "cleanup"
  location                = "eastus"
  resource_group_name     = "prod-rg"
  automation_account_name = "prod-aa"
  log_verbose             = false
  log_progress            = false
  runbook_type            = "PowerShell"

  content = <<EOF
Write-Output "hello"
EOF
}

Gotchas when importing a Azure Automation Runbook

  • The runbook ID nests under its automation account; the account imports separately as azurerm_automation_account.
  • runbook_type is immutable (PowerShell, PowerShellWorkflow, Python2, Python3, GraphPowerShell, GraphPowerShellWorkflow) and must match the existing runbook.
  • Runbook content is not always returned by the API on import; supply the script body in content or via publish_content_link or Terraform will show a diff.
  • Schedules linking the runbook are separate azurerm_automation_job_schedule resources.

Doing it manually with terraform import

The native approach is to write the azurerm_automation_runbook block by hand, then run terraform import azurerm_automation_runbook.example <import-id> for every resource, one at a time. That works for a handful of resources, but it does not scale: you author all the HCL yourself and repeat the command for each item. Terraback generates the HCL and the import blocks for your whole account in one pass.

Import your whole Azure account in minutes

Terraback scans 80+ Azure resource types and emits clean Terraform plus import blocks, running locally with read-only credentials. $499 once, no SaaS.