Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 62

Caching Azure Blob Storage with CDN

Recommended resources

Hello! Let's continue our exploration of optimizing applications in Azure.

In the previous lesson, we established the "what" and "why" of Azure Content Delivery Networks (CDNs), understanding their purpose in reducing latency by caching static content at globally distributed Points of Presence (PoPs).

Today, we'll transition from theory to practice. This lesson is a hands-on lab designed to achieve the learning outcome: Create a CDN profile and endpoint to cache content from an Azure Blob Storage account. You will set up the essential infrastructure to serve content from the edge, a core skill for building performant Azure solutions and a key topic for the AZ-204 exam.

1. Review: CDN Profiles and Endpoints

Before we start creating resources, let's clarify the two main components you'll be working with:

  • CDN Profile: A CDN profile is a container for one or more CDN endpoints. It defines a pricing tier and a provider (e.g., Microsoft Standard, Akamai Standard). Think of it as a management boundary for your CDN configuration. For most general purposes, including what's required for the AZ-204, the Azure CDN Standard from Microsoft tier is the go-to choice.

  • CDN Endpoint: This is the specific entity that delivers content to users. It provides a publicly accessible URL (e.g., your-endpoint-name.azureedge.net) and is configured to pull content from a specific origin.

  • Origin: The origin is the source of your content. While it can be an App Service or any publicly accessible web server, a very common origin for static assets is an Azure Blob Storage account.

When you create an endpoint, you link it to an origin. When a request hits the endpoint for the first time (a "cache miss"), the CDN fetches the content from the origin, caches it at the edge, and then serves it. Subsequent requests are served directly from the cache (a "cache hit").


2. Hands-On Lab: Serving a File from the Edge

In this lab, we will perform the following steps:

  1. Create an Azure Storage Account and a container for public access.
  2. Upload a sample file to serve as our static content.
  3. Create a CDN profile and an endpoint that uses the storage account as its origin.
  4. Test that the file is accessible through the new CDN endpoint URL.

We will primarily use the Azure CLI, which is a crucial tool for developers and frequently tested on the AZ-204 exam.

Step 1: Prepare the Origin (Blob Storage)

First, we need a source for our static content. Let's create a storage account and a container. For the CDN to be able to read the files, the container's access level must be set to allow public read access for blobs.

Open your local terminal with the Azure CLI installed and authenticated.

  1. Set some variables to make the commands easier to write. Remember to choose a unique name for your storage account and CDN endpoint.

    RESOURCE_GROUP="your-resource-group-name" # Use the resource group from previous lessons
    LOCATION="eastus"
    STORAGE_ACCOUNT_NAME="cdnstor<your-initials-and-date>" # Must be globally unique
    CONTAINER_NAME="assets"
    CDN_PROFILE_NAME="cdn-profile-${STORAGE_ACCOUNT_NAME}"
    CDN_ENDPOINT_NAME="cdn-endpoint-${STORAGE_ACCOUNT_NAME}" # Must be globally unique
    
  2. Create the storage account.

    az storage account create \
      --name $STORAGE_ACCOUNT_NAME \
      --resource-group $RESOURCE_GROUP \
      --location $LOCATION \
      --sku Standard_LRS \
      --kind StorageV2
    
  3. Create a blob container with public access set to blob.

    az storage container create \
      --name $CONTAINER_NAME \
      --account-name $STORAGE_ACCOUNT_NAME \
      --public-access blob
    
  4. Create and upload a sample file. Let's create a simple text file and upload it.

    echo "Hello from the origin server!" > sample.txt
    
    az storage blob upload \
      --account-name $STORAGE_ACCOUNT_NAME \
      --container-name $CONTAINER_NAME \
      --name sample.txt \
      --file sample.txt
    

At this point, your origin is ready. You have a publicly accessible file in Azure Blob Storage.

Step 2: Create the CDN Profile and Endpoint

Now we will create the CDN resources and point them to the storage account you just configured. For a visual overview of how this looks in the Azure Portal, the following short video demonstrates the same core concepts.

How To Setup an Azure CDN Backed by an Azure Blob Storage Account

This video from Jon Gallant provides a quick, practical walkthrough of creating a CDN backed by a blob storage account using the Azure Portal. It's helpful for visualizing the resources we are about to create with the CLI.

