How do I set pathPattern in CloudFront CDN?
To set the pathPattern
in an AWS CloudFront CDN using Pulumi, you need to define a CloudFront distribution and specify cache behaviors with different path patterns. This allows you to control how CloudFront caches and serves content based on the URL path.
Below is a TypeScript example showing how to create a CloudFront distribution with a custom path pattern:
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Create an S3 bucket to serve as the origin
const bucket = new aws.s3.Bucket("my-bucket");
// Create an Origin Access Identity to restrict access to the S3 bucket
const originAccessIdentity = new aws.cloudfront.OriginAccessIdentity("originAccessIdentity");
// Create a CloudFront distribution
const distribution = new aws.cloudfront.Distribution("myDistribution", {
enabled: true,
origins: [
{
domainName: bucket.bucketRegionalDomainName,
originId: bucket.arn,
s3OriginConfig: {
originAccessIdentity: originAccessIdentity.cloudfrontAccessIdentityPath,
},
},
],
defaultCacheBehavior: {
targetOriginId: bucket.arn,
viewerProtocolPolicy: "allow-all",
allowedMethods: ["GET", "HEAD"],
cachedMethods: ["GET", "HEAD"],
forwardedValues: {
queryString: false,
cookies: {
forward: "none",
},
},
},
orderedCacheBehaviors: [
{
pathPattern: "/images/*",
targetOriginId: bucket.arn,
viewerProtocolPolicy: "redirect-to-https",
allowedMethods: ["GET", "HEAD"],
cachedMethods: ["GET", "HEAD"],
forwardedValues: {
queryString: false,
cookies: {
forward: "none",
},
},
minTtl: 0,
defaultTtl: 3600,
maxTtl: 86400,
},
],
restrictions: {
geoRestriction: {
restrictionType: "none",
},
},
viewerCertificate: {
cloudfrontDefaultCertificate: true,
},
});
// Export the distribution domain name
export const cdnDomainName = distribution.domainName;
In this example:
- An S3 bucket is created to serve as the origin for the CloudFront distribution.
- An Origin Access Identity is created to restrict access to the S3 bucket.
- A CloudFront distribution is created with a default cache behavior and an ordered cache behavior.
- The
pathPattern
is set to/images/*
for the ordered cache behavior, which specifies that requests matching this pattern will use this cache behavior configuration. - The default cache behavior is used for all other requests.
The cdnDomainName
is exported to provide the domain name of the CloudFront distribution.
In summary, this code sets up a CloudFront distribution with a specific path pattern that directs how requests for certain paths are handled.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.