Remove Azure App Service Hostnames using Terraform Expression
Date published:
In this blog post, we’ll focus on a specific use case of Terraform with Azure - simplifying Azure hostnames.
When you create an Azure App Service, Azure automatically assigns it a default hostname, typically in the format {app_service_name}.azurewebsites.net. While this is useful for quickly accessing your app, there might be instances where you want to use a simplified version of the hostname, without the azurewebsites.net part.
Here’s how you can use it to remove azurewebsites.net from your Azure App Service’s default hostname:
replace(each.value.default_site_hostname, "/.\\w*.net/", "")
In this example, we’re using the replace
function to remove the .azurewebsites.net part from the default_site_hostname
of an Azure App Service. The search string “/.\w*.net/” is a regular expression that matches any string ending with .azurewebsites.net. The replacement string "" effectively removes the matched part from the original string.
If the default_hostname of your app service resource is app-service-hostname-example.azurewebsites.net, this line of code will change it to app-service-hostname-example.
Conclusion
Using Terraform’s replace
function, you can easily customize the hostnames of your Azure App Services as it only changes the string in your Terraform configuration. I hope you found this blog post helpful.