Photo by fabio

Create an Azure Container Apps environment using Terraform

One environment for your containers

Posted by Damien Aicheh on 05/05/2023 · 3 mins

In this tutorial we will setup the Azure Container Apps environment using Terraform and link it with a Log Analytics workspace. This will be done in the same resource group that we created in the previous tutorial.

This tutorial is part of a full series of tutorials on using Azure Container Apps with Terraform.

Attach a Log Analytics workspace

Before creating the Azure Container Apps environment we will create a Log Analytics workspace. This will allow us to monitor the environment with Grafana. You will discover this in a future tutorial of this series.

For that, create a file called log_analytics.tf and add this code:

resource "azurerm_log_analytics_workspace" "this" {
  name                = format("log-%s", local.resource_suffix_kebabcase)
  location            = azurerm_resource_group.this.location
  resource_group_name = azurerm_resource_group.this.name
  sku                 = "PerGB2018"
  retention_in_days   = 30
}

We will use only 30 days of logs retentions but feel free to adjust this value and the associated sku to your needs.

Create the Azure Container Apps environment

Next step is to create the Azure Container Apps environment. To do that, create a file called ca-env.tf and add this code:

resource "azurerm_container_app_environment" "this" {
  name                       = format("cae-%s", local.resource_suffix_kebabcase)
  location                   = azurerm_resource_group.this.location
  resource_group_name        = azurerm_resource_group.this.name
  log_analytics_workspace_id = azurerm_log_analytics_workspace.this.id
}

As you can see, you use the azurerm_container_app_environment resource to create the environment. Then you reference the azurerm_log_analytics_workspace to retrieve the Log Analytics workspace that you created in the previous step.

Run Terraform

Let’s run Terraform plan command and apply it:

terraform plan --out=plan.out

Then:

terraform apply plan.out

Final touch

You have your Azure Container Apps environment and the Log Analytics workspace deployed. You will find the complete source code in this Github repository.

What’s next?

In the next tutorial of this series we will focus on deploying the Azure Container Apps and link them with the Azure Container Registry.

Do not hesitate to follow me on to not miss my next tutorial!