aws-cdk-lib.aws_cloudfront.CachePolicy

class CachePolicy (construct)

LanguageType name
.NETAmazon.CDK.AWS.CloudFront.CachePolicy
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awscloudfront#CachePolicy
Javasoftware.amazon.awscdk.services.cloudfront.CachePolicy
Pythonaws_cdk.aws_cloudfront.CachePolicy
TypeScript (source)aws-cdk-lib » aws_cloudfront » CachePolicy

Implements IConstruct, IDependable, IResource, ICachePolicy

A Cache Policy configuration.

Example

// Using an existing cache policy for a Distribution
declare const bucketOrigin: origins.S3Origin;
new cloudfront.Distribution(this, 'myDistManagedPolicy', {
  defaultBehavior: {
    origin: bucketOrigin,
    cachePolicy: cloudfront.CachePolicy.CACHING_OPTIMIZED,
  },
});

Initializer

new CachePolicy(scope: Construct, id: string, props?: CachePolicyProps)

Parameters

  • scope Construct
  • id string
  • props CachePolicyProps

Construct Props

NameTypeDescription
cachePolicyName?stringA unique name to identify the cache policy.
comment?stringA comment to describe the cache policy.
cookieBehavior?CacheCookieBehaviorDetermines whether any cookies in viewer requests are included in the cache key and automatically included in requests that CloudFront sends to the origin.
defaultTtl?DurationThe default amount of time for objects to stay in the CloudFront cache.
enableAcceptEncodingBrotli?booleanWhether to normalize and include the Accept-Encoding header in the cache key when the Accept-Encoding header is 'br'.
enableAcceptEncodingGzip?booleanWhether to normalize and include the Accept-Encoding header in the cache key when the Accept-Encoding header is 'gzip'.
headerBehavior?CacheHeaderBehaviorDetermines whether any HTTP headers are included in the cache key and automatically included in requests that CloudFront sends to the origin.
maxTtl?DurationThe maximum amount of time for objects to stay in the CloudFront cache.
minTtl?DurationThe minimum amount of time for objects to stay in the CloudFront cache.
queryStringBehavior?CacheQueryStringBehaviorDetermines whether any query strings are included in the cache key and automatically included in requests that CloudFront sends to the origin.

cachePolicyName?

Type: string (optional, default: generated from the id)

A unique name to identify the cache policy.

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


comment?

Type: string (optional, default: no comment)

A comment to describe the cache policy.


cookieBehavior?

Type: CacheCookieBehavior (optional, default: 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.


defaultTtl?

Type: Duration (optional, default: The greater of 1 day and minTtl)

The default amount of time for objects to stay in the CloudFront cache.

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


enableAcceptEncodingBrotli?

Type: boolean (optional, default: false)

Whether to normalize and include the Accept-Encoding header in the cache key when the Accept-Encoding header is 'br'.


enableAcceptEncodingGzip?

Type: boolean (optional, default: false)

Whether to normalize and include the Accept-Encoding header in the cache key when the Accept-Encoding header is 'gzip'.


headerBehavior?

Type: CacheHeaderBehavior (optional, default: CacheHeaderBehavior.none())

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


maxTtl?

Type: Duration (optional, default: The greater of 1 year and defaultTtl)

The maximum amount of time for objects to stay in the CloudFront cache.

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


minTtl?

Type: Duration (optional, default: Duration.seconds(0))

The minimum amount of time for objects to stay in the CloudFront cache.


queryStringBehavior?

Type: CacheQueryStringBehavior (optional, default: CacheQueryStringBehavior.none())

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

Properties

NameTypeDescription
cachePolicyIdstringThe ID of the cache policy.
envResourceEnvironmentThe environment this resource belongs to.
nodeNodeThe tree node.
stackStackThe stack in which this resource is defined.
static AMPLIFYICachePolicyThis policy is designed for use with an origin that is an AWS Amplify web app.
static CACHING_DISABLEDICachePolicyDisables caching.
static CACHING_OPTIMIZEDICachePolicyOptimize cache efficiency by minimizing the values that CloudFront includes in the cache key.
static CACHING_OPTIMIZED_FOR_UNCOMPRESSED_OBJECTSICachePolicyOptimize cache efficiency by minimizing the values that CloudFront includes in the cache key.
static ELEMENTAL_MEDIA_PACKAGEICachePolicyDesigned for use with an origin that is an AWS Elemental MediaPackage endpoint.

cachePolicyId

Type: string

The ID of the cache policy.


env

Type: ResourceEnvironment

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.


node

Type: Node

The tree node.


stack

Type: Stack

The stack in which this resource is defined.


static AMPLIFY

Type: ICachePolicy

This policy is designed for use with an origin that is an AWS Amplify web app.


static CACHING_DISABLED

Type: ICachePolicy

Disables caching.

This policy is useful for dynamic content and for requests that are not cacheable.


static CACHING_OPTIMIZED

Type: ICachePolicy

Optimize cache efficiency by minimizing the values that CloudFront includes in the cache key.

Query strings and cookies are not included in the cache key, and only the normalized 'Accept-Encoding' header is included.


static CACHING_OPTIMIZED_FOR_UNCOMPRESSED_OBJECTS

Type: ICachePolicy

Optimize cache efficiency by minimizing the values that CloudFront includes in the cache key.

Query strings and cookies are not included in the cache key, and only the normalized 'Accept-Encoding' header is included. Disables cache compression.


static ELEMENTAL_MEDIA_PACKAGE

Type: ICachePolicy

Designed for use with an origin that is an AWS Elemental MediaPackage endpoint.

Methods

NameDescription
applyRemovalPolicy(policy)Apply the given removal policy to this resource.
toString()Returns a string representation of this construct.
static fromCachePolicyId(scope, id, cachePolicyId)Imports a Cache Policy from its id.

applyRemovalPolicy(policy)

public applyRemovalPolicy(policy: RemovalPolicy): void

Parameters

  • policy RemovalPolicy

Apply the given removal policy to this resource.

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.


static fromCachePolicyId(scope, id, cachePolicyId)

public static fromCachePolicyId(scope: Construct, id: string, cachePolicyId: string): ICachePolicy

Parameters

  • scope Construct
  • id string
  • cachePolicyId string

Returns

  • ICachePolicy

Imports a Cache Policy from its id.