AWS::CloudFront::Distribution CacheBehavior
A complex type that describes how CloudFront processes requests.
You must create at least as many cache behaviors (including the default cache behavior) as you have origins if you want CloudFront to serve objects from all of the origins. Each cache behavior specifies the one origin from which you want CloudFront to get objects. If you have two origins and only the default cache behavior, the default cache behavior will cause CloudFront to get objects from one of the origins, but the other origin is never used.
For the current quota (formerly known as limit) on the number of cache behaviors that you can add to a distribution, see Quotas in the Amazon CloudFront Developer Guide.
If you don't want to specify any cache behaviors, include only an empty
CacheBehaviors
element. Don't specify an empty individual
CacheBehavior
element, because this is invalid. For more information,
see CacheBehaviors.
To delete all cache behaviors in an existing distribution, update the distribution
configuration and include only an empty CacheBehaviors
element.
To add, change, or remove one or more cache behaviors, update the distribution configuration and specify all of the cache behaviors that you want to include in the updated distribution.
For more information about cache behaviors, see Cache Behavior Settings in the Amazon CloudFront Developer Guide.
Syntax
To declare this entity in your AWS CloudFormation template, use the following syntax:
JSON
{ "AllowedMethods" :
[ String, ... ]
, "CachedMethods" :[ String, ... ]
, "CachePolicyId" :String
, "Compress" :Boolean
, "DefaultTTL" :Number
, "FieldLevelEncryptionId" :String
, "ForwardedValues" :ForwardedValues
, "FunctionAssociations" :[ FunctionAssociation, ... ]
, "LambdaFunctionAssociations" :[ LambdaFunctionAssociation, ... ]
, "MaxTTL" :Number
, "MinTTL" :Number
, "OriginRequestPolicyId" :String
, "PathPattern" :String
, "RealtimeLogConfigArn" :String
, "ResponseHeadersPolicyId" :String
, "SmoothStreaming" :Boolean
, "TargetOriginId" :String
, "TrustedKeyGroups" :[ String, ... ]
, "TrustedSigners" :[ String, ... ]
, "ViewerProtocolPolicy" :String
}
YAML
AllowedMethods:
- String
CachedMethods:- String
CachePolicyId:String
Compress:Boolean
DefaultTTL:Number
FieldLevelEncryptionId:String
ForwardedValues:ForwardedValues
FunctionAssociations:- FunctionAssociation
LambdaFunctionAssociations:- LambdaFunctionAssociation
MaxTTL:Number
MinTTL:Number
OriginRequestPolicyId:String
PathPattern:String
RealtimeLogConfigArn:String
ResponseHeadersPolicyId:String
SmoothStreaming:Boolean
TargetOriginId:String
TrustedKeyGroups:- String
TrustedSigners:- String
ViewerProtocolPolicy:String
Properties
AllowedMethods
-
A complex type that controls which HTTP methods CloudFront processes and forwards to your Amazon S3 bucket or your custom origin. There are three choices:
-
CloudFront forwards only
GET
andHEAD
requests. -
CloudFront forwards only
GET
,HEAD
, andOPTIONS
requests. -
CloudFront forwards
GET, HEAD, OPTIONS, PUT, PATCH, POST
, andDELETE
requests.
If you pick the third choice, you may need to restrict access to your Amazon S3 bucket or to your custom origin so users can't perform operations that you don't want them to. For example, you might not want users to have permissions to delete objects from your origin.
Required: No
Type: Array of String
Update requires: No interruption
-
CachedMethods
-
A complex type that controls whether CloudFront caches the response to requests using the specified HTTP methods. There are two choices:
-
CloudFront caches responses to
GET
andHEAD
requests. -
CloudFront caches responses to
GET
,HEAD
, andOPTIONS
requests.
If you pick the second choice for your Amazon S3 Origin, you may need to forward Access-Control-Request-Method, Access-Control-Request-Headers, and Origin headers for the responses to be cached correctly.
Required: No
Type: Array of String
Update requires: No interruption
-
CachePolicyId
-
The unique identifier of the cache policy that is attached to this cache behavior. For more information, see Creating cache policies or Using the managed cache policies in the Amazon CloudFront Developer Guide.
A
CacheBehavior
must include either aCachePolicyId
orForwardedValues
. We recommend that you use aCachePolicyId
.Required: Conditional
Type: String
Update requires: No interruption
Compress
-
Whether you want CloudFront to automatically compress certain files for this cache behavior. If so, specify true; if not, specify false. For more information, see Serving Compressed Files in the Amazon CloudFront Developer Guide.
Required: No
Type: Boolean
Update requires: No interruption
DefaultTTL
-
This field is deprecated. We recommend that you use the
DefaultTTL
field in a cache policy instead of this field. For more information, see Creating cache policies or Using the managed cache policies in the Amazon CloudFront Developer Guide.The default amount of time that you want objects to stay in CloudFront caches before CloudFront forwards another request to your origin to determine whether the object has been updated. The value that you specify applies only when your origin does not add HTTP headers such as
Cache-Control max-age
,Cache-Control s-maxage
, andExpires
to objects. For more information, see Managing How Long Content Stays in an Edge Cache (Expiration) in the Amazon CloudFront Developer Guide.Required: No
Type: Number
Update requires: No interruption
FieldLevelEncryptionId
-
The value of
ID
for the field-level encryption configuration that you want CloudFront to use for encrypting specific fields of data for this cache behavior.Required: No
Type: String
Update requires: No interruption
ForwardedValues
-
This field is deprecated. We recommend that you use a cache policy or an origin request policy instead of this field. For more information, see Working with policies in the Amazon CloudFront Developer Guide.
If you want to include values in the cache key, use a cache policy. For more information, see Creating cache policies or Using the managed cache policies in the Amazon CloudFront Developer Guide.
If you want to send values to the origin but not include them in the cache key, use an origin request policy. For more information, see Creating origin request policies or Using the managed origin request policies in the Amazon CloudFront Developer Guide.
A
CacheBehavior
must include either aCachePolicyId
orForwardedValues
. We recommend that you use aCachePolicyId
.A complex type that specifies how CloudFront handles query strings, cookies, and HTTP headers.
Required: Conditional
Type: ForwardedValues
Update requires: No interruption
FunctionAssociations
-
A list of CloudFront functions that are associated with this cache behavior. CloudFront functions must be published to the
LIVE
stage to associate them with a cache behavior.Required: No
Type: Array of FunctionAssociation
Update requires: No interruption
LambdaFunctionAssociations
-
A complex type that contains zero or more Lambda@Edge function associations for a cache behavior.
Required: No
Type: Array of LambdaFunctionAssociation
Update requires: No interruption
MaxTTL
-
This field is deprecated. We recommend that you use the
MaxTTL
field in a cache policy instead of this field. For more information, see Creating cache policies or Using the managed cache policies in the Amazon CloudFront Developer Guide.The maximum amount of time that you want objects to stay in CloudFront caches before CloudFront forwards another request to your origin to determine whether the object has been updated. The value that you specify applies only when your origin adds HTTP headers such as
Cache-Control max-age
,Cache-Control s-maxage
, andExpires
to objects. For more information, see Managing How Long Content Stays in an Edge Cache (Expiration) in the Amazon CloudFront Developer Guide.Required: No
Type: Number
Update requires: No interruption
MinTTL
-
This field is deprecated. We recommend that you use the
MinTTL
field in a cache policy instead of this field. For more information, see Creating cache policies or Using the managed cache policies in the Amazon CloudFront Developer Guide.The minimum amount of time that you want objects to stay in CloudFront caches before CloudFront forwards another request to your origin to determine whether the object has been updated. For more information, see Managing How Long Content Stays in an Edge Cache (Expiration) in the Amazon CloudFront Developer Guide.
You must specify
0
forMinTTL
if you configure CloudFront to forward all headers to your origin (underHeaders
, if you specify1
forQuantity
and*
forName
).Required: No
Type: Number
Update requires: No interruption
OriginRequestPolicyId
-
The unique identifier of the origin request policy that is attached to this cache behavior. For more information, see Creating origin request policies or Using the managed origin request policies in the Amazon CloudFront Developer Guide.
Required: No
Type: String
Update requires: No interruption
PathPattern
-
The pattern (for example,
images/*.jpg
) that specifies which requests to apply the behavior to. When CloudFront receives a viewer request, the requested path is compared with path patterns in the order in which cache behaviors are listed in the distribution.Note
You can optionally include a slash (
/
) at the beginning of the path pattern. For example,/images/*.jpg
. CloudFront behavior is the same with or without the leading/
.The path pattern for the default cache behavior is
*
and cannot be changed. If the request for an object does not match the path pattern for any cache behaviors, CloudFront applies the behavior in the default cache behavior.For more information, see Path Pattern in the Amazon CloudFront Developer Guide.
Required: Yes
Type: String
Update requires: No interruption
RealtimeLogConfigArn
-
The Amazon Resource Name (ARN) of the real-time log configuration that is attached to this cache behavior. For more information, see Real-time logs in the Amazon CloudFront Developer Guide.
Required: No
Type: String
Update requires: No interruption
ResponseHeadersPolicyId
-
The identifier for a response headers policy.
Required: No
Type: String
Update requires: No interruption
SmoothStreaming
-
Indicates whether you want to distribute media files in the Microsoft Smooth Streaming format using the origin that is associated with this cache behavior. If so, specify
true
; if not, specifyfalse
. If you specifytrue
forSmoothStreaming
, you can still distribute other content using this cache behavior if the content matches the value ofPathPattern
.Required: No
Type: Boolean
Update requires: No interruption
TargetOriginId
-
The value of
ID
for the origin that you want CloudFront to route requests to when they match this cache behavior.Required: Yes
Type: String
Update requires: No interruption
TrustedKeyGroups
-
A list of key groups that CloudFront can use to validate signed URLs or signed cookies.
When a cache behavior contains trusted key groups, CloudFront requires signed URLs or signed cookies for all requests that match the cache behavior. The URLs or cookies must be signed with a private key whose corresponding public key is in the key group. The signed URL or cookie contains information about which public key CloudFront should use to verify the signature. For more information, see Serving private content in the Amazon CloudFront Developer Guide.
Required: No
Type: Array of String
Update requires: No interruption
TrustedSigners
-
Important
We recommend using
TrustedKeyGroups
instead ofTrustedSigners
.A list of AWS account IDs whose public keys CloudFront can use to validate signed URLs or signed cookies.
When a cache behavior contains trusted signers, CloudFront requires signed URLs or signed cookies for all requests that match the cache behavior. The URLs or cookies must be signed with the private key of a CloudFront key pair in the trusted signer's AWS account. The signed URL or cookie contains information about which public key CloudFront should use to verify the signature. For more information, see Serving private content in the Amazon CloudFront Developer Guide.
Required: No
Type: Array of String
Update requires: No interruption
ViewerProtocolPolicy
-
The protocol that viewers can use to access the files in the origin specified by
TargetOriginId
when a request matches the path pattern inPathPattern
. You can specify the following options:-
allow-all
: Viewers can use HTTP or HTTPS. -
redirect-to-https
: If a viewer submits an HTTP request, CloudFront returns an HTTP status code of 301 (Moved Permanently) to the viewer along with the HTTPS URL. The viewer then resubmits the request using the new URL. -
https-only
: If a viewer sends an HTTP request, CloudFront returns an HTTP status code of 403 (Forbidden).
For more information about requiring the HTTPS protocol, see Requiring HTTPS Between Viewers and CloudFront in the Amazon CloudFront Developer Guide.
Note
The only way to guarantee that viewers retrieve an object that was fetched from the origin using HTTPS is never to use any other protocol to fetch the object. If you have recently changed from HTTP to HTTPS, we recommend that you clear your objects' cache because cached objects are protocol agnostic. That means that an edge location will return an object from the cache regardless of whether the current request protocol matches the protocol used previously. For more information, see Managing Cache Expiration in the Amazon CloudFront Developer Guide.
Required: Yes
Type: String
Allowed values:
allow-all | https-only | redirect-to-https
Update requires: No interruption
-
See also
-
CacheBehavior in the Amazon CloudFront API Reference