Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Create an Azure Container Apps environment

As a first step you will need to create your Azure Container Apps (ACA) environment. Make sure you also pre-create the virtual network for your ACA service. This will make it easier in the following labs to add additional networking features. You can use the following guidance:

Step by step guidance

  1. On your lab computer, open the Git Bash window and, from the Git Bash prompt, run the following command to sign in to your Azure subscription:

    az login
    

    In case you are running this lab in a GitHub codespace, use az login --use-device-code.

  2. Executing the command will automatically open a web browser window prompting you to authenticate. Once prompted, sign in using the user account that has the Owner role in the target Azure subscription that you will use in this lab and close the web browser window.

  3. Make sure that you are logged in to the right subscription for the consecutive commands.

    az account list -o table
    
  4. If in the above statement you don’t see the right account being indicated as your default one, change your environment to the right subscription with the following command, replacing the <subscription-id>.

    az account set --subscription <subscription-id>
    
  5. install the Azure Container Apps extension for the CLI.

    az extension add --name containerapp --upgrade
    
  6. Now that the current extension or module is installed, register the Microsoft.App namespace.

    az provider register --namespace Microsoft.App
    
  7. Register the Microsoft.OperationalInsights provider for the Azure Monitor Log Analytics workspace.

    az provider register --namespace Microsoft.OperationalInsights
    
  8. Run the following commands to create a resource group that will contain all of your resources (replace the <azure-region> placeholder with the name of any Azure region in which you can create an ACA and an Azure Database for MySQL Flexible Server instance, see this page for regional availability details of those services:

    UNIQUEID=$(openssl rand -hex 3)
    APPNAME=petclinic
    RESOURCE_GROUP=rg-$APPNAME-$UNIQUEID
    LOCATION=<azure-region>
    az group create -g $RESOURCE_GROUP -l $LOCATION
    

    This lab uses quite some environment variables. In case you are using a codespace for running this lab, your environment variables will be lost if the codespace restarts. You can find a couple of methods for persisting these environment variables across codespace restarts in the LabTips.

  9. Create a virtual network and subnet for your ACA.

    VIRTUAL_NETWORK_NAME=vnet-$APPNAME-$UNIQUEID
    az network vnet create \
        --resource-group $RESOURCE_GROUP \
        --name $VIRTUAL_NETWORK_NAME \
        --location $LOCATION \
        --address-prefix 10.1.0.0/16
       
    ACA_SUBNET_CIDR=10.1.0.0/27
    az network vnet subnet create \
        --resource-group $RESOURCE_GROUP \
        --vnet-name $VIRTUAL_NETWORK_NAME \
        --address-prefixes $ACA_SUBNET_CIDR \
        --name aca-subnet \
        --delegations Microsoft.App/environments
    
  10. You will need the ID of this subnet when you create the ACA environment.

    SUBNET_ID=$(az network vnet subnet show --resource-group $RESOURCE_GROUP --vnet-name $VIRTUAL_NETWORK_NAME --name aca-subnet --query id -o tsv)
    
  11. Create your ACA environment and link it to the subnet you just created. We will be creating the environment in the dedicated plan. This plan is also called workload profiles plan. This plan gives you more advanced features as opposed to the consumption plan. In this plan you can choose whether you want to run your workload on dedicated hardware or consumption based. It basically gives you the choice.

    ACA_ENVIRONMENT=acaenv-$APPNAME-$UNIQUEID
       
    az containerapp env create \
        -n $ACA_ENVIRONMENT \
        -g $RESOURCE_GROUP \
        --location $LOCATION \
        --enable-workload-profiles true \
        --infrastructure-subnet-resource-id $SUBNET_ID \
        --logs-destination none 
    

    If you get error message for the resource id, then run the az command with MSYS_NO_PATHCONV=1 in the front. For example, export MSYS_NO_PATHCONV=1 az containerapp env create . Wait for the provisioning to complete. This might take about 5 minutes.

  12. In your browser navigate to the Azure portal.

  13. Navigate to resource groups and select the resource group you just created.

  14. In the resource group overview you will see your newly created Azure container app environment.

    In case you don’t see the container app environment in the overview list of the resource group, hit the refresh button a couple of times, until they show up.

    You may also notice an additional resource group in your subscription, which name will start with ME_. This resource group got created by the ACA creation process. It holds the resources of your ACA environment. For learning purposes it might be good to check this resource group from time to time and to see what got created there.