1. How to set pathPattern in CloudFront CDN?


    To set the pathPattern in CloudFront CDN, we would typically use the aws.cloudfront.Distribution resource from the AWS provider. This Distribution resource creates a new Amazon CloudFront distribution to route traffic to specified origins (like an S3 bucket).

    You can define multiple behaviors, each with their own pathPattern, that tell CloudFront how to route requests to different origins based on the URL path in the request.

    Here's a simple example program for setting the pathPattern in CloudFront CDN:

    import * as pulumi from "@pulumi/pulumi"; import * as aws from "@pulumi/aws"; const originId = "s3-website" // S3 bucket for static files const bucket = new aws.s3.Bucket("Bucket", { website: { indexDocument: "index.html" } }); // CloudFront distribution to serve static files let distributionArgs: aws.cloudfront.DistributionArgs = { enabled: true, isIpv6Enabled: true, comment: "Bucket distribution", defaultRootObject: "index.html", origins: [ { domainName: bucket.bucketRegionalDomainName, originId: originId, customOriginConfig: { originProtocolPolicy: "http-only", httpPort: 80, httpsPort: 443, originSslProtocols: ["TLSv1.2"], }, }, ], defaultCacheBehavior: { targetOriginId: originId, viewerProtocolPolicy: "redirect-to-https", allowedMethods: ["GET", "HEAD", "OPTIONS"], cachedMethods: ["GET", "HEAD", "OPTIONS"], forwardedValues: { queryString: false, cookies: { forward: "none" }, }, minTtl: 0, defaultTtl: 3600, maxTtl: 86400, }, priceClass: "PriceClass_100", restrictions: { geoRestriction: { restrictionType: "none" }, }, viewerCertificate: { cloudfrontDefaultCertificate: true, } }; // Adding the pathPattern to the CloudFront distribution distributionArgs["orderedCacheBehaviors"] = [ { pathPattern: "/content/*", allowedMethods: ["GET", "HEAD", "OPTIONS"], cachedMethods: ["GET", "HEAD"], targetOriginId: originId, forwardedValues: { queryString: false, headers: ["Origin"], cookies: { forward: "none"}, }, minTtl: 0, defaultTtl: 86400, maxTtl: 31536000, compress: true, viewerProtocolPolicy: "redirect-to-https", }, ]; const cdn = new aws.cloudfront.Distribution("cdnDistribution", distributionArgs); // Export the distribution details export const distributionId = cdn.id; export const distributionDomainName = cdn.domainName;

    In this program, we create an S3 bucket and a CloudFront distribution. We specify the pathPattern when defining the CloudFront distribution's orderedCacheBehaviors. This configuration section allows you to specify the path patterns for files that you want CloudFront to serve from your S3 bucket.

    If a URL matches the path pattern /content/*, then the requested file is served from the CloudFront cache. If the file isn't in the cache, CloudFront retrieves it from the S3 bucket and then caches it for future requests.

    Finally, we export the distribution's id and domain name for easy access later.