-
Notifications
You must be signed in to change notification settings - Fork 5
Instructions 04 AzureServices
Create Azure services and host the games application:
- A resource group
- Azure Cosmos DB with a database and a container
- Azure Container Registry
- Log Analytics Workspace
- Azure Container Apps
Create a new resource group named codebreakerlight to contain all the Codebreaker resources.
Create a new Azure Cosmos DB resource.
- Select the API Azure Cosmos DB for NoSQL.
- Select a region in your area.
- Select provisioned throughput.
- Apply for the free tier if it's available in your subscription.
- Limit the total amount of throughput.
All other settings can stay with the default values.
Create the resource. This takes about 2 minutes, but you can continue with the next steps before continuing here.
Open the Data Explorer with Azure Cosmos DB.
- Create a new database named codebreaker.
- Select provisioned throughtput with a value of 400 (this is the minimum that can be used).
- Create a new container named GamesV3 with the partition key /PartitionKey.
Create a new Azure Container Registry (ACR) resource.
- Select your test subscription.
- Select the created resource group.
- Define a unique registry name (e.g. Codebreakerlight-{anumber})
- Select a region in your area.
- Select Basic as SKU.
Create the resource.
With the registry, open the settings "Access Keys". Enable the Admin user, and copy the username and password.
Build the Docker images for the games and bot services (see Part 2).
- `docker build . -t codebreakerlight/gamesapi:3.6.1 codebreakerlight/gamesapi:basta
- docker login
Use the copied username and password to login to the ACR.
Tag the Docker image with the ACR name and version.
- docker tag codebreakerlight/gamesapi:3.6.1 .azurecr.io/codebreakerlight/gamesapi:3.6.1
- docker tag codebreakerlight/gamesapi:3.6.1 .azrecr.io/codebreakerlight/gamesapi:basta
- docker tag codebreakerlight/gamesapi:3.6.1 .azrecr.io/codebreakerlight/gamesapi:latest
Push the Docker image to the ACR.
- docker push .azurecr.io/codebreakerlight/gamesapi:3.6.1
- docker push .azrecr.io/codebreakerlight/gamesapi:latest
Build, tag, and publish the Docker image to the ACR.
With the Bot service project file, add the Docker configuration:
<PropertyGroup>
<ContainerRegistry>{your ACR}.azurecr.io</ContainerRegistry>
<ContainerRepository>codebreaker/bot</ContainerRepository>
<ContainerImageTags>3.6.1;basta;latest</ContainerImageTags>
</PropertyGroup>Login to Azure
- az login
Login to the ACR
- az acr login -n
dotnet publish Codebreaker.Bot.csproj --os linux --arch x64 /t:PublishContainer -c Release
Verify the ACR registry. The images are now available in the ACR.
Container Apps previously needed a Log Analytics Workspace. This is no longer required. Instead, you can use Azure Monitor, or no logging. Still it's best to create a Log Analytics Workspace.
Create a new Log Analytics Workspace resource.
- Select the subscription
- Select the resource group
- Specify the name codebreakerlight-logs
- Select the region
A log analytics workspace can also be created automatically when creating the Azure Container Apps environment.
Create a new Azure Container Apps resource.
- Select the subscription.
- Select the existing resource group.
- Specify a container app name for the games, e.g. codebreakergames.
- Select the region for the environment.
Select to create a new Azure Container App environment. We'll change these settings in the next steps:
- Basic
- Monitoring
- Networking
Select Consumption only, and keep the zones disabled.

Keep Azure Log Analytics selected. Select the log-analytics workspace you created earlier.
Keep your own virtual network turned off.
Click the Create button to complete the configuration of the environment.
Next, configure the container app.
Configure the container
- Select the ACR
- Select the registry
- Select the image
- Select the image tag latest or basta
- Select 0.25 CPU and 0.5 Gi memory
- Set the environment variable DataStorage to the value Cosmos.

The DataStorage configuration defines which storage provider should be used during runtime.
Because the connection to Azure Cosmos contains a secret, we cannot configure this at this point - but add the secret later on.
Enable the Ingress controller to accept traffic from everywhere, and to route to the port 8080.
Click on Review + Create. When this passes, verify the settings, and click Create to create the container app and the environment.
Azure Container Apps support storing secrets.
Get the connection string from the Azure Cosmos database, select Keys and copy the Primary Connection String.
After the Container App is created, open Secrets within Settings.
- Open Secrets
- Create a new secret
- Select Container Apps Secret
- Specify the key named cosmos-connection
- Paste the connection string as value
Create an environment variable to reference the secret.
Use the Azure CLI to update the container app by adding an environment variable referencing the secret. Pay attention to use your resource group and container app name. The configuration with the API uses ConnectionStrings:GamesCosmosConnection.
az containerapp update -n codebreakergames -g codebreakerlight --set-env-vars ConnectionStrings__GamesCosmosConnection=secretref:cosmos-connectionWhen the service scales down to 0, the first user accessing the service will experience a delay. To avoid this, configure the service to always have at least one instance running. If it's not used, the pricing is the same as 1 instance for the memory, but about 1/10 for the CPU.
Open the Container App
- Open Scales and Replicas with Settings.
- Click Edit and deploy.
- Open Scale.
- Change scaling from 1 to 3.
- Click the Create button.
Change the scaling from a minimum of 1 to a maximum of 3 (if this is hard using the mouse, use the cursor keys of the keyboard).
Create the container app for the bot similar. First, copy the link to the games container app to set the link with the bot with the environment variable APIBase.
- Open your existing Container App Environment.
- With the Apps category, create a new Azure Container App.
- Set the name to codebreakerbot
- With the container configuration,
- Select your Azure Container Registry
- Select the codebreaker/bot image
- Set APIBase environment variable to the link of your games container app, e.g. http://codebreakergames.azurecontainer.io:8080
- Configure Ingress to allow traffic from everywhere, and to route to target port 8080.

Configure the scaling for the bot service in a range of 0 to 2.
You have different ways to run the service:
- Use the Swagger UI of the bot app, and start games.
- Use the Swagger UI of the games app, and play games.
- Use the HTTP file from Visual Studio to directly access the API and play games.
- Use the .NET MAUI app you develop next to play games.
Check the database to see the games played.