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 Terraform modules create the following Azure resources:
- Azure Resource Group: A logical container scoping all resources in this sample.
- Azure Virtual Network: Hosts two subnets:
- app-subnet: Dedicated to regional VNet integration with the Function App.
- pe-subnet: Used for hosting Azure Private Endpoints.
- Azure Private DNS Zone: Handles DNS resolution for the CosmosDB for MongoDB Private Endpoint within the virtual network.
- Azure Private Endpoint: Secures network access to the CosmosDB for MongoDB account via a private IP within the VNet.
- Azure NAT Gateway: Provides deterministic outbound connectivity for the Web App. Included for completeness; the sample app does not call any external services.
- Azure Network Security Group: Enforces inbound and outbound traffic rules across the virtual network's subnets.
- Azure Log Analytics Workspace: Centralizes diagnostic logs and metrics from all resources in the solution.
- Azure Cosmos DB for MongoDB: A globally distributed database account optimized for MongoDB workloads, with multi-region failover enabled.
- MongoDB Database: The
sampledbdatabase that holds all application data. - MongoDB Collection: The
activitiescollection withinsampledb, used to store vacation activity records. - Azure App Service Plan: The underlying compute tier that hosts the web application.
- Azure Web App: Runs the Python Flask single-page application (Vacation Planner), connected to CosmosDB for MongoDB via VNet integration.
- App Service Source Control: (Optional) Configures continuous deployment from a public GitHub repository.
The web app enables users to plan and manage vacation activities, with all data persisted in a CosmosDB-backed MongoDB collection. For more information on the sample application, see Azure Web App with Azure CosmosDB for MongoDB.
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.
When using LocalStack for Azure, configure the metadata_host and subscription_id settings in the Azure Provider for Terraform to ensure proper connectivity:
provider "azurerm" {
features {
resource_group {
prevent_deletion_if_contains_resources = false
}
}
# Set the hostname of the Azure Metadata Service (for example management.azure.com)
# used to obtain the Cloud Environment when using LocalStack's Azure emulator.
# This allows the provider to correctly identify the environment and avoid making calls to the real Azure endpoints.
metadata_host="localhost.localstack.cloud:4566"
# Set the subscription ID to a dummy value when using LocalStack's Azure emulator.
subscription_id = "00000000-0000-0000-0000-000000000000"
}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:
# Set the authentication token
export LOCALSTACK_AUTH_TOKEN=<your_auth_token>
# Start the LocalStack Azure emulator
IMAGE_NAME=localstack/localstack-azure-alpha localstack start -d
localstack wait -t 60
# Route all Azure CLI calls to the LocalStack Azure emulator
azlocal start-interceptionNavigate to the terraform folder:
cd samples/web-app-cosmosdb-mongodb-api/python/terraformMake the script executable:
chmod +x deploy.shRun the deployment script:
./deploy.shOnce the deployment completes, run the validate.sh script to confirm that all resources were provisioned and configured as expected:
#!/bin/bash
# Variables
PREFIX='local'
SUFFIX='test'
RESOURCE_GROUP_NAME="${PREFIX}-rg"
LOG_ANALYTICS_NAME="${PREFIX}-log-analytics-${SUFFIX}"
WEBAPP_SUBNET_NSG_NAME="${PREFIX}-webapp-subnet-nsg-${SUFFIX}"
PE_SUBNET_NSG_NAME="${PREFIX}-pe-subnet-nsg-${SUFFIX}"
NAT_GATEWAY_NAME="${PREFIX}-nat-gateway-${SUFFIX}"
VIRTUAL_NETWORK_NAME="${PREFIX}-vnet-${SUFFIX}"
PRIVATE_DNS_ZONE_NAME="privatelink.mongo.cosmos.azure.com"
PRIVATE_ENDPOINT_NAME="${PREFIX}-mongodb-pe-${SUFFIX}"
APP_SERVICE_PLAN_NAME="${PREFIX}-app-service-plan-${SUFFIX}"
WEBAPP_NAME="${PREFIX}-webapp-${SUFFIX}"
COSMOSDB_ACCOUNT_NAME="${PREFIX}-mongodb-${SUFFIX}"
MONGODB_DATABASE_NAME="sampledb"
COLLECTION_NAME="activities"
# Check resource group
echo -e "[$RESOURCE_GROUP_NAME] resource group:\n"
az group show \
--name "$RESOURCE_GROUP_NAME" \
--output table \
--only-show-errors
# Check App Service Plan
echo -e "\n[$APP_SERVICE_PLAN_NAME] app service plan:\n"
az appservice plan show \
--resource-group "$RESOURCE_GROUP_NAME" \
--name "$APP_SERVICE_PLAN_NAME" \
--output table \
--only-show-errors
# Check Azure Web App
echo -e "\n[$WEBAPP_NAME] web app:\n"
az webapp show \
--name "$WEBAPP_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--output table \
--only-show-errors
# Check Azure CosmosDB account
echo -e "\n[$COSMOSDB_ACCOUNT_NAME] cosmosdb account:\n"
az cosmosdb show \
--name "$COSMOSDB_ACCOUNT_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query '{Name:name,Location:location,ResourceGroup:resourceGroup,DocumentEndpoint:documentEndpoint}' \
--output table \
--only-show-errors
# Check MongoDB database
echo -e "\n[$MONGODB_DATABASE_NAME] mongodb database:\n"
az cosmosdb mongodb database show \
--name "$MONGODB_DATABASE_NAME" \
--account-name "$COSMOSDB_ACCOUNT_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query '{Name:name,ResourceGroup:resourceGroup}' \
--output table \
--only-show-errors
# Check MongoDB collection
echo -e "\n[$COLLECTION_NAME] mongodb collection:\n"
az cosmosdb mongodb collection show \
--name "$COLLECTION_NAME" \
--database-name "$MONGODB_DATABASE_NAME" \
--account-name "$COSMOSDB_ACCOUNT_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--output table \
--only-show-errors
# Check Log Analytics Workspace
echo -e "\n[$LOG_ANALYTICS_NAME] log analytics workspace:\n"
az monitor log-analytics workspace show \
--resource-group "$RESOURCE_GROUP_NAME" \
--workspace-name "$LOG_ANALYTICS_NAME" \
--query '{Name:name,Location:location,ResourceGroup:resourceGroup}' \
--output table \
--only-show-errors
# Check NAT Gateway
echo -e "\n[$NAT_GATEWAY_NAME] nat gateway:\n"
az network nat gateway show \
--name "$NAT_GATEWAY_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--output table \
--only-show-errors
# Check Virtual Network
echo -e "\n[$VIRTUAL_NETWORK_NAME] virtual network:\n"
az network vnet show \
--name "$VIRTUAL_NETWORK_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--output table \
--only-show-errors
# Check Private DNS Zone
echo -e "\n[$PRIVATE_DNS_ZONE_NAME] private dns zone:\n"
az network private-dns zone show \
--name "$PRIVATE_DNS_ZONE_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query '{Name:name,ResourceGroup:resourceGroup,RecordSets:recordSets,VirtualNetworkLinks:virtualNetworkLinks}' \
--output table \
--only-show-errors
# Check Private Endpoint
echo -e "\n[$PRIVATE_ENDPOINT_NAME] private endpoint:\n"
az network private-endpoint show \
--name "$PRIVATE_ENDPOINT_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--output table \
--only-show-errors
# Check Web App Subnet NSG
echo -e "\n[$WEBAPP_SUBNET_NSG_NAME] network security group:\n"
az network nsg show \
--name "$WEBAPP_SUBNET_NSG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--output table \
--only-show-errors
# Check Private Endpoint Subnet NSG
echo -e "\n[$PE_SUBNET_NSG_NAME] network security group:\n"
az network nsg show \
--name "$PE_SUBNET_NSG_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--output table \
--only-show-errors
# List resources
echo -e "\n[$RESOURCE_GROUP_NAME] all resources:\n"
az resource list \
--resource-group "$RESOURCE_GROUP_NAME" \
--output table \
--only-show-errorsTo 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.