Azure Terrafy – Import your existing Azure infrastructure into Terraform HCL (2024)

When working with Infrastructure as Code (IaC) it’s difficult to know sometimes where to start. You have a couple of options, go to the Terraform on Azure documentation, then figure out how to write some Terraform templates. Or you can start with a tool like NubesGen that allows you to build your Terraform files (and Bicep) from an easy set of options, spitting out a Terraform template that includes all of the best practices.

Those options are great when you’re wanting to create new infrastructure, but previously, you couldn’t import your existing infrastructure into Terraform. Ah, but now you can! Introducing Azure Terrafy!

Azure Terrafy allows you to easily and quickly import your existing Azure infrastructure into Terraform HCL AND import it into your Terraform state (Learn about state files here. This has been one of the BIGGEST and coolest feature releases that all of you in the community (and myself) have been asking for! It’s great to adopt new working tools and practices for when we start a new project, but every single customer I work with wants to know how to manage their legacy infrastructure. The biggest challenge we face in ITOps is managing that legacy infrastructure.

Let’s walk through how to get started using Azure Terrafy:

  1. Create a new directory in which to generate the Terraform code into. For this demo, I created a new folder called ‘aztfy-demo’ (seen below).
  1. To start using the aztfy tool, we need to install it locally. There are two options: the latest binaries or running the Go toolchain. You can obtain the latest binaries from the releases area of the GitHub repository or more easily you can run the Go toolchain using a single command:
go install github.com/Azure/aztfy@latest

I opted for the latter option:

Azure Terrafy – Import your existing Azure infrastructure into Terraform HCL (1)

Note: You will also need to ensure that you have Azure CLI installed and have authenticated to Azure

  1. Once installed, run the aztfy command and specify the Azure resource group that you want to import (Note: Currently aztfy only supports importing at the resource group level)

Azure Terrafy – Import your existing Azure infrastructure into Terraform HCL (2)

Aztfy will now begin the import process:

Azure Terrafy – Import your existing Azure infrastructure into Terraform HCL (3)

It will show you the importing resources as it run, along with a progress bar:

Azure Terrafy – Import your existing Azure infrastructure into Terraform HCL (4)

  1. When the import has completed (mine took a few minutes, not enough time to even make a proper coffee), it will display the resources to be imported.

Azure Terrafy – Import your existing Azure infrastructure into Terraform HCL (5)

Aztfy found 250 resources in my resource group along with their dependencies, scrolling through I can proceed to the end of the screen and review my options.From this view we can filter our results, show any errors/recommendations, or import our resources:

Azure Terrafy – Import your existing Azure infrastructure into Terraform HCL (6)

5. Type ‘w’ to import all of the viewed resources. Aztfy will begin to generate your Terraform scripts and it does all of the heavy lifting for you. Not only does it create your Terraform scripts, it will include the dependencies and update the state.

Azure Terrafy – Import your existing Azure infrastructure into Terraform HCL (7)

Once Azure Terrafy has completed, you can open your folder and see the newly generated template files:

Azure Terrafy – Import your existing Azure infrastructure into Terraform HCL (8)

You can open the Azure portal and visually check that the resource naming, dependencies and settings have been imported successfully.

Azure Terrafy – Import your existing Azure infrastructure into Terraform HCL (9)

6. The best way to validate the importing of your resources is by running ‘Terraform Plan’

Azure Terrafy – Import your existing Azure infrastructure into Terraform HCL (10)

This will confirm that your configuration matches your infrastructure. The output will confirm that no changes need to be made.

Azure Terrafy – Import your existing Azure infrastructure into Terraform HCL (11)

Congratulations! You’ve successfully imported your existing infrastructure in Azure into Terraform templates!

While this makes importing existing resources a breeze, there are some things to be conscious of at this point.

  • Naming conventions and reusability. While the templates are able to be used, naming conventions for your resources need to be reconfigured and all elements are defined in a very static way. You will need to manually edit and declare your variables to make the templates more scalable and reusable.
  • State storage. When you import your Terraform files, the state is saved to the local file store, work needs to be done to store it remotely in a more secure way.
  • Code refactoring. Aztfy currently spits out the templates into one main.tf file, splitting out your templates into modules should be considered. You will need to spend some time in VSCode and make the new files more reusable and scalable.
  • Dependencies. Aztfy uses many of the dependencies in your resource group to map those out in the templates (see the 'depends_on' line declared in the main.tf above), much of that can be cleaned up once naming conventions and modules are implemented.
  • Import of Resource Groups. Currently, you can only import resource groups. Expect the product team to look into this, potentially making it available to import vNets or maybe even subscriptions!

There is no such thing as a perfect tool, but the announcement of Azure Terrafy is a huge step in the right direction!

Read more about the aztfy announcement and the new AzAPI Terraform Provider (TL;DR: Day 0 support for ALL Azure resources when creating Terraform templates), the Microsoft team has done an amazing job at removing the need for ARM templates to be embedded in your Terraform templates along with launching this new tool. You can also check out the azty GitHub repo here for more details on how the tool works and new features.

Happy importing!

Azure Terrafy – Import your existing Azure infrastructure into Terraform HCL (2024)

FAQs

How to import existing Azure infrastructure to Terraform? ›

Import Azure Terraform Tutorial — Step-by-Step Guide
  1. Create Cloud Provider Credentials. Start by creating credentials for your Azure account. ...
  2. Launch Import. Terraform import all existing resources. ...
  3. Resource Mapping. ...
  4. Generate Terraform Code. ...
  5. Cleanup and Verification. ...
  6. Final Steps. ...
  7. Design Considerations.
Jun 8, 2024

What is Azure Terrafy? ›

A tool brings existing Azure resources under the management of Terraform.

How do I provision Azure infrastructure with Terraform? ›

  1. Configure your environment. Azure subscription: If you don't have an Azure subscription, create a free account before you begin. ...
  2. Create and apply the Terraform plan. Browse to the Azure portal. ...
  3. Fork the test project. ...
  4. Deploy from GitHub to your deployment slots. ...
  5. Test the app deployments. ...
  6. Swap the two deployment slots.
Aug 13, 2024

How to use existing resource groups in Terraform? ›

To use an existing Azure Resource Group without destroying it, you should import the resource group into your Terraform state.
  1. Remove the Resource Group resource from your code.
  2. Use Data Source to Reference the Resource Group.
  3. Reference the Data Source in Your Resources.
  4. Run terraform apply.
Jan 18, 2024

Is terraform able to import existing infrastructure? ›

To import a simple resource into Terraform, follow the step-by-step guide below.
  • Prepare the EC2 instance. ...
  • Create main.tf and set provider configuration. ...
  • Write config for the resource to be imported. ...
  • Run the import command. ...
  • Observe state files and plan output. ...
  • Improve config to avoid replacement.

What are the two steps to successfully import the existing configuration with terraform? ›

Using configuration to import resources involves the following steps:
  1. Identify the existing infrastructure you will import.
  2. Define an import block for the resources.
  3. Run terraform plan to review the import plan and optionally generate configuration for the resources.

What is Terraform import? ›

`terraform import` lets you target a resource that already exists, and map it to a resource you've defined in code. Grab the Terraform cheat sheet. Check out the top 10 Terraform commands and get a full rundown of all the basic commands you need to get the most out of Terraform in our Terraform cheat sheet.

What is Terra Azure? ›

Terra is Broad's flagship biomedical data platform, co-developed by Broad, Microsoft, and Verily. The open-source platform is used by over 65,000 individuals globally for biomedical data analysis, secure data sharing, and cross-enterprise data science collaborations.

What is Azure called now? ›

Azure AD is now Microsoft Entra ID

All licensing and functionality remain the same.

How to integrate Azure with Terraform? ›

Quickstart
  1. Create an Azure resource group using Terraform.
  2. Create an AKS cluster.
  3. Create a Linux VM.
  4. Create a Windows VM.
  5. Create an Azure key vault and key using Terraform.
  6. Direct web traffic with Azure Application Gateway - Terraform.
  7. Create a single database in Azure SQL Database using Terraform.

How do I migrate infrastructure to Azure? ›

Start your migration by discovering and assessing on-premises resources using the free Azure Migrate tool. Then plan your move with technical and business insights into infrastructure, databases, and applications.

How do I generate a Terraform from an existing Azure resource? ›

The Import Process on Terraform

Write a Terraform resource block in your configuration file that corresponds to the resource you created. Make sure the name and type match the Azure resource. The simplest way to achieve this is by adding the import block of code into your tf file.

How to import resources in Terraform Azure? ›

Importing resources
  1. Overview. Another common scenario is importing resources that have been created manually. ...
  2. Starting point. Your files should look similar to this: ...
  3. Create the resource. ...
  4. Check for no diff. ...
  5. Refresh state (only if required) ...
  6. Import into state. ...
  7. Check the diff. ...
  8. Update the config files.

Can we have multiple providers in Terraform? ›

alias : Multiple Provider Configurations

You can optionally define multiple configurations for the same provider, and select which one to use on a per-resource or per-module basis.

How do I import an existing IAM role in terraform? ›

Import
  1. Add resource block to . tf file. ...
  2. Import IAM Policy. Here is the command and the result example. ...
  3. Import IAM Role. Here is the command and the result example. ...
  4. Check the tfstate file. ...
  5. Update main.tf in IAM Policy and Role module directory. ...
  6. Plan to check differences.
Apr 25, 2024

What CLI tool to generate terraform files from existing infrastructure? ›

Terraformer is a CLI level tool that allows you to easily create terraform scripts with already existing resources in your environment. Performing the reverse of what terraform is designed to do, this tool can be thought of as Infrastructure to Code.

How to import an existing S3 bucket in terraform? ›

  1. execute terraform import, something like this: terraform import aws_s3_bucket.my_bucket my-existing-bucket-name.
  2. do terraform plan.
  3. if your bucket still trying to be recreated, check terraform plan output and you will see how to update your code.
Mar 28, 2023

How do I import terraform configuration? ›

To import a resource using import blocks, you must:
  1. Define an import block for the resource(s).
  2. Add a corresponding resource block to your configuration , or generate configuration for that resource.
  3. Run terraform plan to review how Terraform will import the resource(s).

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 5775

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.