lambda edge routing

0

I'm trying to do traffic routing based on url to different distribution in cloudfront. Lets say I have 706.domain.com and 707.domain.com. both are pointing to the same Cloudfront where there are 3 different origins based on s3 bucket. bucket1 (default), bucket2 (706), bucket3 (707). I'm attaching lambda@edge to default behaviour which is changing origin bucket depend on the url.

def lambda_handler(event, context): request = event['Records'][0]['cf']['request'] headers = request['headers']

if 'referer' in headers:
    referer = headers['referer'][0]['value']
    if 'https://706.domain.com/' in referer:
        request['origin'] = {
            's3': {
                'region': '',
                'authMethod': 'none',
                'domainName': '706.s3.amazonaws.com',
                'path': '',
                'customHeaders': {}
            }
        }
        request['headers']['host'] = [{'key': 'host', 'value': '706.s3.amazonaws.com'}]
    elif 'https://707.domain.com/' in referer:
        request['origin'] = {
            's3': {
                'region': '',
                'authMethod': 'none',
                'domainName': '707.s3.amazonaws.com',
                'path': '',
                'customHeaders': {}
            }
        }
        request['headers']['host'] = [{'key': 'host', 'value': '707.s3.amazonaws.com'}]
return request

Website is showing only website from default bucket. Am I doing something wrong or it's not possible to do routing like that?

Lukas
asked a month ago108 views
1 Answer
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.
profile picture
EXPERT
answered a month ago
profile picture
EXPERT
reviewed a month ago
profile picture
EXPERT
reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions