Terraform adding control of an object not created through Terraform

March 11, 2022

In recent investigations into Terraform, I came across a situation where I’d created a resource not known to Terraform, but which I now wished to manage through it. Initially, I simply tried to create the resource. Interestingly, this goes through the plan stage, but errors at the apply stage with something similar to the following error:

│ Error: A resource with the ID “/subscriptions/34a7cc79-9bea-4d04-86a1-01f3b260bdb0/resourceGroups/ServiceBusTest/providers/Microsoft.Logic/workflows/NotifyDeadLetter” already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for “azurerm_logic_app_workflow” for more information.
│ │ with azurerm_logic_app_workflow.logicapp, │ on basic.tf line 22, in resource “azurerm_logic_app_workflow” “logicapp”: │ 22: resource “azurerm_logic_app_workflow” “logicapp” {

This is a really useful error - if you do, in fact, look at the docs for that resource then you’ll see at the end it gives the following way of importing:



terraform import azurerm\_logic\_app\_workflow.workflow1 /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Logic/workflows/workflow1

So, simply replace the values that you’ve been given in the error message and you end up with:



terraform import azurerm\_logic\_app\_workflow.logicapp /subscriptions/34a7cc79-9bea-4d04-86a1-01f3b260bdb0/resourceGroups/ServiceBusTest/providers/Microsoft.Logic/workflows/NotifyDeadLetter


The result of running this is:

PS C:\Users\pcmic\source\repos\tf-logicapp-test> terraform import azurerm_logic_app_workflow.logicapp /subscriptions/34a7cc79-9bea-4d04-86a1-01f3b260bdb0/resourceGroups/ServiceBusTest/providers/Microsoft.Logic/workflows/NotifyDeadLetter azurerm_logic_app_workflow.logicapp: Importing from ID “/subscriptions/34a7cc79-9bea-4d04-86a1-01f3b260bdb0/resourceGroups/ServiceBusTest/providers/Microsoft.Logic/workflows/NotifyDeadLetter”… azurerm_logic_app_workflow.logicapp: Import prepared! Prepared azurerm_logic_app_workflow for import azurerm_logic_app_workflow.logicapp: Refreshing state… [id=/subscriptions/34a7cc79-9bea-4d04-86a1-01f3b260bdb0/resourceGroups/ServiceBusTest/providers/Microsoft.Logic/workflows/NotifyDeadLetter]

Import successful!

The resources that were imported are shown above. These resources are now in your Terraform state and will henceforth be managed by Terraform.

This now adds that resource into your state file (.tfstate):



    {
      "mode": "managed",
      "type": "azurerm\_logic\_app\_workflow",
      "name": "logicapp",
. . .



Profile picture

A blog about one man's journey through code… and some pictures of the Peak District
Twitter

© Paul Michaels 2024