Skip to content
v2.277.0

CachePolicyProps Struct ​

Properties for creating a Cache Policy.

Example ​

ts
// Creating a custom cache policy for a Distribution -- all parameters optional
declare const bucketOrigin: origins.S3Origin;
const myCachePolicy = new cloudfront.CachePolicy(this, 'myCachePolicy', {
  cachePolicyName: 'MyPolicy',
  comment: 'A default policy',
  defaultTtl: Duration.days(2),
  minTtl: Duration.minutes(1),
  maxTtl: Duration.days(10),
  cookieBehavior: cloudfront.CacheCookieBehavior.all(),
  headerBehavior: cloudfront.CacheHeaderBehavior.allowList('X-CustomHeader'),
  queryStringBehavior: cloudfront.CacheQueryStringBehavior.denyList('username'),
  enableAcceptEncodingGzip: true,
  enableAcceptEncodingBrotli: true,
});
new cloudfront.Distribution(this, 'myDistCustomPolicy', {
  defaultBehavior: {
    origin: bucketOrigin,
    cachePolicy: myCachePolicy,
  },
});
python
# Creating a custom cache policy for a Distribution -- all parameters optional
# bucket_origin: origins.S3Origin

my_cache_policy = cloudfront.CachePolicy(self, "myCachePolicy",
    cache_policy_name="MyPolicy",
    comment="A default policy",
    default_ttl=Duration.days(2),
    min_ttl=Duration.minutes(1),
    max_ttl=Duration.days(10),
    cookie_behavior=cloudfront.CacheCookieBehavior.all(),
    header_behavior=cloudfront.CacheHeaderBehavior.allow_list("X-CustomHeader"),
    query_string_behavior=cloudfront.CacheQueryStringBehavior.deny_list("username"),
    enable_accept_encoding_gzip=True,
    enable_accept_encoding_brotli=True
)
cloudfront.Distribution(self, "myDistCustomPolicy",
    default_behavior=cloudfront.BehaviorOptions(
        origin=bucket_origin,
        cache_policy=my_cache_policy
    )
)
java
// Creating a custom cache policy for a Distribution -- all parameters optional
S3Origin bucketOrigin;

CachePolicy myCachePolicy = CachePolicy.Builder.create(this, "myCachePolicy")
        .cachePolicyName("MyPolicy")
        .comment("A default policy")
        .defaultTtl(Duration.days(2))
        .minTtl(Duration.minutes(1))
        .maxTtl(Duration.days(10))
        .cookieBehavior(CacheCookieBehavior.all())
        .headerBehavior(CacheHeaderBehavior.allowList("X-CustomHeader"))
        .queryStringBehavior(CacheQueryStringBehavior.denyList("username"))
        .enableAcceptEncodingGzip(true)
        .enableAcceptEncodingBrotli(true)
        .build();
Distribution.Builder.create(this, "myDistCustomPolicy")
        .defaultBehavior(BehaviorOptions.builder()
                .origin(bucketOrigin)
                .cachePolicy(myCachePolicy)
                .build())
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
// Creating a custom cache policy for a Distribution -- all parameters optional
S3Origin bucketOrigin;

