This gist contains a create and destroy script to provision and cleanup custom domains and managed certficates assigned to Azure Container Apps.
This gist supports the following workflow:
- Bind custom domain to existing Container App
- Provision a managed certificate for the custom domain
- Bind the managed certificate to the custom domain
Please note, the scripts were created with AZCLI version 2.53.0 in mind. If the scripts are failing it may be because of your version of AZCLI, please check and modify the script or your version if you encounter problems
The create script has the following checks throughout its execution to prevent failing on edge cases:
- Makes sure an
asuid
TXT record exists before binding the custom domain - Checks if there's an already provisioned, managed certificate for the domain
- Lastly checks if the custom domain already has a managed certificate bound
I've also included a terraform module to execute the scripts in the gist and handle some sense of state for the resource.
These variables are used across both create and destroy scripts
CUSTOM_DOMAIN
- The custom domain that is being/has been assigned to the container appCONTAINER_APP_NAME
- The name of the container app the custom domain is being/has been assigned toRESOURCE_GROUP
- The resource group name the container app has been created inCONTAINER_APP_ENV_NAME
- The name of the container app environment name the container app has been assigned to
The create script has been updated to include further checks to increase confidence of successful script
execution. These checks include using dig
to check for a valid asuid TXT record, and instead of waiting
for 5 minutes and hoping Azure has provisioned the cert, query the provisioning state of the cert every 15
seconds in a loop to check for success status.
A destroy script has been added to cleanup provisioned managed certificates assigned to the container app and unbind the hostname in the process. This script could use some fleshing out, usually this gets executed when destroying the container app (which might not be necessary) or changing custom domains.
Included are terraform files using the null_resource
and local-exec
provisioners to execute the script and
provide some sort of runtime state to terraform managing the certificate resources in azure. The triggers on
the resource help control the state to determine when to delete/create the resource based on the container app.
Initial creation of the gist with a create script to provision a managed certificate for a custom domain assigned to an Azure Container App.
Great script, thanks @LynnAU
I found line 36 fails because the Azure CLI only looks in the certificates namespace. See Azure/azure-cli#29119
I had to change lines 17 and 19 to capture the resource ID rather than the name, and that resolved it for me.