Skip to content

Latest commit

 

History

History
270 lines (209 loc) · 11.9 KB

File metadata and controls

270 lines (209 loc) · 11.9 KB

Terraform Deployment

This directory contains Terraform modules and deploy.sh deployment script for creating Azure services. For more information, see Azure Functions Sample with LocalStack for Azure.

Prerequisites

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
  • zip: Packages the code of the functions; the deployment's remote build installs the packages of requirements.txt
  • Docker: Container runtime required for LocalStack
  • Azure CLI: Azure command-line interface
  • lstk CLI: LocalStack command-line interface (proxies the Azure CLI via lstk az)
  • jq: JSON processor for scripting and parsing command outputs

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 main.tf Terraform module creates the following Azure resources:

  1. Azure Resource Group: Logical container for all gaming system resources
  2. Azure Storage Account: Provides blob containers, queues, and tables for the gaming system
  3. Azure App Service Plan: Hosting plan for the Azure Functions application
  4. Azure Linux Function App: Hosts the gaming logic on the App Service plan
  5. Log Analytics Workspace: Stores the telemetry of the Application Insights resource, and the logs and metrics that the diagnostic settings send to it.
  6. Application Insights: A workspace-based resource that collects the invocations of the functions and the log records of the functions and of the Functions host, which the host sends to the connection string of the APPLICATIONINSIGHTS_CONNECTION_STRING app setting.
  7. Diagnostic Settings: Send the resource logs (FunctionAppLogs) and the platform metrics of the function app, and the platform metrics of the App Service plan, to the Log Analytics workspace.

The system implements a complete gaming scoreboard with multiple Azure Functions that handle HTTP requests, process blob uploads, manage queue messages, and maintain game statistics.

Provisioning Scripts

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

  • Runs the standard terraform and az CLIs (run lstk az start-interception beforehand to route all Azure CLI calls to the LocalStack emulator)
  • Initializes Terraform and downloads required providers
  • Creates and validates Terraform execution plan with custom variables
  • Applies Terraform configuration to provision Azure resources
  • Extracts output values (resource group name, function app name)
  • Creates the deployment zip package of function_app.py, host.json and requirements.txt; the remote build of the deployment installs the packages
  • Deploys the zip to Azure Function App using Azure CLI

Configuration

providers.tf hardcodes no endpoint and no subscription. deploy.sh exports the variables the Azure Provider for Terraform reads natively, so the same files run unmodified against the LocalStack emulator and against Azure:

Variable Exported when Value
ARM_SUBSCRIPTION_ID, ARM_TENANT_ID always az account show
ARM_METADATA_HOSTNAME the active az cloud is LocalStack host of az cloud show --query endpoints.resourceManager

The provider discovers every endpoint of the emulator from ARM_METADATA_HOSTNAME, and authenticates with the Azure CLI session on both targets. Against Azure, deploy.sh unsets ARM_METADATA_HOSTNAME, so the provider uses the endpoints of the public cloud. Running terraform directly, rather than through deploy.sh, needs these variables exported.

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 terraform folder:

cd samples/function-app-storage-http/python/terraform

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="${PREFIX:-local}"
SUFFIX="${SUFFIX:-test}"
FUNCTION_APP_NAME="${PREFIX}-functionapp-${SUFFIX}"
STORAGE_ACCOUNT_NAME="${PREFIX}storage${SUFFIX}"
RESOURCE_GROUP_NAME="${PREFIX}-rg"
LOG_ANALYTICS_NAME="${PREFIX}-log-analytics-${SUFFIX}"
APPLICATION_INSIGHTS_NAME="${PREFIX}-insights-${SUFFIX}"

# Check resource group
az group show \
  --name $RESOURCE_GROUP_NAME \
  --output table

# List resources
az resource list \
  --resource-group $RESOURCE_GROUP_NAME \
  --output table

# Check function app status
az functionapp show  \
  --name $FUNCTION_APP_NAME \
  --resource-group $RESOURCE_GROUP_NAME \
  --output table

# Check storage account properties
az storage account show \
  --name $STORAGE_ACCOUNT_NAME \
  --resource-group $RESOURCE_GROUP_NAME \
  --output table

