How to boost developer workflow activities with Azure Developer CLI?

This blog post is an overview of the Azure Developer CLI tool and how it helps to boost developer workflow activities. Microsoft has created a lot of great documentation about Azure Developer CLI. This blog post concentrates only main points and especially how to create a Web API project (App Service based + .NET 6) that is compatible with Azure Developer CLI.

What is the Azure Developer CLI?

Microsoft announced the public preview of the Azure Developer CLI in July 2022. Azure Developer CLI (azd) is an open-source tool that accelerates the process of building cloud apps on Azure. The CLI provides best practice, developer-friendly commands that map to key stages in your workflow (ex. build, deployment, monitoring, etc.), whether you’re working in the terminal, your editor, or integrated development environment (IDE), or DevOps.

You can use the azd with extensible azd templates that include everything you need to get an application up and running in Azure. These templates include application code and reusable infrastructure as code assets. Community-developed templates can be found from here (Awesome azd templates) and here (GitHub).

Currently on October 2022 Azure Developer CLI supports the following Azure compute services: Azure App Service, Function, Azure Container AppsAzure Static Web Apps, and the following programming languages: Node.js, Python, .NET.

Sources:

Understand the architecture

The below diagram created by MS describes nicely the architecture of AZD. This blog post follows the main architecture points presented in this diagram.

Let's get started

Prerequisites

Before starting you need to install the following command line tools.

the 1. Install the latest version of Azure CLI

az upgrade

2. Install Azure Developer CLI

powershell -ex AllSigned -c "Invoke-RestMethod 'https://aka.ms/install-azd.ps1' | Invoke-Expression"

3. Install Bicep CLI

az bicep upgrade

Create a project template

These steps create a sample Web Api Visual Studio project which is used to deploy to Azure later.

1. Create a solution

Execute this command in root folder.

dotnet new sln --name "Api.Template"

2. Create a new Dotnet Web API project

Execute this command in root folder.

dotnet new webapi -o src/Api.Template

3. Add Git ignore file

dotnet new gitignore

4. Add project to the solution

dotnet sln add src/Api.Template/Api.Template.csproj

Now you should have this kind of folder structure:

├── Api.Template.sln     [ Solution file ]
├── .gitignore           [ Git ignore file ]
├── src                  [ Source code folder ]
│   ├── Api.Template     [ Folder for Visual Studio WebAPI project ]

Initialize a project

When Visual Studio project is created then project is ready to be initialized with Azure Developer CLI (AZD). Azure Developer CLI creates some configuration files during the initializing.

1. Login to Azure subscription

I recommend login in to Azure subscription before starting the environment initializing because otherwise, you might need to submit subscription details manually. 

az login

2. Initialize a new environment with Azure Developer CLI

Azure Developer CLI uses an environment name to set the AZURE_ENV_NAME environment variable that's used by Azure Developer CLI templates. The environment name is stored in the environment configuration file. After environment initialization configurations can be found in .azure folder. It's also important to understand that the environment name is also used as a prefix in the Azure Resource Group name. You can find more information about azd environments from here.

This command prompts which template to use. Select "Empty template". I didn't find how to set the empty template as a parameter. Execute this command in the root folder where the solution file is located.

azd init --location westeurope --subscription 00000000-0000-0000-0000-000000000000 --environment dev

After initializing you have this kind of folder structure:

├── Api.Template.sln      [ Solution file ]
├── .gitignore            [ Git ignore file ]
├── azure.yaml            [ Describes the app and type of Azure resources]
├── src                   [ Source code folder ]
│   ├── Api.Template      [ Folder for Visual Studio WebAPI project ]
├── .azure                [ Creates and configures Azure resources ]
│   ├── dev               [ Environment folder ]
│   │    ├── .env         [ Environment configuration file]
│   └── config.json       [ AZD Azure configuration file  ]

Create the infrastructure files

Azure infrastructure must be described with Bicep and files must be created to a directory called infra.

1. Create a The folder for infra

The Infra folder should be created in the root folder.

2. Create infrastructure (Bicep) files

I prefer this kind of folder structure for infrastructure files:

├── main.bicep                            [ Main bicep file ]
├── main.parameters.json                  [ Parameter file]
├── assets                                [ Assets folder ]
│   ├── abbreviations.json                [ JSON file which contains Azure resource abbreviations ]
├── modules                               [ Bicep modules folder ]
│   ├── applicationinsights.bicep         [ Application Insights bicep module ]
│   ├── appService.bicep                  [ App Service bicep module ]
│   ├── appServicePlan.bicep              [ App Service Plan bicep module ]
│   ├── logAnalyticsWorkspace.bicep       [ Log Analytics Workspace bicep module ]
│   └── resources.bicep                   [ Resource bicep file which orchestrates resource creation  ]

Note! You can create parameter file also with a Bicep CLI:

az bicep generate-params --file main.bicep

Add these parameter values to the parameter file.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "name": {
        "value": "${AZURE_ENV_NAME}"
        },
        "location": {
        "value": "${AZURE_LOCATION}"
        },
        "principalId": {
        "value": "${AZURE_PRINCIPAL_ID}"
        }
    }
}

Update Azure.yaml file

Azure Developer CLI (AZD) needs to know the details of your application to enable the deployment of infrastructure and applications to Azure. Basically, azure.yaml describes the app and type of Azure resources. The main configuration points are under the service root element.

name: Api.Template
metadata:
  template: webapi-template-azd@0.0.1-beta
services:
  appService:
    project: src/Api.Template
    language: csharp
    host: appservice
    resourceName: app-microservice-westeurope-001

Element

Description

AppService

Name of the service

Project

Path to the source folder

Host

Type of Azure resource used for service implementation

Language

Service implementation language

ResourceName

Name of the Azure resource that implements the service. If ResourceName is not used, then resource tag called "azd-service-name" is used to find a resource where application will be deployed.

More information about updating azure.yaml can be found here. Schema of azure.yaml is available here.

Provision & Deploy

Azure Developer CLI provides different commands whether if you just want to deploy only applications or infrastructure or both.

Deploy infrastructure and application to Azure

azd up

Deploy only applications, to Azure

azd deploy

Deploy only infrastructure to Azure

azd provision

Lastly, if you want to remove created resource from Azure you can use the following command:

azd down

Templating

Azure Developer CLI enables to use of project templates published to GitHub. You can use community-driven templates or use own template.

Initialize the the project using custom template

Template is available from GitHub.

azd init --template kalleantero/Api.Template

Deploy infrastructure and application to Azure

azd up

Super easy, just two commands and the working environment is up and running in Azure.

Summary

Azure Developer CLI widely supports different software development workflow activities like build, deployment and also monitoring & setting up CI/CD which wasn't handled in this blog post. The command interface of Azure Developer CLI is very intuitive, and commands wrap different Azure and Bicep CLI commands together behind the scenes. Of course, you can do almost the same things just using different Azure CLI and Bicep CLI commands separately but using Azure Developer CLI makes things more straightforward. 

I see that this tool is very useful especially if you need to quickly set up a new developer environment and maybe test something. Using your own templates is also a very interesting option. Azure Developer CLI is currently still in preview and there are some limitations, but I believe that interesting features are coming in the future to this tool.

The source code of this sample project is available on GitHub.

Comments