This directory contains Terraform modules and a deployment script for provisioning Azure services in LocalStack for Azure. For further details about the sample application, refer to the Azure Web App with Azure CosmosDB for MongoDB.
Before deploying this solution, ensure you have the following tools installed:
- LocalStack for Azure: Local Azure cloud emulator for development and testing
- Visual Studio Code: Code editor installed on one of the supported platforms
- Terraform: Infrastructure as Code tool for provisioning Azure resources
- Docker: Container runtime required for LocalStack
- Azure CLI: Azure command-line interface
- Azlocal CLI: LocalStack Azure CLI wrapper
- Python: Python runtime (version 3.12 or above)
- jq: JSON processor for scripting and parsing command outputs
The deploy.sh Bash script uses the azlocal CLI instead of the standard Azure CLI to work with LocalStack. Install it using:
pip install azlocalFor more information, see Get started with the az tool on LocalStack.
The main.tf Terraform module creates the following Azure resources:
- Azure Resource Group: Logical container for all resources in the sample.
- Azure CosmosDB Account (MongoDB API): A globally distributed database account configured for MongoDB workloads, with multi-region failover.
- MongoDB Database: The
sampledbdatabase for storing application data. - MongoDB Collection: The
activitiescollection withinsampledbfor storing vacation activity records. - Azure App Service Plan: The compute resource that hosts the web application.
- Azure Web App: Hosts the Python Flask single-page application (Vacation Planner), connected to CosmosDB for MongoDB.
- App Service Source Control: (Optional) Configures automatic deployment from a public GitHub repository.
The web app allows users to plan and manage vacation activities, storing all activity data in the CosmosDB-backed MongoDB collection. All resources are provisioned and configured using Terraform for easy reproducibility and local development with LocalStack for Azure.
You can use the deploy.sh script to automate the deployment of all Azure resources and the sample application in a single step, streamlining setup and reducing manual configuration. The script executes the following steps:
- Cleans up any previous Terraform state and plan files to ensure a fresh deployment.
- Initializes the Terraform working directory and downloads required plugins.
- Creates and validates a Terraform execution plan for the Azure infrastructure.
- Applies the Terraform plan to provision all necessary Azure resources.
- Extracts resource names and outputs from the Terraform deployment.
- Packages the code of the web application into a zip file for deployment.
- Deploys the zip package to the Azure Web App using the LocalStack Azure CLI.
Before deploying the Terraform modules, update the terraform.tfvars file with your specific values:
location = "westeurope"
cosmosdb_database_name = "sampledb"
cosmosdb_collection_name = "activities"You can set up the Azure emulator by utilizing LocalStack for Azure Docker image. Before starting, ensure you have a valid LOCALSTACK_AUTH_TOKEN to access the Azure emulator. Refer to the Auth Token guide to obtain your Auth Token and specify it in the LOCALSTACK_AUTH_TOKEN environment variable. The Azure Docker image is available on the LocalStack Docker Hub. To pull the Azure Docker image, execute the following command:
docker pull localstack/localstack-azure-alphaStart the LocalStack Azure emulator using the localstack CLI, execute the following command:
export LOCALSTACK_AUTH_TOKEN=<your_auth_token>
IMAGE_NAME=localstack/localstack-azure-alpha localstack startNavigate to the terraform folder:
cd samples/web-app-cosmosdb-mongodb-api/python/terraformMake the script executable:
chmod +x deploy.shRun the deployment script:
./deploy.shAfter deployment, you can use the validate.sh script to verify that all resources were created and configured correctly:
#!/bin/bash
# Variables
ENVIRONMENT=$(az account show --query environmentName --output tsv)
# Choose the appropriate CLI based on the environment
if [[ $ENVIRONMENT == "LocalStack" ]]; then
echo "Using azlocal for LocalStack emulator environment."
AZ="azlocal"
else
echo "Using standard az for AzureCloud environment."
AZ="az"
fi
# Check resource group
$AZ group show \
--name local-rg \
--output table
# List resources
$AZ resource list \
--resource-group local-rg \
--output table
# Check Azure Web App
$AZ webapp show \
--name local-webapp-test \
--resource-group local-rg \
--output table
# Check Azure CosmosDB account
$AZ cosmosdb show \
--name local-mongodb-test \
--resource-group local-rg \
--output table
# Check MongoDB database
$AZ cosmosdb mongodb database show \
--name sampledb \
--account-name local-mongodb-test \
--resource-group local-rg \
--output table
# Check MongoDB collection
$AZ cosmosdb mongodb collection show \
--name activities \
--database-name sampledb \
--account-name local-mongodb-test \
--resource-group local-rg \
--output tableTo destroy all created resources:
# Delete resource group and all contained resources
az group delete --name local-rg --yes --no-wait
# Verify deletion
az group list --output tableThis will remove all Azure resources created by the CLI deployment script.