Import Existing Resources into Terraform from Azure

15 minutes
  • 3 Learning Objectives

About this Hands-on Lab

Hey, Gurus! Welcome to the lab. In this lab, we will cover three objectives. First, we will log into the Azure portal and configure the Cloud Shell to use Bash. Second, we will download and run a script to set up the lab environment. And for our third objective, we will import our existing Azure resource group into our Terraform state, so we can use it to manage our resources. This is something that you will run into when an organization has already established a presence with a cloud provider like Azure and decides to start using Terraform to manage and create infrastructure. More than likely, you will be working with existing resources that you will want to manage or make changes to with Terraform.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Set Up Cloud Shell
  1. Go to the Azure portal and log in using your lab credentials.
  2. Click the Cloud Shell icon next to the search bar in the portal.
  3. Select Bash at the prompt.
  4. Click Show Advanced Settings.
  5. Set the Cloud Shell region to the same location as the resource group.
  6. Select the existing Resource Group, and select Use Existing for the Storage Account.
  7. In the File share section, choose Create new and enter terraform.
  8. Click Create Storage.
Set Up Lab Environment
  1. Download the script at
  2. Add execute permissions to the script.
  3. Run the script.
  4. Move to the terraform directory.
  5. Initialize the working directory.
Import the Resource Group into Terraform
  1. In the Cloud Shell, create a file called
  2. Add the following resource block and save the file:
    resource "azurerm_resource_group" "guru" {}
  3. Run the az group list command to get the subscription id.
  4. Import your resource group into Terraform.
  5. Add to the file so it looks like the code below (fill in with your resource group and location):
    resource "azurerm_resource_group" "guru" {
        name     = "<RESOURCE_GROUP>"
        location = "<LOCATION>"
        tags     = {
            environment = "demo"
  6. Save the file.
  7. Apply your changes and confirm.

Additional Resources

You are picking up a project that a co-worker barely started before they left the company. They were in charge of building out a new development environment for testing a new application. They got as far as creating a new resource group for the development environment, but that was as far as they got. You decide to use Terraform to build out the rest of the environment.

You will need to import the resource group into your Terraform state so you can manage this resource with Terraform as well as use the resource group in your Terraform configurations. You’ve got the Azure portal pulled up in your browser. You log in to the portal and set up the Cloud Shell, so you are ready to rock and roll. You will create a configuration file called and add an empty resource block for your resource group. You will then import the resource and add the values for your resource group name and location to your resource block. After you have done that, apply the changes and check your state file to make sure Terraform is managing your resource.

Now that we have a plan, let’s put it into action!

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?