Showing posts with label web api. Show all posts
Showing posts with label web api. Show all posts

Wednesday, November 1, 2023

Deploy backend API Microservice to Azure Container Apps

 Ref: https://bitoftech.net/2022/08/25/deploy-microservice-application-azure-container-apps/

This articles gives co plete steps to deploy basic web api to Azure using Azure container apps and settting access to api either internal or external in docker config. 

Other taks like creating UI as micro site and communication between components has been eplained very good and links given to github and other components .  Articles gives complete picture of tasks to develop an basic microservice using azure container and dockers.

In this post, we will start by creating the solution skeleton by adding our first microservice named “Web API – Backend” as illustrated in the architecture diagram, then start provisioning Azure resources needed to deploy the service to Azure Container Apps using Azure CLI and Azure Portal.

As a prerequisite you need to have Visual Studio 2022 version 17.2 or higher, or VS Code to develop the solution, for my case I’ve started the Project using Visual Studio 2022 and then switched to VS code as debugging Dapr enabled applications and the extensions available in VS code makes developing the solution in VS code much easier. A recommendation is to use VS Code to build the solution, yet you can use Visual Studio 2022 and VS Code for local debugging.

The source code for this tutorial is available on GitHub. You can check the demo application too.

Setting up the Web API Backend Project

Step 1: Create the Web API Project

Create an empty solution and name it “TasksTracker.ContainerApps” then add a new ASP.NET Core Web API project named “TasksTracker.TasksManager.Backend.Api”, Configuration will be as the image below, check “Enable Docker” as we are going to containerize the application and deploy it to ACR, make sure “Linux” is selected for the Docker OS setting.
NetCore6WebApi

Step 2: Add Models (DTO)

Add a new folder named “Models” and create a new file named “TaskModel.cs” and paste the code below, those are the DTOs that will be used across the projects.

Step 3: Add In-memory Tasks Manager Service

We will work initially with data in memory to keep things simple with very limited dependency on any other components or data store and focus on the deployment of the backend API to Azure Container Apps, then we will switch this implementation with a concrete one where we are going to store data in Redis and Azure Cosmos DB using Dapr State Store building block, so to add the Fake Tasks Manager service (In-memory), create new folder named “Services” and add a new file named “ITasksManager.cs”, this will be the interface of Tasks Manager service. Paste the code below:

Now add a new file named “FakeTasksManager.cs” and paste the code below:

The code above is self-explanatory, it generates 10 tasks and stores them in a list in memory, as well it has some operations to add/remove/update those tasks.

Step 4: Register FakeTasksManager on project startup

Open file “Program.cs” and register the newly created service by adding the line highlighted below

Step 5: Create API endpoints to manage tasks

Now we will add a new controller named “TasksController.cs” under the folder “Controllers” which will contain simple action methods to enable basic CRUD operations on tasks.

Step 6: Add Dockerfile to the Web API project

In case you didn’t check the “Enable Docker” checkbox when creating the project we need to add a Dockerfile to the Web API project, so ensure you are on the root of the project and add file named “Docekrfile” and paste the code below:

Notes:

  • If you are using Visual Studio 2022, you can right-click on the Web API project and then select “Add->Docker Support”, it will generate the same file for you.
  • If you want to run the project locally on Docker, you need to install Docker Desktop, it is straight forward installation and can be installed from here. My recommendation is to have Docker Desktop installed as we are going to need it when introducing Dapr state Management API.

Now you can run your application locally to test, try to issue GET request to the endpoint https://localhost:7088/api/tasks?createdby=tjoudeh@bitoftech.net  and you should receive an array of tasks similar to the below:

Deploy the Web API Backend Project to Azure Container Apps

There are several ways to deploy the Web API to Azure Container Apps, this is a good link to view different ways, for this tutorial we will use Azure CLI/PowerShell.

Step 1: Install/Upgrade Azure Container Apps Extension

Open the PowerShell console on your machine, and sign in to Azure from the CLI. Run the below command, and follow the prompts to complete the authentication process.

If you have multiple Azure subscriptions under the account you authenticated with you need to select the subscription you want to provision resources under

Ensure you’re running the latest version of the CLI via the upgrade command.

Now install or update the Azure Container Apps extension for the CLI.

Register the “Microsoft.App” and “Microsoft.OperationalInsights” namespaces if you haven’t already registered them in your Azure subscription.

Step 2: Provision Azure Resources

Now we will be defining some variables in the PowerShell console to use them across the series of posts, you should change the values of those variables to be able to create the resources successfully, some of those variables should be unique across all Azure subscriptions such as Azure Container Registry name.

Now we will create a resource group to organize the services related to our application

Next, we will create an Azure Container Registry (ACR) instance in the resource group to store images of all Microservices we are going to build during this tutorial, make sure that you set the admin-enabled flag to true in order to seamlessly authenticate Azure container app when trying to create the container app using the image stored in ACR

Now we will build the Web API project on ACR and push the docker image to ACR. Use the below command to initiate the image build and push process using ACR. The “.” at the end of the command represents the docker build context, in our case, we need to be on the parent directory which hosts the .csproject.

Once this step is completed you can verify the results by going to the Azure portal and check that a new repository named “tasksmanager/tasksmanager-backend-api” has been created and there is a new docker image with a “latest” tag is created, it should look like the below

Azure Container Registry

Step 3: Provision Azure Container Env and Container App

Next is to create Azure Container Apps Environment, as shared previously, it acts as a secure boundary around a group of all container apps we are going to provision during this tutorial, to create it, run the below command:

Note: If you are using the CLI command to create Container Apps Environment, it is better to set the property “–dapr-instrumentation-key” when creating the Environment, so you need to provision Application Insights resource to get its instrumentation key, more details on this post.

The last step here is to create and deploy the Web API to Azure Container App following the below command:

Some notes on the above command:

  • Ingress param is set to “external” which means that this container app (Web API) project will be accessible from the public internet. When Ingress is set to “Internal” or “External” it will be assigned a fully qualified domain name (FQDN). Important notes about IP addresses and domain names can be found here.
  • The target port param is set to 80, this is the port our Web API container listens to for incoming requests.
  • We didn’t specify the ACR registry username and password, “az containerapp create” command was able to look up ACR username and password and add them as a secret under the created Azure container app for future container updates.
  • The minimum and the maximum number of replicas are set, more about this when we talk about Autoscaling. In the meantime, only 1 single instance of this container app will be provisioned as Auto scale is not configured.
  • We are setting the size of the Container App, the total amount of CPUs and memory requested for the container app must add up to certain combinations, for full details check the link here
  • The “query” property will filter the response coming from the command and just return the FQDN.

For full details on all available parameters for this command, please visit this page.

Step 4: Verify the deployment of the Azure Container App.

Now we can navigate to the Azure Portal and select the resource group named “tasks-tracker-rg” created, you should see the 4 recourses created below. By default when you create an Azure Container Environment, a Log Analytics workspace will be created, we will cover this in the Monitoring and observability post.

Now copy the FQDN (Application URL) of the Azure container app named “tasksmanager-backend-api” and issue GET request similar to this one: https://tasksmanager-backend-api.agreeablestone-8c14c04c.eastus.azurecontainerapps.io/api/tasks/?createdby=tjoudeh@bitoftech.net
And you should receive an array of the 10 tasks similar to the below image


In the next post, we will see how we will add a new Frontend Web App as a microservice and how it will communicate with the backend API.

Performance optimization techniques in ReactJS

Summary: Helps to learn how to measure performance improvements. As the majority of modern web applications rely on what React.js brings to ...