Cache Behaviour

Learn via video courses

Overview

You can make all the configurations in behaviours. By the person making the request or the length of time an object will be stored in the cache, you can use it to enforce policies, alter or vary the sort of material given, or both. The default behaviour is always present in CloudFront Distributions, which have one too many Behaviours. Different behaviours in AWS can be set up in a hierarchy of precedence, and the default behaviour in AWS will be used if none of the conditions are met.

Introduction to CacheBehaviors

Each cache action identifies the single origin CloudFront should use to obtain things. If you only utilize the default CacheBehavior in AWS and you have two origins, CloudFront will only ever use one of the origins to acquire items when you do this.

Only a CacheBehaviors element that is empty should be present if you don't wish to provide any CacheBehaviors. Include a CacheBehavior element, but do not leave it empty.

cache-behaviour1

Update the distribution settings and add simply an empty CacheBehaviors element if you want to remove all CacheBehaviors from an existing distribution.

Update the distribution settings and list every cache behaviour in AWS you wish to be included in the updated distribution to add, modify, or remove more than one CacheBehaviors in AWS.

Syntax

Use the following syntax in your AWS CloudFormation template to declare this entity:

JSON

YAML

Properties of CacheBehaviors

AllowedMethods

  • A complex type that manages which HTTP methods CloudFront handles and forwards to your custom origin or Amazon S3 bucket. Three options are available:
    • Only GET and HEAD requests are forwarded by CloudFront.
    • Only GET, HEAD, and OPTIONS requests are forwarded by CloudFront.
    • HEAD, GET, PUT, PATCH, OPTIONS, POST, and DELETE requests are forwarded via CloudFront.
  • If you choose option three, you might need to limit user access to your custom origin or Amazon S3 bucket to prevent users from carrying out actions that you don't want them to. You might not want people to be able to remove objects from your origin, for instance.
  • Specifications:
    • Required: No
    • Type: List of String

CachedMethods

  • A complex type that regulates whether CloudFront stores responses to requests made using the designated HTTP methods in its cache. There are two options:
    • The GET and HEAD responses are cached by CloudFront.
    • GET, HEAD, and OPTIONS replies are cached by CloudFront.
  • Specifications:
    • Required: No
    • Type: List of String

CachePolicyId

  • The unique code for the cache policy associated with this cache behavior. A CachePolicyId or ForwardedValues must be included in a CacheBehavior. It is advised to make use of a CachePolicyId.
  • Specifications:
    • Required: Conditional
    • Type: String

Compress

  • You can choose whether or not CloudFront should automatically compress specific files for this caching behavior. If so, enter true; otherwise, enter false.
  • Specifications:
    • Required: No
    • Type: Boolean

DefaultTTL

  • This field has become outdated. Instead of using this field in a cache policy, it is advised that you use the DefaultTTL field.
  • The period by default that you want things to remain in CloudFront caches before CloudFront sends a new request to your origin to check if the object has been updated. Only when your origin does not add HTTP headers like Cache-Control max-age, Cache-Control s-maxage, and Expires to objects does the value you specify take effect.
  • Specifications:
    • Required: No
    • Type: Double

cache-behaviour2

FieldLevelEncryptionId

  • The ID value for the field-level encryption setting that CloudFront should employ to encrypt particular fields of data for this cache activity.
  • Specifications:
    • Required: No
    • Type: String

ForwardedValues

  • This field has become outdated. Instead of using this field, it is advised to use a cache policy or an origin request policy.
  • Specifications:
    • Required: Conditional
    • Type: ForwardedValues

FunctionAssociations

  • A list of the CloudFront operations connected to this caching behavior. To link CloudFront functions with a CacheBehavior, they must be published to the LIVE stage.
  • Specifications:
    • Required: No
    • Type: List of FunctionAssociation

LambdaFunctionAssociations

  • A complex type with one or more relationships for LambdaEdge functions for a CacheBehavior.
  • Specifications:
    • Required: No
    • Type: List of LambdaFunctionAssociation

OriginRequestPolicyId

  • The unique code that represents the origin request policy that is associated with this cache activity.
  • Specifications:
    • Required: No
    • Type: String

PathPattern

  • The pattern that designates which requests should be subject to the action (for instance, test/*.jpg). The requested path is compared against path patterns in the order that CacheBehavior in AWS is mentioned in the distribution when CloudFront gets a viewer request.
  • Specifications:
    • Required: Yes
    • Type: String

RealtimeLogConfigArn

  • The real-time log setting that is associated with this CacheBehavior's Amazon Resource Name (ARN).
  • Specifications:
    • Required: No
    • Type: String

ResponseHeadersPolicyId

  • The response headers policy identifier.
  • Specifications:
    • Required: No
    • Type: String

SmoothStreaming

  • Specifies whether you wish to use the origin connected to this CacheBehavior in AWS to distribute media files in the Microsoft Smooth Streaming format. If so, enter true; otherwise, enter false. If you set SmoothStreaming to true, you can still use this CacheBehavior in AWS to distribute additional content as long as it fits PathPattern's value.
  • Specifications:
    • Required: No
    • Type: Boolean

TargetOriginId

  • The origin's ID value for which requests should be forwarded by CloudFront when they match this CacheBehavior.
  • Specifications:
    • Required: Yes
    • Type: String

TrustedKeyGroups

  • A list of important groups that CloudFront can use to confirm the authenticity of signed cookies or URLs.
  • Specifications:
    • Required: No
    • Type: List of String

ViewerProtocolPolicy

  • The protocol that users can access when a request matches the path pattern in PathPattern to access the files in the origin supplied by TargetOriginId. You have the below-mentioned choices:
    • allow-all - HTTP or HTTPS can be used by viewers.
    • redirect-to-https - When a viewer sends an HTTP request, CloudFront replies with the HTTPS URL and the HTTP status code 301 (Moved Permanently). The viewer then tries again with the updated URL.
    • https-only - In response to an HTTP request from a viewer, CloudFront provides a 403 status code (Forbidden).
  • Specifications:
    • Required: Yes
    • Type: String

Conclusion

  • You can make all the configurations in behaviors. By the person making the request or the length of time an object will be stored in a cache, you can use it to enforce policies, alter or vary the sort of material given, or both.
  • Update the distribution settings and add simply an empty CacheBehaviors element if you want to remove all CacheBehaviors from an existing distribution.
  • You can have several different behaviors, and if none of them take precedence and a condition isn't met in any of them, the default behavior will be used.
  • For CloudFront to serve objects from all origins, you must create at least as many CacheBehaviors in AWS (including the default CacheBehavior in AWS). The one origin from which you want CloudFront to obtain things is specified in each CacheBehavior in AWS.