Tutorial: Deploy an event-driven job with Azure Container Apps

Azure Container Apps jobs allow you to run containerized tasks that execute for a finite duration and exit. You can trigger a job execution manually, on a schedule, or based on events. Jobs are best suited to for tasks such as data processing, machine learning, resource cleanup, or any scenario that requires serverless ephemeral compute resources.

In this tutorial, you learn how to work with event-driven jobs.

  • Create a Container Apps environment to deploy your container apps
  • Create an Azure Storage Queue to send messages to the container app
  • Build a container image that runs a job
  • Deploy the job to the Container Apps environment
  • Verify that the queue messages are processed by the container app

The job you create starts an execution for each message that is sent to an Azure Storage queue. Each job execution runs a container that performs the following steps:

  1. Gets one message from the queue.
  2. Logs the message to the job execution logs.
  3. Deletes the message from the queue.
  4. Exits.

Important

The scaler monitors the queue's length to determine how many jobs to start. For accurate scaling, don't delete a message from the queue until the job execution has finished processing it.

The source code for the job you run in this tutorial is available in an Azure Samples GitHub repository.

Prerequisites

Setup

  1. To sign in to Azure from the CLI, run the following command and follow the prompts to complete the authentication process.

    az login
    
  2. Ensure you're running the latest version of the CLI via the upgrade command.

    az upgrade
    
  3. Install the latest version of the Azure Container Apps CLI extension.

    az extension add --name containerapp --upgrade
    
  4. Register the Microsoft.App and Microsoft.OperationalInsights namespaces if you haven't already registered them in your Azure subscription.

    az provider register --namespace Microsoft.App
    az provider register --namespace Microsoft.OperationalInsights
    
  5. Now that your Azure CLI setup is complete, you can define the environment variables that are used throughout this article.

    $RESOURCE_GROUP="jobs-quickstart"
    $LOCATION="chinanorth3"
    $ENVIRONMENT="env-jobs-quickstart"
    $JOB_NAME="my-job"
    

Create a Container Apps environment

The Azure Container Apps environment acts as a secure boundary around container apps and jobs so they can share the same network and communicate with each other.

  1. Create a resource group using the following command.

    az group create --name $RESOURCE_GROUP  --location $LOCATION
    
  2. Create the Container Apps environment using the following command.

    az containerapp env create --name $ENVIRONMENT --resource-group $RESOURCE_GROUP --location $LOCATION
    

Set up a storage queue

The job uses an Azure Storage queue to receive messages. In this section, you create a storage account and a queue.

  1. Define a name for your storage account.

    STORAGE_ACCOUNT_NAME="<STORAGE_ACCOUNT_NAME>"
    QUEUE_NAME="myqueue"
    

    Replace <STORAGE_ACCOUNT_NAME> with a unique name for your storage account. Storage account names must be unique within Azure and be from 3 to 24 characters in length containing numbers and lowercase letters only.

  2. Create an Azure Storage account.

    az storage account create \
        --name "$STORAGE_ACCOUNT_NAME" \
        --resource-group "$RESOURCE_GROUP" \
        --location "$LOCATION" \
        --sku Standard_LRS \
        --kind StorageV2
    
  3. Save the queue's connection string into a variable.

    QUEUE_CONNECTION_STRING=`az storage account show-connection-string -g $RESOURCE_GROUP --name $STORAGE_ACCOUNT_NAME --query connectionString --output tsv`
    

Build and deploy the job

To deploy the job, you must first build a container image for the job and push it to a registry. Then, you can deploy the job to the Container Apps environment.

  1. Define a name for your container image and registry.

    CONTAINER_IMAGE_NAME="queue-reader-job:1.0"
    CONTAINER_REGISTRY_NAME="<CONTAINER_REGISTRY_NAME>"
    

    Replace <CONTAINER_REGISTRY_NAME> with a unique name for your container registry. Container registry names must be unique within Azure and be from 5 to 50 characters in length containing numbers and lowercase letters only.

  2. Create a container registry.

    az acr create \
        --name "$CONTAINER_REGISTRY_NAME" \
        --resource-group "$RESOURCE_GROUP" \
        --location "$LOCATION" \
        --sku Basic \
        --admin-enabled true
    
  3. The source code for the job is available on GitHub. Run the following command to clone the repository and build the container image in the cloud using the az acr build command.

    az acr build \
        --registry "$CONTAINER_REGISTRY_NAME" \
        --image "$CONTAINER_IMAGE_NAME" \
        "https://github.com/Azure-Samples/container-apps-event-driven-jobs-tutorial.git"
    

    The image is now available in the container registry.

The event-driven job is now created in the Container Apps environment.

Verify the deployment

The job is configured to evaluate the scale rule every 60 seconds, which checks the number of messages in the queue. For each evaluation period, it starts a new job execution for each message in the queue, up to a maximum of 10 executions.

To verify the job was configured correctly, you can send some messages to the queue, confirm that job executions are started, and the messages are logged to the job execution logs.

  1. Send a message to the queue.

    az storage message put \
        --content "Hello Queue Reader Job" \
        --queue-name "$QUEUE_NAME" \
        --connection-string "$QUEUE_CONNECTION_STRING"
    

Tip

Having issues? Let us know on GitHub by opening an issue in the Azure Container Apps repo.

Clean up resources

Once you're done, run the following command to delete the resource group that contains your Container Apps resources.

Caution

The following command deletes the specified resource group and all resources contained within it. If resources outside the scope of this tutorial exist in the specified resource group, they will also be deleted.

az group delete \
    --resource-group $RESOURCE_GROUP

Next steps