Terraform

Get started with the terraform tool on LocalStack

Introduction

Terraform is an Infrastructure-as-Code (IaC) tool that can deploy your full infrastructure with a few simple commands, in a reproducible manner. This guide will show you how to use it with LocalStack.

Getting started

This guide is designed for users who are new to LocalStack for Azure emulator and assumes basic knowledge of how Terraform works. We will demonstrate how to create an Azure resource group using Terraform.

Install the packages

Run the following command to install the required packages:

$ pip install azlocal

You now have access to the following LocalStack tools:

CLI toolLocalStack toolPurpose
azazlocalInteract with Azure resources
azdazdlocalDeploy ARM/Bicep templates
terraformtflocalDeploy Terraform templates
funcfunclocalDeploy Azure Functions

The LocalStack variants are wrappers around the existing tools, so you keep the full functionality of the original tool - it will just redirect all commands to the running LocalStack Emulator.

Terraform configuration

Create the following two files:

A Terraform template with the provider information called provider.tf:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=4.14.0"
    }
  }
}

provider "azurerm" {
  features {}
  subscription_id = "00000000-0000-0000-0000-000000000000"
}

A Terraform file that contains the resource group information called main.tf:

resource "random_uuid" "uuid" {}

resource "azurerm_resource_group" "rg" {
  name     = "rg-hello-tf-${random_uuid.uuid.result}"
  location = "westeurope"
}

Deploy Terraform

First, lets: login to Azure:

azlocal login

Now that we’re logged in, we can deploy this infrastructure using the tflocal tool, using the wellknown Terraform commands.

First initialize the repository, and download the specified provider:

tflocal init

Second, apply (create) the specified resources:

tflocal apply

The tflocal tool will now create the resource group.

You can verify that the resource exist by using the azlocal tool:

azlocal group list

The template shown here is very basic, but tflocal can be used to deploy any other resource that the LocalStack Azure Emulator supports. Happy deploying!