Skip to content
Get Started for Free

Terraform

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.

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.

Create the following two files:

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

Terminal window
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "= 5.1.0"
}
# only needed for the `random_uuid` resource
random = {
source = "hashicorp/random"
version = "= 3.9.0"
}
}
}
provider "azurerm" {
features {}
metadata_host = "azure.localhost.localstack.cloud:4566"
subscription_id = "00000000-0000-0000-0000-000000000000"
}

Please note the metadata_host attribute! This is essential to ensure that the infrastructure is deployed to the LocalStack Emulator, instead of to the real cloud.

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

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

Unless you configure other credentials, the azurerm provider signs in through the Azure CLI. Point the Azure CLI at the emulator first, which also signs it in:

Terminal window
lstk az start-interception

You can now use Terraform like you would normally.

First initialize the repository, and download the specified provider:

terraform init

Second, apply (create) the specified resources:

terraform apply

The terraform tool will now create the resource group.

The Azure CLI still targets the emulator, so you can check that the resource group exists:

Terminal window
az group list

Delete the resource group, then point the Azure CLI back at Azure:

Terminal window
terraform destroy
lstk az stop-interception
Was this page helpful?