Quickstart: Container Apps using Bicep

Posted by Jason on Wednesday, April 6, 2022

This quickstart is a supplement to the existing Azure Container Apps docs and provides a Bicep walkthrough to getting started.

Quickstart: Deploy you first container app using Bicep

Azure Container Apps is a serverless platform to run and orchestrate your container applications.

In this quickstart, you’ll use Bicep to create a Container Apps environment with the helloworld sample container image. If you have not used Bicep yet or want to learn more about what it can do, I recommend going through the Fundamentals of Bicep learning path to get you started.

Prerequisites

Setup

First you’ll need to sign in to Azure using the CLI (I typically use the device code option but it may not be necessary for you). Run the command below and follow the prompts to get logged into your account.

az login --use-device-code

If you haven’t used your Azure CLI installation for awhile, you may want to make sure it is up-to-date before continuing on. You can do this by running the following:

az upgrade

If you haven’t already installed the Azure Container Apps extension, you will need to run the following command:

az extension add --name containerapp

Next you will need to register the Microsoft.App namespace, by running:

az provider register --namespace Microsoft.App

That should be your Azure account setup, now you need to run the following in order to set the environment variables we’ll use:

RESOURCE_GROUP="my-container-apps"
LOCATION="eastus2"
CONTAINERAPP_NAME="my-container-app"
CONTAINERAPPS_ENVIRONMENT="my-environment"

Now you can create the resource group we’ll use for this quickstart:

az group create \
  -n $RESOURCE_GROUP \
  -l $LOCATION

Create a Bicep template

You need to create a Bicep template containing all the resources for this walkthrough:

  • Log Analytics Workspace
  • Container Apps Environment
  • Container App

Copy and save the following text into a file named hello-world-aca.bicep:

param location string = resourceGroup().location
param environmentName string
param appName string
param logAnalyticsWorkspaceName string = '${appName}-logs'
param containerImage string = 'mcr.microsoft.com/azuredocs/containerapps-helloworld:latest'
param containerPort int = 80

resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2020-03-01-preview' = {
  name: logAnalyticsWorkspaceName
  location: location
  properties: any({
    retentionInDays: 30
    features: {
      searchVersion: 1
    }
    sku: {
      name: 'PerGB2018'
    }
  })
}

resource environment 'Microsoft.App/managedEnvironments@2022-01-01-preview' = {
  name: environmentName
  location: location
  properties: {
    appLogsConfiguration: {
      destination: 'log-analytics'
      logAnalyticsConfiguration: {
        customerId: logAnalyticsWorkspace.properties.customerId
        sharedKey: logAnalyticsWorkspace.listKeys().primarySharedKey
      }
    }
  }
}

resource containerApp 'Microsoft.App/containerApps@2022-01-01-preview' = {
  name: appName
  location: location
  properties: {
    managedEnvironmentId: environment.id
    configuration: {  
      ingress: {
        external: true
        targetPort: containerPort
      }
    }
    template: {
      containers: [
        {
          image: containerImage
          name: appName
        }
      ]
    }
  }
}

output fqdn string = containerApp.properties.configuration.ingress.fqdn

Deploy resources

Next you need to navigate to the directory you saved the hello-world-aca.bicep file and run the following command:

az deployment group create \
  -g $RESOURCE_GROUP \
  -f ./hello-world-aca.bicep \
  -p environmentName=$CONTAINERAPPS_ENVIRONMENT \
  -p appName=$CONTAINERAPP_NAME \
  --query properties.outputs.fqdn.value

Once the resources have been deployed, the fully qualified domain name will be returned in the output. Mine looks like this:

Result
-----------------------------------------------------------------
my-container-app.calmocean-5f9a8879.eastus2.azurecontainerapps.io

Verify

Copy the fully qualified domain name returned from the deployment and paste it into a browser to verify the hello world Container App is running. The container app should like like this: Screenshot of helloworld app

Clean up resources

When you are done, don’t forget to delete your resource group to remove all the resources created in this quickstart:

az group delete \
  -g $RESOURCE_GROUP