1 Answer
- Newest
- Most votes
- Most comments
0
Your approach to routing traffic based on URL to different S3 bucket origins using Lambda@Edge is fundamentally sound, but there are a few key points and potential issues to address to ensure it works as expected.
-
Replace the Referer header check with a host header check to determine the requested domain directly.
Adjusted Code Example Using the Host Header
def lambda_handler(event, context): request = event['Records'][0]['cf']['request'] headers = request['headers'] # Use the host header to determine the requested domain host = headers['host'][0]['value'] if host == '706.domain.com': request['origin'] = { 's3': { 'region': 'your-region', # Specify the region 'authMethod': 'none', 'domainName': '706.s3.amazonaws.com', 'path': '', 'customHeaders': {} } } request['headers']['host'] = [{'key': 'host', 'value': '706.s3.amazonaws.com'}] elif host == '707.domain.com': request['origin'] = { 's3': { 'region': 'your-region', # Specify the region 'authMethod': 'none', 'domainName': '707.s3.amazonaws.com', 'path': '', 'customHeaders': {} } } request['headers']['host'] = [{'key': 'host', 'value': '707.s3.amazonaws.com'}] return request
- Specify the actual AWS region of your S3 buckets in the region field of the request['origin']['s3'] object.
- Ensure your CloudFront distribution is set to trigger the Lambda@Edge function on the default viewer request behavior.
- Verify that your Lambda@Edge function has the necessary IAM permissions, particularly for modifying request objects and executing in response to CloudFront events.
- Deploy your updated Lambda@Edge function, and test it with different domain names to confirm the routing logic works as expected.
Relevant content
- asked 3 months ago
- asked 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 10 months ago
- AWS OFFICIALUpdated a year ago