1. Url rewrite and redirection using Azure CDN rules

    TypeScript

    In this guide, we'll be using Pulumi to set up URL rewrite and redirection rules using Azure Content Delivery Network (CDN). CDN rules can improve the performance and reliability of your web application by strategically distributing content to users and optionally modifying the requests and responses as they pass through the CDN.

    We'll be using Azure CDN standard rules engine to create a rule that will redirect or rewrite URLs to your desired format. For instance, you can redirect users from an old URL to a new one or ensure that all requests use HTTPS. Azure CDN Standard from Microsoft provides a rules engine that allows you to customize how HTTP requests are handled.

    Before we dive into the Pulumi program, here’s a brief overview of the resources we will create:

    1. CDN Profile: This is a container for CDN endpoints and defines a set of CDN capabilities and behaviors.
    2. CDN Endpoint: It represents the edge servers where your content is cached and delivered to users.
    3. CDN Rule: The specific rule we will define for URL rewrite or redirection.

    Here’s a Pulumi TypeScript program that creates these resources and applies a simple URL redirection rule:

    import * as pulumi from '@pulumi/pulumi'; import * as azure_native from '@pulumi/azure-native'; // Define the resource group in which all resources will be created. const resourceGroup = new azure_native.resources.ResourceGroup('resourceGroup'); // Create a CDN profile. const cdnProfile = new azure_native.cdn.Profile('cdnProfile', { resourceGroupName: resourceGroup.name, location: 'Global', sku: { name: 'Standard_Microsoft' } }); // Create a CDN endpoint linked to the CDN profile. const cdnEndpoint = new azure_native.cdn.Endpoint('cdnEndpoint', { profileName: cdnProfile.name, resourceGroupName: resourceGroup.name, isHttpAllowed: false, // We want to enforce HTTPS isHttpsAllowed: true, contentTypesToCompress: [ 'text/plain', 'text/html', 'text/css', 'text/javascript', 'application/x-javascript', 'application/javascript', 'application/json', 'application/xml', ], originHostHeader: 'www.example.com', // Replace with your origin host }); // Create a rule set for the CDN profile. const ruleSet = new azure_native.cdn.RuleSet('ruleSet', { profileName: cdnProfile.name, resourceGroupName: resourceGroup.name, }); // Create a URL redirect rule. This rule simply redirects HTTP to HTTPS. const urlRedirectRule = new azure_native.cdn.Rule('urlRedirectRule', { ruleSet: { name: ruleSet.name, profileName: cdnProfile.name, resourceGroupName: resourceGroup.name }, order: 1, // Execution order for the rule conditions: [{ name: 'matchHttpRequestScheme', parameters: { odataType: '#Microsoft.Azure.Cdn.Models.DeliveryRuleRequestSchemeConditionParameters', matchValues: ['HTTP'], operator: 'Equal' } }], actions: [{ name: 'UrlRedirect', parameters: { odataType: '#Microsoft.Azure.Cdn.Models.DeliveryRuleUrlRedirectActionParameters', redirectType: 'Found', protocol: 'Https' } }] }); // Output the CDN endpoint hostname. export const cdnEndpointHostname = cdnEndpoint.hostName;

    In the above program, you can see that:

    • We’ve created a new resource group named 'resourceGroup'.
    • A CDN profile 'cdnProfile' is created in the Standard_Microsoft SKU.
    • An endpoint 'cdnEndpoint' is added to the CDN profile, with HTTPS enabled.
    • The rule set 'ruleSet' defines a collection of rules that apply to the CDN endpoint.
    • A URL redirect rule 'urlRedirectRule' is created that redirects all HTTP requests to HTTPS.

    In order to make any specific URL rewrite or redirection, you'd modify the urlRedirectRule block with your specific match conditions and actions, following the structure and options provided by Azure CDN.

    Once you’ve created these resources using Pulumi, the CDN will take care of caching your content and applying the rules you have specified for requests and responses.

    To run this program:

    1. Save the above code as index.ts.
    2. Install the required dependencies using npm:
      npm install @pulumi/pulumi @pulumi/azure-native
    3. Create a new Pulumi stack:
      pulumi stack init dev
    4. Log in to your Pulumi account:
      pulumi login
    5. Finally, run pulumi up to deploy your changes.

    By executing pulumi up, the Pulumi CLI will provision the Azure resources as defined in the TypeScript program. You can then observe the output, which will include the hostname of the CDN endpoint that you just created.