Introduction
In this blog, we will learn how to import Azure resources into Terraform state, view objects in Terraform state, and migrate your state to Terraform Cloud. Before we dive deep, lets understand some important concept about Terraform Migration.
The Terraform Migration Concept
Terraform Cloud also known as HPC Terraform offers secure remote state storage, makes it easier to collaborate on infrastructure development. You can as well migrate your state to HCP Terraform without interrupting services or recreating your existing infrastructure.
Migrating your Terraform Statefile to Terraform Cloud offers numerous benefits, including secure, centralized state storage with state locking to prevent conflicts, and multi-user access with granular permissions and automatic state versioning for better collaboration.
It ensures enhanced security and compliance through encrypted state files and detailed audit logs. The integration with version control systems and automated workflows, along with policy enforcement, streamline CI/CD processes.
Terraform Cloud provides high availability, redundancy, and efficient multi-environment management, making it scalable and reliable. Additionally, it offers resource usage insights, cost estimation, and optimization for effective cost management.
Before we begin this project, ensure you follow the prerequisite listed below.
Prerequisite
An Azure account with an active subscription. You can also create a free account here if you do not have one.
Install Terraform.. Visit the official terraform website to install and configure terraform.
Download and install Visual Studio Code. Install the terraform extension on visual as well
Ensure you have Azure CLI installed on your host machine to enable you login to azure through the CLI
An Hashicorp Terraform registry account is also needed to create your organization.
An existing resource group created in Azure.
Create a Terraform Configuration Folder or an Existing Folder containing your configuration.
Open visual studio code in new windows
Navigate to "Open Folder" as shown in the picture below.
Click on create "New Folder" and give your folder a name. Ensure the new folder has been named.
Then click on "Select Folder"
5)From the screenshot below, within the created folder, click on the file icon to create a main.tf file.
The main.tf file contains the main set of configuration for your module.
Declare Your Provider Configuration
Lets break it down..
1) The first lines of code we need to input in our main.tf file is the azurerm provider. The AzureRM Terraform Provider allows for the management of resources within Azure Resource Manager.
To get the latest version of azurerm provider, click on this link
2) The Azure Provider allows the behaviour of certain resources to be configured using the features block.
3) Line 12: <skip_provider_registration = true> manages the registration of a Resource Provider which allows access to the API's supported by this Resource Provider.
4) The <"azurerm_resource_group" "rg"> manages the resource group.
5) As seen in Lines 18 and 19. This argument specifies the location and the name of our existing resource group.
7) On visual studio code, click on the (3 dots) ellipsis
8) Select the terminal button, and then new terminal
Authenticate Your Azure Account
1)To login to your azure account, you need to input the az login command.
2) You will be prompted to login into your azure account
3) Once you are signed in, a success output would be seen on your terminal.
4) To initialize the Terraform Azurerm provider, you run the "terraform init" command
5) From the screenshot provided below, our working directory as been successfully initialized.
Import The Existing Resource Group
1) To import the existing resource group from azure input the command
<terraform import azurerm_resource_group.(name) resource id>
2) This ensures that terraform doesn't create another resource group when we deploy our infrastructure. To see the steps to get your resource ID in Azure check here
3) If the import is successful, you will get the "Import successful' as shown below. This will create a new terraform statefile.
4) To view the objects in the statefile, you can run the "terraform show" command
Migrate Terraform State to Terraform Cloud
1)Open a new browser window or tab.
2) In the new window or tab, enter https://app.terraform.io/ into the address bar to navigate to Terraform Cloud and log in to your account.
3) Click on the "+create organization" at the the top left corner.
4) Give your organization a name and create an organization.
5) To create a workspace, click on the CLI-Driven Workflow
6)Give your workspace a name and click on the create button.
7) Copy the highlighted code block has shown in the diagram.
8) Navigate back to visual studio code.
9) Paste the code block into your main.tf in the terraform block under the required_providers block.
10) Go back to your terminal and run the "terraform login" command to login to terraform cloud
11) When prompted to enter a value, enter yes. This will navigate you to Terraform Cloud to request an API token.
12) You will be prompted to generate a token. Once you generate the token, copy the token.
13)Gently right-click to paste the code in space shown in the below screenshot.
14) Enter the terraform init command and click on yes.
15) The success state shows terraform state has been migrated. To confirm this, we need to go and verify.
16) Here we have our statefile migrated successfully to our workspace.
17) Now that we have our remote state in terraform cloud, we can go ahead and delete our terraform state. Run the "rm terraform.tfstate" command to delete it.
18) As shown here, the statefile has successfully been deleted.
Thank you!!