Skip to content

Latest commit

 

History

History
264 lines (211 loc) · 12.1 KB

File metadata and controls

264 lines (211 loc) · 12.1 KB

Azure CLI Deployment

This directory includes Bash scripts designed for deploying and testing the sample Web App utilizing the lstk CLI. For further details about the sample application, refer to the Azure Web App with Azure SQL Database and Azure Key Vault.

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 Bash script creates the following Azure resources using Azure CLI commands:

  1. Azure Resource Group: Logical container for all resources
  2. Azure SQL Server: Logical server hosting one or more Azure SQL Databases.
  3. Azure SQL Database: The PlannerDB database storing relational vacation activity data. Its auditing sends the records of the statements run on it and of the sign-ins to the workspace: az sql db audit-policy update --log-analytics-target-state Enabled turns it on and gives the audit category (SQLSecurityAuditEvents) a diagnostic setting of its own.
  4. Azure App Service Plan: The compute resource that hosts the web application.
  5. Azure Web App: Hosts the ASP.NET Core Razor Pages single-page application (Vacation Planner), connected to Azure SQL Database. The app setting APPLICATIONINSIGHTS_CONNECTION_STRING holds the connection string of the Application Insights resource.
  6. App Service Source Control: (Optional) Configures automatic deployment from a public GitHub repository.
  7. Azure Key Vault: Stores the SQL connection string in a secret, a self-signed certificate for HTTPS, and the RSA key that serves as the TDE protector of the SQL server.
  8. User-Assigned Managed Identity: The identity the SQL server uses to reach its TDE protector key in Key Vault.
  9. Azure Log Analytics Workspace: Stores the telemetry of Application Insights, and the resource logs and platform metrics that the diagnostic settings of the web app, the App Service plan, the database, the key vault and the workspace itself send to it.
  10. Azure Application Insights: workspace-based, it stores the telemetry of the web app in the Log Analytics workspace.

The system implements a Vacation Planner web application that stores and retrieves activity data from Azure SQL Database. For more information, see Azure Web App with Azure SQL Database and Azure Key Vault.

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

Important

Each logical server you create runs a Microsoft SQL Server container, so you must accept the SQL Server End-User Licensing Agreement before the emulator provisions one. Start the emulator with MSSQL_ACCEPT_EULA set to Y:

LOCALSTACK_MSSQL_ACCEPT_EULA=Y lstk start --type azure

Host variables prefixed with LOCALSTACK_ are forwarded to the emulator, so the container receives MSSQL_ACCEPT_EULA=Y.

To accept it once rather than on every start, add it to an environment profile in your lstk config:

[[containers]]
type = "azure"
tag  = "latest"
port = "4566"
env  = ["mssql"]

[env.mssql]
MSSQL_ACCEPT_EULA = "Y"

A profile's values are injected into the container directly, so the LOCALSTACK_ prefix is not used here. lstk reads the first config.toml it finds, starting with ./.lstk/config.toml in the current directory and then $HOME/.config/lstk/config.toml. See Config file search order for the full list and Passing environment variables to the container for how profiles work.

If you start the emulator with the docker CLI or Docker Compose, set MSSQL_ACCEPT_EULA=Y directly in the container environment instead. Without it, az sql server create fails with (EulaNotAccepted).

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

cd samples/web-app-sql-database/dotnet/scripts

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}"
RESOURCE_GROUP_NAME="${PREFIX}-rg"
SQL_SERVER_NAME="${PREFIX}-sqlserver-${SUFFIX}"
SQL_DATABASE_NAME='PlannerDB'
WEB_APP_NAME="${PREFIX}-webapp-${SUFFIX}"
KEY_VAULT_NAME="${PREFIX}-kv-${SUFFIX}"
SECRET_NAME="${PREFIX}-secret-${SUFFIX}"
TDE_KEY_NAME="${PREFIX}-tde-key-${SUFFIX}"
APPLICATION_INSIGHTS_NAME="${PREFIX}-insights-${SUFFIX}"

# Check resource group
echo -e "[$RESOURCE_GROUP_NAME] resource group:\n"
az group show \
--name "$RESOURCE_GROUP_NAME" \
--output table

# 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, defaultHostName:defaultHostName}" \
--output table

# Check Azure SQL Server
echo -e "\n[$SQL_SERVER_NAME] SQL server:\n"
az sql server show \
--name "$SQL_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--output table

# Check Azure SQL Database
echo -e "\n[$SQL_DATABASE_NAME] SQL database:\n"
az sql db show \
--name "$SQL_DATABASE_NAME" \
--server "$SQL_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--output table

# Check that the Key Vault key is the TDE protector of the Azure SQL Server
echo -e "\n[$SQL_SERVER_NAME] SQL server TDE protector:\n"
# Read the key through Azure Resource Manager: the Bicep variant grants the caller no Key Vault data-plane access.
KEY_VAULT_ID=$(az keyvault show \
--name "$KEY_VAULT_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query "id" \
--output tsv)
TDE_KEY_ID=$(az resource show \
--ids "$KEY_VAULT_ID/keys/$TDE_KEY_NAME" \
--api-version 2024-11-01 \
--query "properties.keyUriWithVersion" \
--output tsv)
TDE_PROTECTOR=$(az sql server tde-key show \
--server "$SQL_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--output json)
echo "$TDE_PROTECTOR" | jq '{serverKeyType, uri, autoRotationEnabled}'
if [[ "$(jq -r .serverKeyType <<< "$TDE_PROTECTOR")" != "AzureKeyVault" ||
	"$(jq -r .uri <<< "$TDE_PROTECTOR")" != "$TDE_KEY_ID" ||
	"$(jq -r .autoRotationEnabled <<< "$TDE_PROTECTOR")" != "true" ]]; then
	echo "The TDE protector of [$SQL_SERVER_NAME] is not the auto-rotated Key Vault key [$TDE_KEY_ID]"
	exit 1
fi

# Check that TDE is enabled on the Azure SQL Database
TDE_STATE=$(az sql db tde show \
--database "$SQL_DATABASE_NAME" \
--server "$SQL_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query "state" \
--output tsv)
echo -e "\n[$SQL_DATABASE_NAME] SQL database TDE state: [$TDE_STATE]"
if [[ "$TDE_STATE" != "Enabled" ]]; then
	echo "TDE is not enabled on [$SQL_DATABASE_NAME]"
	exit 1
fi

# Check Azure Key Vault
echo -e "\n[$KEY_VAULT_NAME] Key Vault:\n"
az keyvault show \
--name "$KEY_VAULT_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--output table

# Check Key Vault secret
echo -e "\n[$SECRET_NAME] Key Vault secret:\n"
az keyvault secret show \
--vault-name "$KEY_VAULT_NAME" \
--name "$SECRET_NAME" \
--query "{name:name, enabled:attributes.enabled, created:attributes.created}" \
--output table

# Check Application Insights component
echo -e "\n[$APPLICATION_INSIGHTS_NAME] Application Insights component:\n"
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

# Print the list of resources in the resource group
echo -e "\nListing resources in resource group [$RESOURCE_GROUP_NAME]...\n"
az resource list --resource-group "$RESOURCE_GROUP_NAME" --output table 

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.

Telemetry, logs and metrics

Once the web app is deployed, read the telemetry it sent to Application Insights and the logs and metrics the diagnostic settings sent to the workspace. The scripts take the same PREFIX and SUFFIX as the deployment:

bash query-telemetry.sh
bash query-logs-and-metrics.sh

See Application Insights and Logs and metrics for what they read.

Related Documentation