How to Use AWS CloudFront for Image Redirection to Default Images
Learn how to serve a default image using CloudFront if you get a 404 from your origin server.

Search for a command to run...
Learn how to serve a default image using CloudFront if you get a 404 from your origin server.

No comments yet. Be the first to comment.
Now You Can Use Amazon GuardDuty to Monitor EC2 Runtime

In today's digital market, when milliseconds can mean the difference between user experience and retention, optimizing content delivery is critical for every website or application. Fast and dependable content delivery around the world is made possib...

Simplify Lambda deployments and eliminate frustration with Chalice's seamless integration and user-friendly tools

Learn how to make your origins accessible only via CloudFront, make them more secure.

In this blog, we'll explore how to use AWS CloudFront to serve a default image when your origin returns a 404 error. By implementing a Lambda@Edge function, we can ensure that users always receive a placeholder image instead of a broken link.
When a requested image is missing, returning a 404 error can lead to a poor user experience. Redirecting to a default image ensures that your website remains visually consistent and user-friendly.
First, create a CloudFront distribution that points to your origin server where your images are hosted. Ensure that your distribution is properly configured to handle the necessary requests.
The core of this solution involves using a Lambda@Edge function triggered on the Origin Response event. This function checks the status code from the origin and redirects to a default image if a 404 error is detected.
Here’s a breakdown of the code:
def lambda_handler(event, context):
response = event['Records'][0]['cf']['response']
response_headers = response['headers']
if response['status'] == '404':
if 'transfer-encoding' in response_headers:
return {
'status': '302',
'headers': {
'location': [{
'key': 'Location',
'value': "DEFAULT-IMAGE-URL"
}],
'transfer-encoding': [{
'key': 'Transfer-Encoding',
'value': 'chunked'
}]
}
}
else:
return {
'status': '302',
'headers': {
'location': [{
'key': 'Location',
'value': "DEFAULT-IMAGE-URL"
}]
}
}
return response
Event Trigger: The function is triggered on the Origin Response event.
Status Check: It checks if the response status code is 404.
Transfer-Encoding Handling: If the Transfer-Encoding header is present, it includes it in the redirection response to prevent any CloudFront validation errors.
Redirection: If a 404 is detected, it redirects to the specified default image URL.
Create the Lambda Function: Go to the AWS Lambda console and create a new function.
Set Permissions: Ensure the function has the necessary permissions to execute.
Attach to CloudFront: In the CloudFront distribution settings, attach the Lambda@Edge function to the Origin Response event.
After deployment, test the configuration by requesting an image that doesn’t exist on your origin. You should be redirected to the default image.
Monitor your CloudFront logs to ensure that redirections occur as expected and optimize the Lambda function as necessary to handle additional scenarios.
By implementing a Lambda@Edge function with AWS CloudFront, you can efficiently handle missing images by redirecting to a default placeholder. This enhances the user experience and maintains the integrity of your website’s design.
Use Lambda@Edge: To customize content delivery behavior at the edge.
Handle 404 Errors Gracefully: Redirect users to a default image.
Maintain Transfer-Encoding: To prevent CloudFront validation errors.
By following these steps, you’ll ensure a seamless experience for your users, even when images are missing. Happy coding!