Skip to content
v2.276.0

CacheCookieBehavior Class ​

Determines whether any cookies in viewer requests are included in the cache key and automatically included in requests that CloudFront sends to the origin.

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 ​

behaviorstringbehaviorstrbehaviorStringBehaviorstringBehavior*string#
The behavior of cookies: allow all, none, an allow list, or a deny list.
cookiesmay be undefinedstring[]cookiesmay be undefinedtyping.List[str]cookiesmay be undefinedList<String>Cookiesmay be undefinedstring[]Cookiesmay be undefined*[]*string#
The cookies to allow or deny, if the behavior is an allow or deny list.

Methods ​

all(): CacheCookieBehavior // chainable@staticmethod def all() -> CacheCookieBehaviorpublic static CacheCookieBehavior all()public static CacheCookieBehavior All()func All() CacheCookieBehaviorstatic#
All cookies in viewer requests are included in the cache key and are automatically included in requests that CloudFront sends to the origin.
allowList(...cookies: string[]): CacheCookieBehavior // chainable@staticmethod def allow_list(cookies: str) -> CacheCookieBehaviorpublic static CacheCookieBehavior allowList(String cookies)public static CacheCookieBehavior AllowList(string cookies)func AllowList(Cookies *string) CacheCookieBehaviorstatic#
Only the provided `cookies` are included in the cache key and automatically included in requests that CloudFront sends to the origin.
denyList(...cookies: string[]): CacheCookieBehavior // chainable@staticmethod def deny_list(cookies: str) -> CacheCookieBehaviorpublic static CacheCookieBehavior denyList(String cookies)public static CacheCookieBehavior DenyList(string cookies)func DenyList(Cookies *string) CacheCookieBehaviorstatic#
All cookies except the provided `cookies` are included in the cache key and automatically included in requests that CloudFront sends to the origin.
none(): CacheCookieBehavior // chainable@staticmethod def none() -> CacheCookieBehaviorpublic static CacheCookieBehavior none()public static CacheCookieBehavior None()func None() CacheCookieBehaviorstatic#
Cookies in viewer requests are not included in the cache key and are not automatically included in requests that CloudFront sends to the origin.