The Azure Developer’s Superpower: azd CLI
After a somewhat lengthy pause, it was time to update my blog.
Following is the content of my session at the Azure Meetup Casteddu, held a few days ago at Sa Manifattura, Cagliari.
To stay updated about Azure Meetup Casteddu events, I invite you to join the community:
Join Azure Meetup Casteddu on Whatsapp
Join Azure Meetup Casteddu - Meetup page
Follow Azure Meetup Casteddu on LinkedIn
Azure Developer CLI (azd)
The common Azure Development Challenges
Have you ever spent more time configuring Azure deployments than actually writing code?
Well, it happened to me, specially when I was learning Azure.
Before diving into this article, try to answer the following questions:
- How long does it take you to build a proof of concept?
- How long does it take you to deploy it on Azure?
- Can you do it yourself or do you ask your DevOps mates for help?
- Are you able to implement a robust repeatable deployment process that allows you to share your work with your colleagues so they can work on it and redeploy it in a short time? How long does it take you?
The Problems We All Face
- Infrastructure Setup Complexity: Deep knowledge of Azure services is required;
- Time spent on DevOps/Platform engineering instead of development;
- Deployment pipeline + IAC headaches;
- Environment consistency issues.
All of these are factors that can slow down development and increase the risk of errors.
The Solution: Azure Developer CLI
An open-source tool that accelerates provisioning and deploying app resources on Azure. azd CLI provides best practice, developer-friendly commands that map to key stages in development workflows.
Note: Azure Developer CLI ≠ Azure CLI
Tool | Sample Command | Outcome |
---|---|---|
Azure Developer CLI | azd provision |
Provisions multiple Azure resources required for an app based on project resources and configurations, such as an Azure resource group, an Azure App Service web app and app service plan, an Azure Storage account, and an Azure Key Vault. |
Azure CLI | az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name myWebApp |
Provisions a new web app in the specified resource group and app service plan. |
Azure PowerShell | New-AzWebApp -ResourceGroupName "myResourceGroup" -Name "myWebApp" -AppServicePlan "myAppServicePlan" |
Provisions a new web app in the specified resource group and app service plan. |
source - aka.ms/learn - Compare Azure Developer CLI commands
Features
Supported Azure compute services (host)
Azure compute service | Feature Stage |
---|---|
Azure App Service | Stable |
Azure Static Web Apps | Stable |
Azure Container Apps | Beta |
Azure Functions | Stable |
Azure Kubernetes Service | Beta (only for projects deployable via kubectl apply -f ) |
Azure Spring Apps | Beta |
source - aka.ms/learn - azd CLI Supported Azure Compute Services
Supported languages and frameworks
Language | Feature Stage |
---|---|
Node.js | Stable |
Python | Stable |
.NET | Stable |
Java | Stable |
source - aka.ms/learn - azd CLI Supported Languages and Framework
Template library - AWESOME-AZD
Develop your own templates
source - aka.ms/learn - Make azd Compatible
Template Structure
.azure
folder - Contains essential Azure configurations and environment variables, such as the location to deploy resources or other subscription informationinfra
folder - Contains all of the Bicep or Terraform infrastructure-as-code files for the azd templatesrc
folder - Contains all of the deployable app source codeazure.yaml
file - A configuration file that defines services and maps them to Azure resources
Azure.yaml Overview
The azure.yaml file describes the application and the Azure resources included in the azd template.
# yaml-language-server: $schema=https://raw.githubusercontent.com/Azure/azure-dev/main/schemas/v1.0/azure.yaml.json
name: todo-csharp-sql
metadata:
template: todo-csharp-sql@0.0.1-beta
workflows:
up:
steps:
- azd: provision
- azd: deploy --all
services:
web:
project: ./src/web
dist: dist
language: js
host: appservice
hooks:
# Creates a temporary `.env.local` file for the build command. Vite will automatically use it during build.
# The expected/required values are mapped to the infrastructure outputs.
# .env.local is ignored by git, so it will not be committed if, for any reason, if deployment fails.
# see: https://vitejs.dev/guide/env-and-mode
# Note: Notice that dotenv must be a project dependency for this to work. See package.json.
prepackage:
windows:
shell: pwsh
run: 'echo "VITE_API_BASE_URL=""$env:API_BASE_URL""" > .env.local ; echo "VITE_APPLICATIONINSIGHTS_CONNECTION_STRING=""$env:APPLICATIONINSIGHTS_CONNECTION_STRING""" >> .env.local'
posix:
shell: sh
run: 'echo VITE_API_BASE_URL=\"$API_BASE_URL\" > .env.local && echo VITE_APPLICATIONINSIGHTS_CONNECTION_STRING=\"$APPLICATIONINSIGHTS_CONNECTION_STRING\" >> .env.local'
postdeploy:
windows:
shell: pwsh
run: 'rm .env.local'
posix:
shell: sh
run: 'rm .env.local'
api:
project: ./src/api
language: csharp
host: appservice
As you can see from the todo-csharp-sql template’s azure.yaml, the template includes two services:
App | Azure resource | Service implementation language |
---|---|---|
web | App Service | JS |
api | App Service | C# |
How azd Maps IaC templates and application source code to service in azure.yaml
For what concerns IaC templates, the CLI assumes the IaC module name is the same as the service name, otherwise the module path (relative to the root infra folder) can be specified using the module property at the single service level.
The path to the service source code is specified through the service project property.
For further information, here is the link to azure.yaml schema.
Workflows
azd init workflows
- Scan current directory: Analyzes existing app codebase to generate appropriate configuration
- Select a template: Clones and initializes a template from gallery
- Create a minimal project: Initializes basic azure.yaml file
azd up workflow
- Packaging: prepares the application code and dependencies
- Provisioning: creates and configures Azure resources
- Deployment: deploys the packaged application
azd CLI Zero to Hero in 8 Commands
# 1. Login to Azure
azd auth login
# 2. Initialize from template
azd init --template todo-csharp-sql
# 3. Provision and deploy
azd up
# 4. View your live app
azd show
# 5. Provision infrastructure (Bicep/Terraform)
azd provision
# 6. Make changes to app code and redeploy
azd deploy
# 7. Monitor and troubleshoot
azd monitor
# 8. Delete resources
azd down
Below you will see the commands in action:
azd auth login
Let’s authenticate to Azure:
azd init
Let’s download a template and then initialize the workspace:
Let’s take a look to the .azure folder:
As I wrote before, this folder contains the environment configuration. In this case, only one environment has been created, “demo”, but multiple environments can be created.
azd up
This is my favorite command. Hold on tight!
With a single command we created the infrastructure and deployed the applications.
azd show
Let’s have a look at the apps that we just published:
azd deploy
Let’s publish changes to the webapp:
azd monitor
Finally, we can monitor our application:
TIP: run the following command to go to Application Insights Live Metrics
azd monitor --live
azd down
Here is how to cleanup Azure resources:
CI/CD Pipeline Support
Pipeline Configuration
The azd pipeline config
command automates provisioning and deployment of CI/CD pipelines using pipeline definition files included in azd templates.
Supports:
- Azure Pipelines
- Github Actions
Configuration Steps
- Authentication with Azure
- CI/CD platform selection
- Repository configuration
- Setup of the service principal
- Setup of the authentication:
- GitHub: OpenID Connect (OIDC) or client credentials
- Azure Pipelines: Workload identity federation (OIDC) or client credentials
- Provisioning of the pipeline files
- Creation of pipeline variables and secrets
- Commit and push changes
- Trigger pipeline runs
azd pipeline config demo
Let’s see the command in action:
Note: As you can see from the first screenshot, I chose Azure Devops provider, so i used a PAT. The PAT must have the following scopes:
- Agent Pools (read, manage)
- Build (read and execute)
- Code (full)
- Project and team (read, write, and manage)
- Release (read, write, execute, and manage)
- Service Connections (read, query, and manage)
Behind the scenes, azd cli created:
- a new Azure Devops Project;
- a GIT repository for the project;
- an Azure resource group
- a managed identity on Azure, using Workload Identity Federation;
- a service connection (with workload identity federation) on Azure Devops, using the managed identity created previously;
- the CI/CD pipeline using the provided pipeline definition (azd-dev.yaml);
- pipeline variables and secrets. IMPORTANT: secrets are stored in Azure Key vault;
- Key vault read access role assignment for the managed identity, so it can retrieve secrets during pipeline execution.
Impressive, right? :)
It would have taken me longer to do the same things without azd cli.
Event Hooks
Hooks can execute custom scripts before and after azd commands or service lifecycle events. They are configured in azure.yaml file with OS-specific support (Windows or Posix).
Command Hooks
- prerestore and postrestore
- preprovision and postprovision
- predeploy and postdeploy
- preup and postup
- predown and postdown
Service Lifecycle Hooks
- prerestore and postrestore
- prebuild and postbuild
- prepackage and postpackage
- predeploy and postdeploy
azd compose (alpha)
azd config set alpha.compose on
I find this command very useful, even though the set of available resources is still limited, because I can create projects from scratch and have a well-set up IAC starter template, in a short time.
azd add
azd add
command creates resources without manual IAC templates.
Infrastructure state is tracked in-memory.
azd infra gen
azd infra gen
or azd infra synth
converts state to Bicep files.
azd CLI Extensions (alpha)
azd config set alpha.extensions on
The extensions are modular components that extend azd CLI functionality.
Extension Sources
Extensions are distributed and managed through extension sources (an equivalent concept to NuGet or NPM feeds):
- Official registry: https://aka.ms/azd/extensions/registry
- Development registry: https://aka.ms/azd/extensions/registry/dev
Extension sources are manifest files providing lists of available extensions, following the official schema.
Get Started with azd CLI
Install azd CLI
# Windows
winget install microsoft.azd
# macOS
brew tap azure/azd && brew install azd
# Linux
curl -fsSL https://aka.ms/install-azd.sh | bash
Try It Out
azd auth login
azd init --template todo-csharp-sql
azd up
Resources
Documentation: aka.ms/azd
Templates: aka.ms/awesome-azd
Blog: Azure SDK Blog
See you at the next Azure Meetup Casteddu events!
Join Azure Meetup Casteddu on Whatsapp
Leave a comment