How to Create Azure Resources with Terraform and GitHub and How to Migrate Terraform State to Terraform Cloud: A Step-by-Step Guide.

Teslim LawalTeslim Lawal
3 min read

If you're building in the cloud, repeatable and version-controlled infrastructure is essential. With Terraform and GitHub, you can provision Azure resources in a clean, automated, and auditable way.

In this post, we’ll show you how to go from zero to deployed Azure infrastructure using Terraform, GitHub, and optionally Terraform Cloud or GitHub Actions.

Prerequisites

To follow along, you’ll need:

Step 1:

Create a repository in GitHub (see https://hashnode.com/post/cm88y3kw2000109jvfu4n209h)

Step 2:

Clone your repository from Visual Studio Code.

Select the "clone repository option"

Select the repository created in GitHub.

Step 3:

Create a new file (main.tf)

Step 4:

Go to the Terraform official site to select the Azure provider: https://registry.terraform.io/browse/providers.

Copy the code and paste it in the main.tf file in Visual Studio Code.

This is how your code should ultimately look. NB: Save your code (Ctrl+S)

Step 5:

Open a new terminal in Visual Studio Code.

First, authenticate Terraform with your Azure account:

az login

Set your desired subscription (if needed):

az account set --subscription "YOUR_SUBSCRIPTION_ID"

In the terminal, initialize the Terraform project:

terraform init

This indicates successful initialization.

Add your resource group, virtual network, subnet, public IP, etc. to the main.tf code.

terraform plan -out main.tfplan

Apply it.

terraform apply main.tfplan

Confirm with yes when prompted.

This shows a successful “terraform apply.”

Navigate to Azure to check out the resources.

Step 6:

Log in to your Terraform Cloud account and create an organization.

Give the organization a unique name related to the project and click “create organization.”

Create a new workspace by selecting “CLI-driven workflow.”

Give your workspace a name and click Create.

Step 7:

Copy the example code as highlighted in the image below.

Go back to Visual Studio Code and update your main.tf code as shown below.

NB: Save your code (Ctrl+S)

Generate a user token.

Copy your token password.

In the terminal, run:

terraform login

Confirm with yes when prompted.

Paste the token by right-clicking once only and clicking “enter.”

This indicates a successful login.

In the terminal, run:

terraform init

Confirm with yes when prompted (Should Terraform migrate your existing state?)

This indicates a success.

terraform plan

Click as shown below.

Select your organization and click on the workspace.

This indicates a successful migration

Step 8: Clean Up Local State

After confirming that the migration was successful, you can safely delete:

  • .terraform/ directory

  • terraform.tfstate

  • terraform.tfstate.backup

This prevents any confusion between local and remote state in the future.

rm terraform.tfstate 
rm terraform.tfstate.backup

Run:

terraform destroy

You're All Set!

Congratulations! You've successfully migrated your Terraform state to Terraform Cloud. Enjoy safer, more scalable infrastructure management with better collaboration, visibility, and control.

0
Subscribe to my newsletter

Read articles from Teslim Lawal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Teslim Lawal
Teslim Lawal