1. URL redirection and rewriting for a custom domain in Azure Front Door

    TypeScript

    To set up URL redirection and rewriting for a custom domain in Azure Front Door, you will need to configure a few key resources:

    1. Front Door: This is the entry point for your application. You'll create a Front Door resource and configure it with routing rules for redirection and rewriting.
    2. Frontend Endpoint: Your custom domain will be associated with a frontend endpoint on Azure Front Door.
    3. Routing Rules: These define how requests to your Front Door are handled and include actions for redirection and rewriting URLs.

    Here is a program that sets up an Azure Front Door with a custom domain, a single backend, and a routing rule that includes URL redirection and rewriting:

    import * as pulumi from "@pulumi/pulumi"; import * as azure_native from "@pulumi/azure-native"; // Main Front Door resource const frontDoor = new azure_native.network.FrontDoor("myFrontDoor", { // The resource group name should be updated with your specific resource group. resourceGroupName: "myResourceGroupName", // Other Front Door configurations can be added here as needed. }); // Create a frontend endpoint for your custom domain on Azure Front Door const frontendEndpoint = new azure_native.network.FrontendEndpoint("myFrontEndEndpoint", { frontDoorName: frontDoor.name, hostName: "www.customdomain.com", // Replace with your custom domain resourceGroupName: "myResourceGroupName", // More frontend endpoint settings can be configured here }); // Backend pool for Azure Front Door indicating where traffic should be sent const backendPool = new azure_native.network.BackendPool("myBackendPool", { backends: [ { // The address of your backend service, update it accordingly address: "backend.example.com", httpPort: 80, httpsPort: 443, // Backend-specific settings can be adjusted here }, ], // Reference to the Front Door frontDoorName: frontDoor.name, loadBalancingSettings: { // Update with your load balancing settings id: "sampleLoadBalancingSettingsId", }, healthProbeSettings: { // Update with your health probe settings id: "sampleHealthProbeSettingsId", }, resourceGroupName: "myResourceGroupName", }); // Routing rules for Azure Front Door const routingRule = new azure_native.network.RoutingRule("myRoutingRule", { frontDoorName: frontDoor.name, frontendEndpoints: [{ id: frontendEndpoint.id, // The frontend endpoint for the custom domain }], resourceGroupName: "myResourceGroupName", routeConfiguration: { // URL redirection or rewriting configuration goes here // For example, we're rewriting the paths to replace `/api/*` with `/newapi/*` rewriteRules: [{ name: "RewriteApiPath", sourcePattern: "/api/*", destinationPattern: "/newapi/{R:0}", }], // Create a URL redirection action redirectConfiguration: { redirectProtocol: azure_native.network.RedirectProtocol.HttpsOnly, redirectType: azure_native.network.RedirectType.Found, customHost: "www.redirecteddomain.com", // The host to redirect to // More redirect configurations can be set here }, }, // Rule-specific settings can be adjusted here }); // Export the Front Door URL so you can easily access it export const frontDoorUrl = pulumi.interpolate`https://${frontendEndpoint.hostName}`;

    This program creates a new Azure Front Door, a frontend endpoint to attach your custom domain, a backend pool that represents your actual service, and a routing rule that directs traffic coming into the Front Door to your backend. The routing rule also specifies how URL paths are rewritten and where traffic is redirected if necessary.

    Make sure you replace placeholder values like "myResourceGroupName", "www.customdomain.com", "backend.example.com", and various ids in loadBalancingSettings and healthProbeSettings with actual values that correspond to your setup.

    Once deployed, traffic to www.customdomain.com will be processed by Azure Front Door, which then routes it according to the defined rules. The rewrite rule example changes paths starting with /api/ to /newapi/, and the redirect configuration redirects all HTTP traffic to HTTPS, with the redirection host being www.redirecteddomain.com.

    You will also need to configure DNS for your custom domain to point to the Azure Front Door's frontend host. This usually involves creating a CNAME record in your DNS settings that points your custom domain to the URL of the Azure Front Door frontend endpoint.

    Remember to replace placeholder values with actual values that correspond to your setup and ensure that your resource group and other dependent resources are properly set up before deploying this Pulumi program. After deploying, you can access the exported URL from the stack output to see your Front Door in action.