Introduction
Azure Static Web Apps provide a streamlined way to deploy and host static websites with serverless APIs. In this blog post, we’ll walk through how to use Terraform to deploy an Azure Static Web App, configure DNS settings, and set up a custom domain.
Why Use Terraform?
Terraform allows you to define and provision your infrastructure using a high-level configuration language. It makes infrastructure management reproducible, predictable, and version-controlled. By using Terraform to set up your custom domain for Azure Static Web Apps, you ensure a consistent and automated deployment process.
Prerequisites
Before we get started, ensure you have:
- An Azure account.
- Terraform installed on your local machine.
- Basic knowledge of Terraform and Azure services.
Step 1: Create an Azure Resource Group
A Resource Group in Azure is a container that holds related resources for an Azure solution. Let’s start by defining a resource group.

Step 2: Create the Azure Static Web App
Next, deploy an Azure Static Web App within the resource group:

Step 3: Set Up a DNS Zone
To map your custom domain to the Azure Static Web App, you need a DNS zone. Define it as follows:

Step 4: Create a DNS CNAME Record
A CNAME record maps your custom domain or subdomain to your static web app’s default hostname. Add the following configuration:

Step 5: Configure the Custom Domain
Finally, link your custom domain to the Azure Static Web App by defining a custom domain resource:

Conclusion
In this blog post, we walked through the process of setting up a custom domain for an Azure Static Web App using Terraform. By following these steps, you have successfully associated your web app with a custom domain, enhancing both its branding and accessibility.
Using Terraform for this setup not only ensures a consistent and repeatable deployment but also integrates seamlessly with version control, making infrastructure management more robust. Adjust the provided Terraform configurations as needed to fit your specific use case and environment.