Use Terraform to connect ACR with Azure Web App
You can connect an Azure Web App to Docker Hub, Private Repository and also an Azure Container Registry(ACR). Using Terraform you can take it a step further and build your whole infrastructure environment at the same time as connecting these container registries. However, how do you connect them together in Terraform?
I am going to focus on the connection of an ACR, but you can also follow the same method for the other providers.
Why I am using this as an example, is when correcting the other methods they are a simple URL, username and password, but the Azure Container Registry within the portal has a different user interface where it connects natively in the Azure. Why I was learning to do this, I kept getting my ACR connecting like a private repository instead of an actual ACR. Therefore, the method below will have the desired outcome of within the Azure portal the Web App showing it is connected to an ACR.
I will go through the general setup I have got for a simple Web App connecting to an ACR with all of the supporting elements. I am not showing best practice of having the variables and outputs in separate files as this is not the point of the post, but I would encourage people to do that.
First we will need to create the infrastructure to support the Web App, by connecting to the Azure Resource Manager provider in Terraform:
provider "azurerm" { version = "=2.25.0" subscription_id = var.subscription_id features {} }
This passes a ‘subscription_id’ variable to connect to the correct subscription. We then create the Resource Group to contain all the resources.
variable "resource_group_name" { type = string description = "Azure Resource Group Name. " } variable "location" { type = string description = "Azure Resource Region Location" } # Create a Resource Group resource "azurerm_resource_group" "acr-rg" { name = var.resource_group_name location = var.location }
The next part is to create the Azure Container Registry with your chosen name and the SKU for the service level you would like. For this example we have use the ‘Standard’ to keep it cheap and simple, while using the same location as the Resource Group.
variable "container_registry_name" { type = string description = "Azure Container Registry Name" } # Azure Container Regristry resource "azurerm_container_registry" "acr" { name = var.container_registry_name resource_group_name = azurerm_resource_group.acr-rg.name location = azurerm_resource_group.acr-rg.location sku = "Standard" admin_enabled = true }
For the Web App we will need an App Service Plan to contain the Web App and set the SKU Level. You can see this is the same as before using the same locations and also I am using Linux as the base operating system.
variable "app_plan_name" { type = string description = "Azure App Service Plan Name" } # App Plan resource "azurerm_app_service_plan" "service-plan" { name = var.app_plan_name location = azurerm_resource_group.acr-rg.location resource_group_name = azurerm_resource_group.acr-rg.name kind = "Linux" reserved = true sku { tier = "Standard" size = "S1" } }
Now is where we declare the Web App itself, but first create the 3 variables we will need. The Web App name, your Registry name and the Tag assigned to your image.
variable "web_app_name" { type = string description = "Azure Web App Name" } variable "registry_name" { type = string description = "Azure Web App Name" } variable "tag_name" { type = string description = "Azure Web App Name" default: 'latest' }
To link to Docker Registries you need 3 App Settings configured ‘ DOCKER_REGISTRY_SERVER_URL’, ‘ DOCKER_REGISTRY_SERVER_USERNAME’, and ‘DOCKER_REGISTRY_SERVER_PASSWORD’.
These are used to gain the correct access to the registries.
For the ACR, the URL is the ‘Login Server’ and then the username/password is the Admin Username/Password.
These can be found here in the portal, if your ACR is already created.
See the full example and other configurations on original post https://prcode.co.uk/2020/10/28/use-terraform-to-connect-acr-with-azure-web-app/