7

I have already created Azure virtual networks and resource groups manually. I am going to create a VM using Terraform and deploy it via Azure DevOps.My Terraform state file resides in an Azure Storage account. Currently, it is blank. I need to import the existing virtual network and resource groups into the state file. I am able to do the Terraform initialize, plan and apply using the release pipelines but I am not able to find a task in the release pipeline to import terraform. I can import using my VSCode, but I need to do this import using Azure DevOps.

Could someone let me know if that is possible? Thanks in advance

Rtk
  • 103
  • 3
  • 7

4 Answers4

3

This Azure Pipelines extension claims to support Terraform import: Terraform Tasks

WaitingForGuacamole
  • 3,744
  • 1
  • 8
  • 22
1

Can we run terraform import in Azure DevOps release pipeline

I am afraid there is no such out of box task to run terraform import in Azure DevOps release pipeline at this moment.

There is user voice about it on Developer Community:

Support for Terraform Import

It would be great if we are creating infrastructure via the Azure Portal to then be able to bring those resources into Terraform state management via a simple Terraform import task rather than attempting to call this in a PowerShell/Bash/Azure CLI script. Currently the Terraform task supports plan, init, apply etc. But not import

You could vote and add your comments for this feedback.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
1

Azure Terrafy is a different way to solve this problem. You would run this against existing infrastructure, and it would generate Terraform for you and add it to a state file.

https://github.com/Azure/aztfy

It does have some limitations, which are discussed here:

https://techcommunity.microsoft.com/t5/itops-talk-blog/azure-terrafy-import-your-existing-azure-infrastructure-into/ba-p/3357653

james.garriss
  • 12,959
  • 7
  • 83
  • 96
0

You could... or you could setup your local terraform to read your state in AZ and then, import locally, saved to AZ and you're done.

Something along the lines of:

terrafrom init -backend-config=storage_account_name=<storageaccountname> -backend-config=container_name=<container_name> -backend-config="key=statefile.terraform-state" -backend-config=resource_group_name=<rg-name> -backend-config=subscription_id=<subid> -backend-config=tenant_id=<tenantid>

As so:

terrafrom init -backend-config=storage_account_name=terraform_state -backend-config=container_name=dev -backend-config="key=dev-app-uk.terraform-state" -backend-config=resource_group_name=terraform-state -backend-config=subscription_id=0000-0000-0000-0000 -backend-config=tenant_id=9999-9999-9999-9999

Then import your resources as so, and it will save right up to AZ all imported

Stuart.Sklinar
  • 3,683
  • 4
  • 35
  • 89