criabdala
criabdala

Reputation: 1

Amazon Lambda@edge response

I'm using AWS S3 and cloudfront to host a static site. I need to catch when the user agent is whatsapp or other...

So, I'm using lambda@edge function associated to a cloudfront with tiggers. Then, Tigger has 4 options (viewer_request, origin_request, origin_response, viewer_response). I'm develpoing a small script, tiggered on viewer_request that if the user agent is Whatsapp the response will be a pure html but if the user agent is any other the response should continue the natural flow and thus cloudfront should response a index.html configurated in the cloudfront properties. I can't do the flow continue to the index.html...

My code:

let content = ``;

exports.handler = (event, context, callback) => {
   var response = event.Records[0].cf.response;

   const request = event.Records[0].cf.request;
   const headers = JSON.stringify(request.headers);

   if(headers.toUpperCase().indexOf("WHATSAPP")>0) {
    console.log("is whatsapp");

    var html = `
    <html prefix="og: http://ogp.me/ns#">
        <head>
            <meta property="og:url" 
            content="http://www.nytimes.com/2015/02/19/arts/international/when-
            great-minds-dont-think-alike.html" />
            <meta property="og:type" content="article" />
            <meta property="og:title" content="When Great Minds Don’t Think 
            Alike" />
            <meta property="og:description" content="How much does culture 
            influence creative thinking?" />
            <meta property="og:image" 
    content="http://static01.nyt.com/images/2015/02/19/arts/international/19iht-btnumbers19A/19iht-btnumbers19A-facebookJumbo-v2.jpg" />
        </head>
        <body>Whatsapp</body>
    </html>    
    `;

    response = {
        status: '200',
        statusDescription: 'OK',
        headers: {
            'cache-control': [{
                key: 'Cache-Control',
                value: 'max-age=100'
            }],
            'content-type': [{
                key: 'Content-Type',
                value: 'text/html'
            }],
            'content-encoding': [{
                key: 'Content-Encoding',
                value: 'UTF-8'
            }],
        },
        body: html,
    };
    context.succeed(response);    
   } else {  
    context.succeed(null);
   }

Upvotes: 0

Views: 3097

Answers (2)

lazurey
lazurey

Reputation: 327

I ran into the same issue, and I fixed my problem by separating into 2 lambda functions, one handles request, the other handles response.

  1. In CloudFront behavior settings, choose viewer request for the one processes request. And choose origin response for the one processes response.
  2. For the request one, remember to return callback(null, request) as @michael-sqlbot mentioned.

Note:

  • viewer request contains raw user agent from request headers, e.g. "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
  • origin request contains CloudFront processed headers, e.g. "Amazon CloudFront"

Hope this helps and please correct me if anything wrong.

Reference:

Upvotes: 1

Michael - sqlbot
Michael - sqlbot

Reputation: 179404

To allow request processing by CloudFront to continue from a Viewer Request or Origin Request trigger:

return callback(null,request);

Upvotes: 0

Related Questions