Skip to content

Latest commit

 

History

History
194 lines (146 loc) · 8.53 KB

File metadata and controls

194 lines (146 loc) · 8.53 KB

Bicep Deployment

This directory contains the Bicep template and a deployment script for provisioning Azure services in LocalStack for Azure. Refer to the Azure Web App with Managed Identity guide for details about the sample application.

Prerequisites

Before deploying this solution, ensure you have the following tools installed:

Installing lstk CLI

Deploying to LocalStack requires the lstk CLI, which routes Azure CLI commands to the emulator (run lstk az start-interception before deploying). Install it using Homebrew:

brew install localstack/tap/lstk

or npm:

npm install -g @localstack/lstk

Alternatively, download a pre-built binary from the lstk releases page. For more information, see the lstk CLI documentation and the lstk GitHub repository.

Architecture Overview

The deploy.sh script creates the Azure Resource Group for all the Azure resources, while the main.bicep Bicep module creates the following Azure resources:

  1. Azure Storage Account: Provides blob storage for persisting vacation activity data. The web application stores each activity as a JSON blob file in the activities container.
  2. Azure App Service Plan: Defines the compute resources (CPU, memory, and scaling options) that host the web application.
  3. Azure Web App: Hosts the Python Flask-based Vacation Planner application. The web app uses managed identity to securely access the Azure Storage Account without requiring explicit credentials.
  4. Managed Identity: Provides secure, credential-free authentication between the web app and storage account. Supports both system-assigned and user-assigned identity types.
  5. Role Assignment: Grants the web app's managed identity the Storage Blob Data Contributor role, enabling read/write access to blob containers.
  6. App Service Source Control: (Optional) Enables continuous deployment from a Git repository for automated application updates.

The web app allows users to plan and manage vacation activities, storing all activity data as blob files in the activities containers in the Azure Storage Account. For more information, see Azure Web App with Managed Identity.

Provisioning Scripts

See deploy.sh for the complete deployment script. The script performs the following operations:

  • Detects environment (LocalStack or Azure Cloud) and selects appropriate CLI
  • Creates resource group if it doesn't exist
  • Validates Bicep template syntax and parameters
  • Optionally runs what-if deployment for preview
  • Deploys Bicep template to create all Azure resources
  • Extracts deployment outputs (resource names, URLs) using jq
  • Packages web application code into zip file
  • Deploys zip package to Azure Web App
  • Cleans up temporary artifacts

Deployment

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

Start the LocalStack Azure emulator with the lstk CLI:

# Set the authentication token
export LOCALSTACK_AUTH_TOKEN=<your_auth_token>

# Start the LocalStack Azure emulator
lstk start --type azure

# Route all Azure CLI calls to the LocalStack Azure emulator
lstk az start-interception

Navigate to the bicep folder:

cd samples/web-app-managed-identity/python/bicep

Make the script executable:

chmod +x deploy.sh

Run the deployment script:

./deploy.sh

Validation

After deployment, you can use the validate.sh script to verify that all resources were created and configured correctly:

#!/bin/bash

# Variables
PREFIX='local'
SUFFIX='test'
RESOURCE_GROUP_NAME="${PREFIX}-rg"
STORAGE_ACCOUNT_NAME="${PREFIX}storage${SUFFIX}"
CONTAINER_NAME='activities'
APP_SERVICE_PLAN_NAME="${PREFIX}-app-service-plan-${SUFFIX}"
WEB_APP_NAME="${PREFIX}-webapp-${SUFFIX}"
MANAGED_IDENTITY_NAME="${PREFIX}-identity-${SUFFIX}"

# 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[$WEB_APP_NAME] web app:\n"
az webapp show \
	--name "$WEB_APP_NAME" \
	--resource-group "$RESOURCE_GROUP_NAME" \
	--query '{Name:name,State:state,Location:location,DefaultHostName:defaultHostName}' \
	--output table \
	--only-show-errors

# Check user-assigned managed identity
echo -e "\n[$MANAGED_IDENTITY_NAME] managed identity:\n"
az identity show \
	--name "$MANAGED_IDENTITY_NAME" \
	--resource-group "$RESOURCE_GROUP_NAME" \
	--query '{Name:name,Location:location,ResourceGroup:resourceGroup,ClientId:clientId,PrincipalId:principalId}' \
	--output table \
	--only-show-errors

# Check storage account
echo -e "\n[$STORAGE_ACCOUNT_NAME] storage account:\n"
az storage account show \
	--name "$STORAGE_ACCOUNT_NAME" \
	--resource-group "$RESOURCE_GROUP_NAME" \
	--query '{Name:name,Location:location,ResourceGroup:resourceGroup,Kind:kind,Sku:sku.name}' \
	--output table \
	--only-show-errors

# List storage containers
echo -e "\n[$STORAGE_ACCOUNT_NAME] storage containers:\n"
az storage container list \
	--account-name "$STORAGE_ACCOUNT_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-errors

Cleanup

To 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 table

This will remove all Azure resources created by the CLI deployment script.

Related Documentation