|
| 1 | +# Bicep Deployment |
| 2 | + |
| 3 | +This directory contains a Bicep template and a deployment script for provisioning Azure services in LocalStack for Azure. For further details about the sample application, refer to the [Azure Service Bus with Spring Boot](../../README.md). |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +Before deploying this solution, ensure you have the following tools installed: |
| 8 | + |
| 9 | +- [LocalStack for Azure](https://azure.localstack.cloud/): Local Azure cloud emulator for development and testing |
| 10 | +- [Visual Studio Code](https://code.visualstudio.com/): Code editor installed on one of the [supported platforms](https://code.visualstudio.com/docs/supporting/requirements#_platforms) |
| 11 | +- [Bicep extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-bicep): VS Code extension for Bicep language support and IntelliSense |
| 12 | +- [Docker](https://docs.docker.com/get-docker/): Container runtime required for LocalStack |
| 13 | +- [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli): Azure command-line interface |
| 14 | +- [Azlocal CLI](https://azure.localstack.cloud/user-guides/sdks/az/): LocalStack Azure CLI wrapper |
| 15 | +- [Java 21+](https://learn.microsoft.com/en-us/java/openjdk/download): Java runtime for compiling and running the sample application |
| 16 | +- [Maven 3.8+](https://maven.apache.org/download.cgi): Build tool for managing Java project dependencies and compilation |
| 17 | +- [jq](https://jqlang.org/): JSON processor for scripting and parsing command outputs |
| 18 | + |
| 19 | +### Installing azlocal CLI |
| 20 | + |
| 21 | +The [deploy.sh](deploy.sh) Bash script uses the `azlocal` CLI instead of the standard Azure CLI to work with LocalStack. Install it using: |
| 22 | + |
| 23 | +```bash |
| 24 | +pip install azlocal |
| 25 | +``` |
| 26 | + |
| 27 | +For more information, see [Get started with the az tool on LocalStack](https://azure.localstack.cloud/user-guides/sdks/az/). |
| 28 | + |
| 29 | +## Architecture Overview |
| 30 | + |
| 31 | +The [deploy.sh](deploy.sh) script creates the [Azure Resource Group](https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/manage-resource-groups-cli), while the Bicep template creates the following Azure resources: |
| 32 | + |
| 33 | +1. [Azure Resource Group](https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/manage-resource-groups-cli): A logical container scoping all resources in this sample. |
| 34 | +2. [Azure Service Bus Namespace](https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview): The messaging namespace that hosts the queue used by the application. |
| 35 | +3. [Azure Service Bus Queue](https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-queues-topics-subscriptions#queues): The `myqueue` queue used to send and receive messages. |
| 36 | + |
| 37 | +The Spring Boot sample application connects to the Service Bus namespace, sends a test message to the sample queue, receives it back, and exits. For more information on the sample application, see [Azure Service Bus with Spring Boot](../../README.md). |
| 38 | + |
| 39 | +## Configuration |
| 40 | + |
| 41 | +Before deploying the `main.bicep` template, update the [main.bicepparam](main.bicepparam) file with your specific values: |
| 42 | + |
| 43 | +```bicep |
| 44 | +using 'main.bicep' |
| 45 | +
|
| 46 | +param queueName = 'myqueue' |
| 47 | +param zoneRedundant = false |
| 48 | +param tags = { |
| 49 | + environment: 'test' |
| 50 | + iac: 'bicep' |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +## Provisioning Scripts |
| 55 | + |
| 56 | +You can use the [deploy.sh](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: |
| 57 | + |
| 58 | +- Detects environment (LocalStack vs Azure Cloud) and uses the appropriate CLI. |
| 59 | +- Creates the resource group if it doesn't exist. |
| 60 | +- Optionally validates the Bicep template. |
| 61 | +- Optionally runs a what-if deployment for preview. |
| 62 | +- Deploys the `main.bicep` template with parameters from [main.bicepparam](main.bicepparam). |
| 63 | +- Extracts the Service Bus namespace name from deployment outputs. |
| 64 | +- Retrieves the namespace connection string and exports it as `AZURE_SERVICEBUS_CONNECTION_STRING`. |
| 65 | +- Compiles the Spring Boot project and runs the app on the host machine. |
| 66 | + |
| 67 | +## Deployment |
| 68 | + |
| 69 | +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](https://docs.localstack.cloud/getting-started/auth-token/) 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](https://hub.docker.com/r/localstack/localstack-azure-alpha). To pull the Azure Docker image, execute the following command: |
| 70 | + |
| 71 | +```bash |
| 72 | +docker pull localstack/localstack-azure-alpha |
| 73 | +``` |
| 74 | + |
| 75 | +Start the LocalStack Azure emulator using the localstack CLI, execute the following command: |
| 76 | + |
| 77 | +```bash |
| 78 | +export LOCALSTACK_AUTH_TOKEN=<your_auth_token> |
| 79 | +IMAGE_NAME=localstack/localstack-azure-alpha localstack start |
| 80 | +``` |
| 81 | + |
| 82 | +Navigate to the `bicep` folder: |
| 83 | + |
| 84 | +```bash |
| 85 | +cd samples/servicebus/java/bicep |
| 86 | +``` |
| 87 | + |
| 88 | +Make the script executable: |
| 89 | + |
| 90 | +```bash |
| 91 | +chmod +x deploy.sh |
| 92 | +``` |
| 93 | + |
| 94 | +Run the deployment script: |
| 95 | + |
| 96 | +```bash |
| 97 | +./deploy.sh |
| 98 | +``` |
| 99 | + |
| 100 | +## Validation |
| 101 | + |
| 102 | +Once the deployment completes, run the [validate.sh](../scripts/validate.sh) script to confirm that all resources were provisioned and configured as expected: |
| 103 | + |
| 104 | +```bash |
| 105 | +#!/bin/bash |
| 106 | + |
| 107 | +# Variables |
| 108 | +PREFIX='local' |
| 109 | +SUFFIX='test' |
| 110 | +RESOURCE_GROUP_NAME="${PREFIX}-rg" |
| 111 | +SERVICEBUS_NAMESPACE_NAME="${PREFIX}-sb-ns-${SUFFIX}" |
| 112 | +SERVICEBUS_QUEUE_NAME="myqueue" |
| 113 | +ENVIRONMENT=$(az account show --query environmentName --output tsv) |
| 114 | + |
| 115 | +# Choose the appropriate CLI based on the environment |
| 116 | +if [[ $ENVIRONMENT == "LocalStack" ]]; then |
| 117 | + echo "Using azlocal for LocalStack emulator environment." |
| 118 | + AZ="azlocal" |
| 119 | +else |
| 120 | + echo "Using standard az for AzureCloud environment." |
| 121 | + AZ="az" |
| 122 | +fi |
| 123 | + |
| 124 | +# Check resource group |
| 125 | +echo -e "[$RESOURCE_GROUP_NAME] resource group:\n" |
| 126 | +$AZ group show \ |
| 127 | + --name "$RESOURCE_GROUP_NAME" \ |
| 128 | + --output table \ |
| 129 | + --only-show-errors |
| 130 | + |
| 131 | +# Check Service Bus namespace |
| 132 | +echo -e "\n[$SERVICEBUS_NAMESPACE_NAME] Service Bus namespace:\n" |
| 133 | +$AZ servicebus namespace show \ |
| 134 | + --resource-group "$RESOURCE_GROUP_NAME" \ |
| 135 | + --name "$SERVICEBUS_NAMESPACE_NAME" \ |
| 136 | + --query "{name:name, location:location, serviceBusEndpoint:serviceBusEndpoint, status:provisioningState}" \ |
| 137 | + --output table \ |
| 138 | + --only-show-errors |
| 139 | + |
| 140 | +# Check Service Bus queue |
| 141 | +echo -e "\n[$SERVICEBUS_QUEUE_NAME] Service Bus queue:\n" |
| 142 | +$AZ servicebus queue show \ |
| 143 | + --resource-group "$RESOURCE_GROUP_NAME" \ |
| 144 | + --namespace-name "$SERVICEBUS_NAMESPACE_NAME" \ |
| 145 | + --name "$SERVICEBUS_QUEUE_NAME" \ |
| 146 | + --query "{name:name, messageCount:messageCount, sizeInBytes:sizeInBytes}" \ |
| 147 | + --output table \ |
| 148 | + --only-show-errors |
| 149 | +``` |
| 150 | + |
| 151 | +## Cleanup |
| 152 | + |
| 153 | +To destroy all created resources: |
| 154 | + |
| 155 | +```bash |
| 156 | +#!/bin/bash |
| 157 | + |
| 158 | +# Variables |
| 159 | +PREFIX='local' |
| 160 | +SUFFIX='test' |
| 161 | +RESOURCE_GROUP_NAME="${PREFIX}-rg" |
| 162 | + |
| 163 | +# Delete resource group and all contained resources |
| 164 | +az group delete --name $RESOURCE_GROUP_NAME --yes --no-wait |
| 165 | + |
| 166 | +# Verify deletion |
| 167 | +az group list --output table |
| 168 | +``` |
| 169 | + |
| 170 | +This will remove all Azure resources created by the Bicep deployment script. |
| 171 | + |
| 172 | +## Related Documentation |
| 173 | + |
| 174 | +- [Azure Bicep Documentation](https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/) |
| 175 | +- [Bicep Language Reference](https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions) |
| 176 | +- [LocalStack for Azure Documentation](https://azure.localstack.cloud/) |
0 commit comments