Watch the video from the beginning to 01:42. You will see the creation of the storage account, the CDN profile, and the CDN endpoint. Notice how the endpoint is linked to the storage account as its origin.

Now, let's perform these same steps using the Azure CLI.

  1. Create the CDN Profile. We'll use the Standard_Microsoft SKU, which is sufficient for our needs.

    az cdn profile create \
      --name $CDN_PROFILE_NAME \
      --resource-group $RESOURCE_GROUP \
      --sku Standard_Microsoft
    
Azure CDN Endpoint Configuration in Azure Portal
This image shows the equivalent page in the Azure Portal for creating a CDN profile and endpoint, where you select the pricing tier and provide names for your resources.
  1. Create the CDN Endpoint. This is the critical step where you link the endpoint to your blob storage origin.

    
    
    
    # First, get the hostname of your storage account
    ORIGIN_HOSTNAME=$(az storage account show --name $STORAGE_ACCOUNT_NAME --query "primaryEndpoints.blob" --output tsv | sed 's/https://' | sed 's|/||')
    
    
    
    
    # Now, create the endpoint
    az cdn endpoint create \
      --name $CDN_ENDPOINT_NAME \
      --profile-name $CDN_PROFILE_NAME \
      --resource-group $RESOURCE_GROUP \
      --origin $ORIGIN_HOSTNAME
    

    A key parameter here is --origin. This tells the CDN where to fetch content from during a cache miss. The command automatically sets the origin host header to the same value, which is crucial for Azure Storage to correctly handle the request from the CDN.

You can learn more about these commands and their Bicep equivalents in the following article.

How to Integrate Azure CDN with Azure Blob Storage for Faster ...

This article, 'How to Integrate Azure CDN with Azure Blob Storage...', provides excellent Azure CLI and Bicep examples that align perfectly with our lab.

Briefly review the section titled 'Creating the CDN Profile and Endpoint'. Focus on the Azure CLI commands and the explanation for the --origin-host-header parameter. This will solidify your understanding of the commands you just ran.

Step 3: Test the CDN Endpoint

It can take several minutes (sometimes 10 or more) for a new CDN endpoint to propagate across all edge locations. If you try to access it immediately, you will likely see a "404 Not Found" error. This is normal.

  1. Get the Blob and CDN URLs.

    
    
    
    # Direct Blob URL
    BLOB_URL="https://$STORAGE_ACCOUNT_NAME.blob.core.windows.net/$CONTAINER_NAME/sample.txt"
    
    
    
    
    # CDN Endpoint URL
    CDN_URL="http://$CDN_ENDPOINT_NAME.azureedge.net/$CONTAINER_NAME/sample.txt"
    
    echo "Direct Blob URL: $BLOB_URL"
    echo "CDN URL: $CDN_URL"
    
  2. Test the direct blob URL. Open the Direct Blob URL in your browser. It should immediately display "Hello from the origin server!".

  3. Test the CDN URL. Wait a few minutes, then open the CDN URL in your browser.

    • First Request (Cache Miss): The first time you access this URL, the CDN edge server closest to you receives the request. It doesn't have the file, so it contacts the origin (your blob storage), retrieves sample.txt, caches it, and then sends it to your browser. This might feel slightly slow.
    • Second Request (Cache Hit): Refresh your browser. This time, the edge server finds sample.txt in its local cache and serves it directly. The response should be significantly faster.

Congratulations! You have successfully configured a CDN to cache and serve content from an Azure Blob Storage account.


Conclusion

In this lesson, you moved from theory to a practical, hands-on implementation. You created the core Azure resources required to build a content delivery network, linking a globally-available CDN endpoint to a specific storage origin.

Key Takeaways:

  • A CDN Profile acts as a container for endpoints and defines the pricing tier.
  • A CDN Endpoint provides the public URL (*.azureedge.net) and is mapped to an origin.
  • For a CDN to access blobs, the storage container must allow public read access.
  • Creating a CDN endpoint via the Azure CLI requires the az cdn profile create and az cdn endpoint create commands.
  • CDN endpoint propagation is not instant and can take several minutes.

Next Steps

Now that you have a functioning CDN, a natural next question is: "How long does the content stay in the cache?" By default, the CDN uses a generic caching policy. In our next lesson, we will dive deeper and learn how to configure caching rules and Time-to-Live (TTL) settings to gain fine-grained control over your CDN's cache behavior.