Azure Container Apps

Azure Container Apps offers a serverless container approach to deploying microservices without the need to use Kubernetes. In this tutorial we demonstrate the publication of two microservices developed using the Devprime platform.

The implementation of this environment involving two microservices Devprime and Azure Container Apps will additionally utilize the Azure Container Registry (ACR), Azure Log Analytics, Azure Container Apps Environment, Azure CosmosDB, Azure EventHub services.

The image below demonstrates what the final environment will look like after we start all the procedures of creating the environment and publishing the microservices.

Azure Services

Items needed in your environment

  • Install .NET SDK 8 or higher
  • Visual Studio Code
  • An active account on Microsoft Azure
  • An active account on the Devprime platform and Devloper or Enterprise license.
  • Devprime CLI installed and active (dp auth)
  • Azure CLI installed and active (az login)
  • On-premises docker active
  • Microsoft Powershell installed
  • Microsoft Bicep installed ( az bicep install)
    -GIT

In this article we will use two microservices built by Devprime and implemented as presented in the article [Asynchronous Communication microservices]. /../../how-to/stream/rabbitmq/asynchronous-microservices-communication/). You can pre-run the aforementioned example or go straight to the code provided by github.

This project uses powershell and bicep-based scripts to create the Azure Container Apps environment on Azure. You can adapt the scripts as per your need.

Getting Started

a) Run a clone of the project on github
git clone https://github.com/devprime/azure-container-apps-bicep
b) Check the home folder with the items Order and Payment. Each of these folders has a microservice developed with the Devprime platform.
Locally Cloned Paste
c)Go to the ‘order’ folder and add your Devprime license. After running the command it will change
the file ‘order\src\App\appsettings.json’
dp license
d) Go to the ‘payment’ folder and add your Devprime license. After running it will change
the file ‘payment\src\App\appsettings.json’
dp license

Local Database & Stream Credentials
To run the microservice locally by adding the credentials of a mongodb database and a kafka cluster in the order project and in the payment project by editing the file ‘appsettings.json’ as shown in the example below. At the time of deployment we will use the credentials of the Azure environment.

Optionally locate the ‘State’ and ‘Stream’ keys and change the values with mongodb and/or kafka services credentials in the ‘order’ and ‘payment’ folders

code order\src\App\appsettings.json
code payment\src\App\appsettings.json

Running the microservice locally
Go to the order or payment folder and run
.\run.ps1 ou ./run.sh (Linux, macOS)

Exporting Microservices Configurations
Go to the ‘order’ folder and run the Devprime CLI export command to create a deployment file. Repeat the same procedure on the folder and ‘payment’. We’ll copy some parameters.
dp export kubernetes

Now return to the root folder and open the files to observe the parameters that will be sent
during the deployment of Azure Container Apps. View the ‘env:’ key in the files below.
code order\.Devprime\kubernetes\deployment.yml
code payment\.Devprime\kubernetes\deployment.yml

[Environment variables]
When you run the Order and Payment microservices on your Azure Container Apps instance, you need to configure the environment variables. This procedure is very similar to the one used in docker and Kubernetes and you can see a preview in the image below.
Environment variables

Setting the Environment Variables
a) Edit the 1-docker-build-push.ps1, 2-deploy-azure.ps1 and 3-cleanup.ps1 files by setting a new value in the $app variable. Do not use special characters.
b) Edit the deploy\main.bicep file to change the settings for the environment variables.
code deploy\main.bicep
c) Copy the contents of the ‘Devprime_app’ key into the ‘order. Devprime\kubernetes\deployment.yml’ in Order and change in the deploy\main.bicep file to the Order microservice key. Note that in main.bicep we will create two instances of Azure Container Apps and you must repeat the steps in Payment.

// Container Apps: Order
// Container Apps: Payment

In this example, we won’t change any other settings. If you need to set more parameters for your application, repeat the procedure for the other keys.

Running environment creation in Azure Container Apps
We’ll run the scripts so you can follow along step by step. In the end, if all goes well, you will already have the Azure Container Apps url in the logs and query the services in the Azure portal.

a) We will start with the creation of the Azure Resource Group, Azure Container Registry (ACR), docker Build, and Push the microservices images to the private repository in ACR.
.\1-docker-build-push.ps1

b) Now we will use bicep to create Azure Container Apps, Azure Container App Environment, Azure CosmosDB, Log Analytics, Event Hubs.
.\2-deploy-azure.ps1

Accessing microservices in Azure Container Apps
In our example, when creating the services in Container Apps, we are using the option to receive requests (ingress) through a public endpoint.

The urls below are examples of the accesses available. Get yours.

When making a post to the Order API it will process the business rule, persistence in mongodb (Azure CosmosDB) and then emit an event through Kafka (Azure EventHub).

The second microservices will host the event and perform its natural processing cycle according to the implemented business rule.

Deleting the entire created environment
To exclude all services created in Azure, run the script below. Before confirming, make sure about the name of the Resource Group created in this demo
.\3-cleanup.ps1

Suggestion for next steps

  • Automate this process using Azure DevOps, Github…
  • Add a security setting in the API exposure
  • Add an Azure API Management service

To learn more:
Azure Container Apps documentation

How to deploy Azure Container Apps with Bicep

Deploy to Azure Container App from using a CI/CD Azure DevOps

How to Build and Deliver Apps Fast and Scalable with Azure Container Apps

Output connection strings and keys from Azure Bicep

CosmosDB Bicep

Last modified April 16, 2024 (2b35fcc8)