AWS CloudFront and S3: Understanding the 307 Status Code
AWS CloudFront and S3 are two of the most popular services offered by Amazon Web Services. Amazon S3 provides scalable object storage, while CloudFront is a content delivery network (CDN) that helps deliver content with low latency and high transfer speeds. However, users may encounter a 307 status code when using these services together. In this blog post, we'll explore what the 307 status code means, why it occurs in the context of CloudFront and S3, typical usage scenarios, common practices, and best practices to handle it effectively.
Table of Contents#
- Core Concepts
- What is a 307 Status Code?
- How CloudFront and S3 Interact
- Typical Usage Scenarios
- Redirects in S3 Buckets
- CDN Caching and Redirects
- Common Practices
- Analyzing the Problem
- Troubleshooting Steps
- Best Practices
- Configuring CloudFront and S3 Correctly
- Handling Redirects Efficiently
- Conclusion
- FAQ
- References
Article#
Core Concepts#
What is a 307 Status Code?#
A 307 status code is an HTTP status code that indicates a temporary redirect. When a client sends a request to a server, and the server responds with a 307 status code, it means that the requested resource is temporarily located at a different URL. The client should resend the same request to the new URL provided in the Location header of the response.
How CloudFront and S3 Interact#
CloudFront can be configured to distribute content from an S3 bucket. When a user requests an object from a CloudFront distribution that is linked to an S3 bucket, CloudFront first checks its edge caches. If the object is not cached, CloudFront fetches the object from the S3 bucket and caches it for future requests.
However, if the S3 bucket has redirect rules configured, the S3 bucket may return a 307 status code to CloudFront, which then passes it on to the client.
Typical Usage Scenarios#
Redirects in S3 Buckets#
S3 buckets can be configured with redirect rules. For example, you might want to redirect all requests for a specific prefix to another location within the same bucket or to an external URL. When CloudFront tries to fetch an object from the S3 bucket and the S3 bucket has a redirect rule for that object, it will return a 307 status code.
<RoutingRules>
<RoutingRule>
<Condition>
<KeyPrefixEquals>old-prefix/</KeyPrefixEquals>
</Condition>
<Redirect>
<ReplaceKeyPrefixWith>new-prefix/</ReplaceKeyPrefixWith>
</Redirect>
</RoutingRule>
</RoutingRules>CDN Caching and Redirects#
CloudFront caches content based on the request and response. When a 307 status code is returned from the S3 bucket, CloudFront may cache the redirect response. This can cause issues if the redirect is temporary and the client continues to receive the cached redirect instead of the actual content.
Common Practices#
Analyzing the Problem#
- Check S3 Bucket Configuration: Review the redirect rules in the S3 bucket to ensure they are correct. You can use the AWS Management Console, AWS CLI, or SDKs to view and modify these rules.
- Inspect CloudFront Logs: CloudFront logs can provide valuable information about the requests and responses. Look for requests that return a 307 status code and check the associated details such as the requested URL, the
Locationheader, and the cache behavior.
Troubleshooting Steps#
- Clear CloudFront Cache: If the redirect is cached in CloudFront, you can invalidate the cache for the specific objects or paths. This can be done through the AWS Management Console or the AWS CLI.
aws cloudfront create-invalidation --distribution-id DISTRIBUTION_ID --paths "/old-prefix/*"- Verify DNS Settings: Incorrect DNS settings can sometimes cause issues with CloudFront and S3 interactions. Make sure that the DNS records for your CloudFront distribution are correctly configured.
Best Practices#
Configuring CloudFront and S3 Correctly#
- Use Cache Behaviors Wisely: Configure CloudFront cache behaviors to control how it caches redirects. You can set up cache behaviors to cache or not cache specific status codes, including 307.
- Set Appropriate Time-to-Live (TTL): For redirects, set a short TTL in CloudFront to ensure that the cache is refreshed frequently and clients receive the most up-to-date redirect information.
Handling Redirects Efficiently#
- Minimize Redirects: Try to reduce the number of redirects in your S3 bucket configuration. Multiple redirects can slow down the user experience and increase the chances of caching issues.
- Use Permanent Redirects When Appropriate: If a redirect is permanent, consider using a 301 status code instead of a 307. CloudFront and browsers handle permanent redirects differently, and this can help improve caching and performance.
Conclusion#
The 307 status code in the context of AWS CloudFront and S3 can be caused by redirect rules in S3 buckets. By understanding the core concepts, typical usage scenarios, and following common and best practices, software engineers can effectively troubleshoot and manage these redirects. This ensures a smooth user experience and optimal performance of their applications using CloudFront and S3.
FAQ#
- Why is my CloudFront distribution returning a 307 status code?
- It is likely due to redirect rules configured in the S3 bucket linked to your CloudFront distribution. Check the S3 bucket configuration to verify the redirect rules.
- How can I clear the CloudFront cache for a redirect?
- You can use the AWS Management Console or the AWS CLI to create an invalidation for the specific objects or paths associated with the redirect.
- Should I use a 301 or 307 status code for redirects?
- Use a 301 status code for permanent redirects and a 307 status code for temporary redirects. Consider the caching implications of each when configuring your S3 bucket and CloudFront distribution.