var myCachePolicy = new CachePolicy(this, "myCachePolicy", new CachePolicyProps {
    CachePolicyName = "MyPolicy",
    Comment = "A default policy",
    DefaultTtl = Duration.Days(2),
    MinTtl = Duration.Minutes(1),
    MaxTtl = Duration.Days(10),
    CookieBehavior = CacheCookieBehavior.All(),
    HeaderBehavior = CacheHeaderBehavior.AllowList("X-CustomHeader"),
    QueryStringBehavior = CacheQueryStringBehavior.DenyList("username"),
    EnableAcceptEncodingGzip = true,
    EnableAcceptEncodingBrotli = true
});
new Distribution(this, "myDistCustomPolicy", new DistributionProps {
    DefaultBehavior = new BehaviorOptions {
        Origin = bucketOrigin,
        CachePolicy = myCachePolicy
    }
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
// Creating a custom cache policy for a Distribution -- all parameters optional
var bucketOrigin S3Origin

myCachePolicy := cloudfront.NewCachePolicy(this, jsii.String("myCachePolicy"), &CachePolicyProps{
	CachePolicyName: jsii.String("MyPolicy"),
	Comment: jsii.String("A default policy"),
	DefaultTtl: awscdk.Duration_Days(jsii.Number(2)),
	MinTtl: awscdk.Duration_Minutes(jsii.Number(1)),
	MaxTtl: awscdk.Duration_*Days(jsii.Number(10)),
	CookieBehavior: cloudfront.CacheCookieBehavior_All(),
	HeaderBehavior: cloudfront.CacheHeaderBehavior_AllowList(jsii.String("X-CustomHeader")),
	QueryStringBehavior: cloudfront.CacheQueryStringBehavior_DenyList(jsii.String("username")),
	EnableAcceptEncodingGzip: jsii.Boolean(true),
	EnableAcceptEncodingBrotli: jsii.Boolean(true),
})
cloudfront.NewDistribution(this, jsii.String("myDistCustomPolicy"), &DistributionProps{
	DefaultBehavior: &BehaviorOptions{
		Origin: bucketOrigin,
		CachePolicy: myCachePolicy,
	},
})

Properties ​

cachePolicyNameoptionalstringcache_policy_nameoptionalstrcachePolicyNameoptionalStringCachePolicyNameoptionalstringCachePolicyNameoptional*string#
A unique name to identify the cache policy.

Default: generated from the id

The name must only include '-', '_', or alphanumeric characters.

commentoptionalstringcommentoptionalstrcommentoptionalStringCommentoptionalstringCommentoptional*string#
A comment to describe the cache policy.

Default: no comment

The comment cannot be longer than 128 characters.

cookieBehavioroptionalCacheCookieBehaviorcookie_behavioroptionalCacheCookieBehaviorcookieBehavioroptionalCacheCookieBehaviorCookieBehavioroptionalCacheCookieBehaviorCookieBehavioroptionalCacheCookieBehavior= CacheCookieBehavior.none()#
Determines whether any cookies in viewer requests are included in the cache key and automatically included in requests that CloudFront sends to the origin.
defaultTtloptionalDurationdefault_ttloptionalDurationdefaultTtloptionalDurationDefaultTtloptionalDurationDefaultTtloptionalDuration#
The default amount of time for objects to stay in the CloudFront cache.

Default: The greater of 1 day and minTtl

Only used when the origin does not send Cache-Control or Expires headers with the object.

enableAcceptEncodingBrotlioptionalbooleanenable_accept_encoding_brotlioptionalboolenableAcceptEncodingBrotlioptionalBooleanEnableAcceptEncodingBrotlioptionalboolEnableAcceptEncodingBrotlioptional*bool= false#
Whether to normalize and include the `Accept-Encoding` header in the cache key when the `Accept-Encoding` header is 'br'.
enableAcceptEncodingGzipoptionalbooleanenable_accept_encoding_gzipoptionalboolenableAcceptEncodingGzipoptionalBooleanEnableAcceptEncodingGzipoptionalboolEnableAcceptEncodingGzipoptional*bool= false#
Whether to normalize and include the `Accept-Encoding` header in the cache key when the `Accept-Encoding` header is 'gzip'.
headerBehavioroptionalCacheHeaderBehaviorheader_behavioroptionalCacheHeaderBehaviorheaderBehavioroptionalCacheHeaderBehaviorHeaderBehavioroptionalCacheHeaderBehaviorHeaderBehavioroptionalCacheHeaderBehavior= CacheHeaderBehavior.none()#
Determines whether any HTTP headers are included in the cache key and automatically included in requests that CloudFront sends to the origin.
maxTtloptionalDurationmax_ttloptionalDurationmaxTtloptionalDurationMaxTtloptionalDurationMaxTtloptionalDuration#
The maximum amount of time for objects to stay in the CloudFront cache.

Default: The greater of 1 year and defaultTtl

CloudFront uses this value only when the origin sends Cache-Control or Expires headers with the object.

minTtloptionalDurationmin_ttloptionalDurationminTtloptionalDurationMinTtloptionalDurationMinTtloptionalDuration= Duration.seconds(0)#
The minimum amount of time for objects to stay in the CloudFront cache.
queryStringBehavioroptionalCacheQueryStringBehaviorquery_string_behavioroptionalCacheQueryStringBehaviorqueryStringBehavioroptionalCacheQueryStringBehaviorQueryStringBehavioroptionalCacheQueryStringBehaviorQueryStringBehavioroptionalCacheQueryStringBehavior= CacheQueryStringBehavior.none()#
Determines whether any query strings are included in the cache key and automatically included in requests that CloudFront sends to the origin.