DEV Community

Marco Platzer
Marco Platzer

Posted on

Using Azure Container Instance (ACI) for your API Development Workflow

With Azure Container Instances (ACI), you can spin up containers in no time, making it simple to test and review code functionality. Integrating ACI into a CI/CD workflow can significantly reduce the time to validate and deploy changes. This blog post introduces a quickstart template available on GitHub, which you can use to seamlessly integrate ACI into your development workflow.

Benefits

  • See the impact of your code changes almost immediately by deploying to a live containerized environment.
  • Test and validate code functionality in a consistent and isolated environment.
  • Avoid the complexity of setting up infrastructure for every change.

Bicep Overview and Key Components

This Bicep template facilitates the deployment of an Azure Container Instance (ACI) within a specified Azure Resource Group. It is tailored for hosting APIs and is configured to pull Docker images from a private Azure Container Registry (ACR).

Key Features

Here’s a snippet showcasing the use of the AVM for container group deployment:

@description('The avm module to deploy the container group')
module containerGroup 'br/public:avm/res/container-instance/container-group:0.4.1' = {
  name: guid(rgName, 'containerGroup')
  scope: rg
  params: {
    containers: [
      {
        name: aciName
        properties: {
          image: dockerImage
          ports: [network]
          resources: {
            requests: performance
          }
          environmentVariables: [
            {
              name: 'CLIENT_ID'
              value: aciName
            }
            {
              name: 'CLIENT_SECRET'
              secureValue: 'TestSecret'
            }
          ]
        }
      }
    ]
    name: containerGroupName
    ipAddressPorts: [
      network
    ]
    location: location
    imageRegistryCredentials: [
      {
        server: '${acrName}.azurecr.io'
        username: acrName
        password: acrPassword
      }
    ]
    tags: {
      application: 'quickstart-api-development-with-aci'
      environment: 'dev'
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

The admin account is currently required for some scenarios to deploy an image from a container registry to certain Azure services. For example, the admin account is needed when you use the Azure portal to deploy a container image from a registry directly to Azure Container Instances or Azure Web Apps for Containers. Learn More


CI/CD Pipeline with GitHub Actions

To complement the Bicep template, a GitHub Actions pipeline enables seamless build, test, and deployment workflows. Here's what the pipeline includes:

  • Build Stage: Builds and pushes Docker images to ACR.
  • Test Stage: Runs linting and static analysis tools for Bicep and PowerShell scripts.
  • WAF Stage: Tests your Bicep code against the Azure Well-Architected Framework using PSRule.
  • Preview Stage: Deploys the infrastructure and application to ACI for testing.
  • Production Stage: Deployment to a production environment like Azure App Service, Azure Functions or AKS upon successful validation.

CI/CD Pipeline

Easily Set Up an Azure Project

Simplify your project initialization with the following script, which automates the creation of necessary resources and configurations. It performs the following tasks:

  • Creates an Azure AD Service Principal for GitHub Actions workflows.
  • Configures GitHub federated identity with Entra ID for secretless authentication in pipelines.
  • Assigns necessary Azure RBAC roles for ACI and ACR operations.
  • Configures GitHub repository secrets.

You can run the script locally or directly in the Azure Cloud Shell. Ensure you're authenticated to your Azure subscription using Azure PowerShell and to your GitHub repository with the GitHub CLI.

New-AzureProject -DisplayName "Quickstart ACI API Development" `
                -DockerImageName "quickstart-aci-dev-api" `
                -AciSubscriptionId "<SubscriptionID>" `
                -AcrSubscriptionId "<SubscriptionID>" `
                -AcrResourceGroup "rg-acr-prod-001" `
                -AcrName "latzox" `
                -GitHubOrg "Latzox" `
                -RepoName "quickstart-api-development-with-aci" `
                -EnvironmentNames @('build', 'preview')
Enter fullscreen mode Exit fullscreen mode

With Azure Container Instances and GitHub Actions, you can accelerate your development workflow by enabling rapid testing and deployment of containerized applications. The Bicep template and CI/CD pipeline outlined in this blog offer a robust starting point for integrating ACI into your projects. Visit the GitHub repository to get started today!

GitHub logo Latzox / quickstart-api-development-with-aci

A quickstart template for api development with Azure Container Instance. It includes demo code in Python, CI/CD pipelines and infra as code configurations to get you started.

Build Dev.to

Quickstart API Development with Azure Container Instance (ACI)

This repository contains the code and configurations for deploying and managing applications and infrastructure using Azure Container Instances (ACI), Azure Bicep, and Docker. It is structured to support various stages of development, from infrastructure provisioning to application deployment.

Repository structure

.github/
  workflows/
    build.yml                       # Complete Build and Deployment Workflow

app/  
  app.py                            # Python application source code
  Dockerfile                        # Dockerfile for building the application image

infra/ 
  main.bicep                        # Main Bicep template for infrastructure deployment

.gitignore                          # Git ignore rules
LICENSE                             # License information
New-AzureProject.ps1                # Initial setup script
ps-rule.yml                         # Config file for PSRule IaC Validation
README.md                           # Documentation file
Enter fullscreen mode Exit fullscreen mode

How to use

Clone the repository

Use this template to create a new repository. While this template is designed for Azure Container Instance (ACI) and Azure Container Registry (ACR) services, you can use it as a starting point for any similar project.

Requirements

Top comments (0)