0

I am trying to redirect few domains to one with AWS Lambda

The logic is following:

  1. Hosted zone for domain example.eu with A record being Alias to an LB
  2. LB forwarding traffic to a Target Group and has a valid certificate
  3. Target is a Lambda function:
exports.handler = async (event) => {
    const response = {
        statusCode: 301,
        headers: {
            Location: 'https://example.com'
        }
    };
    
    return response;
};
  1. When I click on test and pass something to Lambda - it works and returning 301 but it is never getting triggered when I open example.eu in browser or using curl.

Expected result:

When I open example.eu it returns me 301 Moved Permanently https://example.com

Daniil
  • 105
  • 7
  • 3
    Theoretically this should work as you described. What does calling example.eu returns? Are you sure your lambda gets called? Otherwise I'd suggest using a cloudfront distribution with a Viewer Request function association, instead of the LB + lambda combo. – Tamas Jul 20 '22 at 13:31
  • 1
    what permissions does your lambda have for invoking it? When you run test you are executing against your logged in user's permissions (full access) but when you hit api from your browser you are executing with your configured LB permissions / Lambda allowable permissions. – Tobin Jul 20 '22 at 13:37
  • If you only want to do a redirect, IIRC you can host a static redirect via S3. – Tobin Jul 20 '22 at 13:40
  • @Tamas it times out curl: (28) Failed to connect to example.eu port 80 after 290635 ms: Operation timed out Can you elaborate on this cloudfront distribution with a VIewer request function please? – Daniil Jul 20 '22 at 14:04
  • 1
    @Tobin thank you! Can you post your reply as an answer, I think I've had some SG issues, I recreated the rule and it started to work – Daniil Jul 20 '22 at 14:19

1 Answers1

1

As @Tobin pointed out - I had an issue with the security group. I've recreated the security group for 443 and 80 ports and it worked.

Daniil
  • 105
  • 7