# Check Log Analytics workspace
az monitor log-analytics workspace show \
  --resource-group $RESOURCE_GROUP_NAME \
  --workspace-name $LOG_ANALYTICS_NAME \
  --query '{Name:name,Location:location,ResourceGroup:resourceGroup,Sku:sku.name,RetentionInDays:retentionInDays}' \
  --output table \
  --only-show-errors

# Check Application Insights component
az monitor app-insights component show \
  --app $APPLICATION_INSIGHTS_NAME \
  --resource-group $RESOURCE_GROUP_NAME \
  --query '{Name:name,Location:location,ResourceGroup:resourceGroup,ApplicationType:applicationType,WorkspaceResourceId:workspaceResourceId}' \
  --output table \
  --only-show-errors

# Check the diagnostic settings of the function app and of its App Service plan
FUNCTION_APP_ID=$(az functionapp show \
  --name "$FUNCTION_APP_NAME" \
  --resource-group "$RESOURCE_GROUP_NAME" \
  --query id \
  --output tsv \
  --only-show-errors)
APP_SERVICE_PLAN_ID=$(az functionapp show \
  --name "$FUNCTION_APP_NAME" \
  --resource-group "$RESOURCE_GROUP_NAME" \
  --query appServicePlanId \
  --output tsv \
  --only-show-errors)

echo -e "\n[$FUNCTION_APP_NAME] function app diagnostic settings:\n"
az monitor diagnostic-settings list \
  --resource "$FUNCTION_APP_ID" \
  --query "[].{Name:name,Logs:join(', ', logs[?enabled].category),Metrics:join(', ', metrics[?enabled].category)}" \
  --output table \
  --only-show-errors

echo -e "\nApp Service plan diagnostic settings:\n"
az monitor diagnostic-settings list \
  --resource "$APP_SERVICE_PLAN_ID" \
  --query "[].{Name:name,Metrics:join(', ', metrics[?enabled].category)}" \
  --output table \
  --only-show-errors

# Check the diagnostic settings of the storage account, of its blob, queue and table services, and of the Log Analytics
# workspace
STORAGE_ACCOUNT_ID=$(az storage account show \
  --name "$STORAGE_ACCOUNT_NAME" \
  --resource-group "$RESOURCE_GROUP_NAME" \
  --query id \
  --output tsv \
  --only-show-errors)
LOG_ANALYTICS_ID=$(az monitor log-analytics workspace show \
  --resource-group "$RESOURCE_GROUP_NAME" \
  --workspace-name "$LOG_ANALYTICS_NAME" \
  --query id \
  --output tsv \
  --only-show-errors)

echo -e "\n[$STORAGE_ACCOUNT_NAME] storage account diagnostic settings:\n"
az monitor diagnostic-settings list \
  --resource "$STORAGE_ACCOUNT_ID" \
  --query "[].{Name:name,Metrics:join(', ', metrics[?enabled].category)}" \
  --output table \
  --only-show-errors

for SERVICE in blob queue table; do
  echo -e "\n[$STORAGE_ACCOUNT_NAME] $SERVICE service diagnostic settings:\n"
  az monitor diagnostic-settings list \
    --resource "$STORAGE_ACCOUNT_ID/${SERVICE}Services/default" \
    --query "[].{Name:name,Logs:join(', ', logs[?enabled].category),Metrics:join(', ', metrics[?enabled].category)}" \
    --output table \
    --only-show-errors
done

echo -e "\n[$LOG_ANALYTICS_NAME] Log Analytics workspace diagnostic settings:\n"
az monitor diagnostic-settings list \
  --resource "$LOG_ANALYTICS_ID" \
  --query "[].{Name:name,Logs:join(', ', logs[?enabled].category),Metrics:join(', ', metrics[?enabled].category)}" \
  --output table \
  --only-show-errors

# List storage containers
az storage container list \
  --account-name $STORAGE_ACCOUNT_NAME \
  --output table \
  --only-show-errors

# List storage queues
az storage queue list \
  --account-name $STORAGE_ACCOUNT_NAME \
  --output table \
  --only-show-errors

# List storage tables
az storage table list \
  --account-name $STORAGE_ACCOUNT_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