Speed Up Your Content with Azure Content Delivery Network: A Simple Guide
Table of contents
If you're building applications or hosting content that needs to reach users globally with lightning speed, Azure CDN (Content Delivery Network) is your go-to solution. Azure CDN accelerates content delivery by caching content closer to your users' locations, reducing latency, and enhancing performance.
In this blog post, I'll guide you through setting up Azure CDN step by step. I'll also include commands, placeholders for values, and explanations to ensure a seamless setup experience.
Prerequisites
Before diving in, ensure you have the following:
An active Azure subscription.
Azure CLI installed and configured on your system.
Step 1: Define Placeholders in CLI
At the start of your Azure CLI session, define the placeholders for reusability:
RESOURCE_GROUP="<value>"
LOCATION="<value>"
STORAGE_ACCOUNT="<value>"
CDN_PROFILE_NAME="<value>"
CDN_ENDPOINT_NAME="<value>"
ORIGIN_HOSTNAME="<value>"
ORIGIN_GROUP_NAME="<value>"
CUSTOM_DOMAIN="<value>"
CUSTOM_HOSTNAME="<value>"
Replace <value>
with your specific values when using these commands.
Step 2: Create a Resource Group
A resource group is a container that holds your Azure resources. Start by creating one:
az group create --name $RESOURCE_GROUP --location $LOCATION
Step 3: Create a Storage Account
The storage account will serve as the origin for your CDN. Use the following command to create it:
az storage account create --resource-group $RESOURCE_GROUP --location $LOCATION --sku Standard_LRS
Step 4: Create a CDN Profile
The CDN profile is a container for your CDN settings and endpoints:
az cdn profile create --resource-group $RESOURCE_GROUP --name $CDN_PROFILE_NAME --sku $Standard_Microsoft
Step 5: Create a CDN Endpoint
A CDN endpoint is where requests are processed and cached. Create one pointing to your storage account:
az cdn endpoint create --resource-group $RESOURCE_GROUP --profile-name $CDN_PROFILE_NAME --name $CDN_ENDPOINT_NAME --origin $ORIGIN_HOSTNAME
Step 6: Configure an Origin Group (Optional)
Origin groups allow you to define multiple origins for failover scenarios. Create an origin group:
az cdn origin-group create --profile-name $CDN_PROFILE_NAME --endpoint-name $CDN_ENDPOINT_NAME --resource-group $RESOURCE_GROUP --origin-group $ORIGIN_GROUP_NAME
Update the endpoint to use this origin group as the default:
az cdn endpoint update --resource-group $RESOURCE_GROUP --profile-name $CDN_PROFILE_NAME --endpoint-name $CDN_ENDPOINT_NAME --default-origin-group $ORIGIN_GROUP_NAME
Step 7: Add an Origin
Substep: Retrieve and Use Origin Information
Before adding a new origin, you can list existing origins and retrieve their names to ensure proper configuration.
List Existing Origins
Use the following command to get a list of origins for your CDN endpoint:az cdn origin list --resource-group $RESOURCE_GROUP --endpoint-name $CDN_ENDPOINT_NAME --profile-name $CDN_PROFILE_NAME
Extract Origin Name
Identify the desired origin's name from the output. This can help in avoiding naming conflicts or confirming existing configurations.
Once you have the necessary details, proceed with the command to add a new origin:
az cdn origin create --resource-group $RESOURCE_GROUP --endpoint-name $CDN_ENDPOINT_NAME --profile-name $CDN_PROFILE_NAME --name $ORIGIN_NAME --host-name $ORIGIN_HOSTNAME
Step 8: Configure a Custom Domain
To associate a custom domain with your CDN endpoint and enable HTTPS, follow these steps:
Associate the custom domain with the endpoint:
az cdn custom-domain create --resource-group $RESOURCE_GROUP --endpoint-name $CDN_ENDPOINT_NAME --profile-name $CDN_PROFILE_NAME --name $CUSTOM_DOMAIN --hostname $CUSTOM_HOSTNAME
Enable HTTPS on the custom domain:
az cdn custom-domain enable-https --resource-group $RESOURCE_GROUP --endpoint-name $CDN_ENDPOINT_NAME --profile-name $CDN_PROFILE_NAME --name $CUSTOM_DOMAIN
Extras: Preloading and Purging
These steps are optional but useful for managing CDN content:
Preload content to the CDN edge servers:
az cdn endpoint load --profile-name $CDN_PROFILE_NAME --name $CDN_ENDPOINT_NAME --resource-group $RESOURCE_GROUP --content-paths '/cdn/og-image.jpg'
Purge content from the CDN cache:
az cdn endpoint purge --profile-name $CDN_PROFILE_NAME --name $CDN_ENDPOINT_NAME --resource-group $RESOURCE_GROUP --content-paths '/cdn/banner.jpg'
Automate Purging with Event Grid and Azure Functions (Additional Approach)
Automate Azure CDN cache purging efficiently using Azure Event Grid and Azure Functions. This scalable and flexible setup triggers purging based on storage account changes like blob uploads or deletions.
Steps to Implement:
Set up Event Grid Subscription:
Monitor your storage account for specific events, ensuring notifications for changes requiring cache updates.Create an Azure Function:
Build a function to process these events and execute the CDN purge programmatically.
Why Event Grid?
Scalability: Handles high-throughput and large-scale workloads.
Decoupling: Enables multiple services to act independently on events.
Advanced Filtering: Targets specific events with precision.
Flexibility: Integrates with diverse Azure services for extensible workflows.
This approach ensures automated, reliable, and up-to-date cache management without manual intervention.
Final Thoughts
Azure CDN is a powerful tool for scaling your content delivery globally. By following this step-by-step guide, you’ve configured a CDN profile, endpoint, custom domain, and automated workflows for preloading and purging. Keep exploring its features to optimize your content delivery strategies!
For questions or improvements, feel free to connect with me on X.