0
0
AwsConceptBeginner · 3 min read

What is Behavior in CloudFront: Explanation and Example

In AWS CloudFront, a behavior defines how the service handles requests for specific URL paths. It controls settings like caching, allowed HTTP methods, and origin selection, shaping how content is delivered to users.
⚙️

How It Works

Think of a CloudFront distribution as a delivery service for your website or app content. A behavior is like a set of instructions for how to handle certain packages based on their labels. For example, you might want images to be cached longer than HTML pages, or allow only certain types of requests for some files.

Each behavior matches requests by URL path patterns, like "/images/*" or "/api/*". When a user requests content, CloudFront checks these behaviors in order to decide how to process the request. This includes which origin server to contact, what HTTP methods are allowed, and how long to keep the content cached.

This system lets you customize delivery for different parts of your site easily, improving performance and security without changing your backend.

💻

Example

This example shows a CloudFront distribution behavior that matches requests to "/images/*" and caches them for 24 hours, allowing only GET and HEAD requests.

json
{
  "CacheBehaviors": [
    {
      "PathPattern": "/images/*",
      "TargetOriginId": "myS3Origin",
      "ViewerProtocolPolicy": "redirect-to-https",
      "AllowedMethods": ["GET", "HEAD"],
      "CachedMethods": ["GET", "HEAD"],
      "DefaultTTL": 86400
    }
  ]
}
Output
Behavior created to cache /images/* for 86400 seconds with GET and HEAD allowed.
🎯

When to Use

Use behaviors when you want different rules for different parts of your website or app. For example:

  • Cache images longer than dynamic pages to save bandwidth.
  • Allow POST requests only on API paths but not on static content.
  • Route requests for videos to a specialized origin server.
  • Enforce HTTPS only on sensitive paths.

Behaviors help optimize performance, security, and cost by tailoring delivery settings to your content types.

Key Points

  • A behavior controls how CloudFront handles requests matching a URL pattern.
  • It defines caching, allowed HTTP methods, and which origin to use.
  • Multiple behaviors let you customize delivery for different content types.
  • Behaviors improve performance and security by applying specific rules.

Key Takeaways

A CloudFront behavior sets rules for handling requests to specific URL paths.
Behaviors control caching, HTTP methods, and origin selection for content delivery.
Use behaviors to optimize performance and security for different content types.
Multiple behaviors allow fine-grained control within a single CloudFront distribution.