Lambda@Edge when triggered Dynamodb giving 503 Error

丶灬走出姿态 提交于 2020-01-25 09:17:08

问题


I am trying to invoke Lambda through cloudfront viewer request . Here is my Lambda code

'use strict';
const AWS = require("aws-sdk");
const docClient = new AWS.DynamoDB.DocumentClient();
exports.handler = (event, context, callback) => {
    /* Get request */
    const request = event.Records[0].cf.request;
    const requestbody = Buffer.from(request.body.data, 'base64').toString();
    const data = JSON.parse(requestbody);
    const Id = data.Name;
    console.log(Id);

    /* Generate body for response */
    const body = 
     '<html>\n'
     + '<head><title>Hello From Lambda@Edge</title></head>\n'
     + '<body>\n'
     + '<h1>You clicked more than 10 Times </h1>\n'
     + '</body>\n'
     + '</html>';
    var params = {
        TableName: "Test",
         ProjectionExpression: "#V,#N",
         KeyConditionExpression: "#N = :v1",
         ExpressionAttributeNames: { 
                  "#N" : "Name",
                  "#V" : "Value"
                                  },
  ExpressionAttributeValues: {
  ":v1": Id
  }
 };
var querydb = docClient.query(params).promise();
querydb.then(function(data) {
    console.log(data.Items[0].Value);
    if(data.Items[0].Value >= 11){
    const response = {
        status: '200',
        body: body,
    };
    callback(null, response);
}else {
    callback(null,request);
}
}).catch(function(err) {
  console.log(err);
});
};

When i triggered the same lambda through console it is giving correct response. But when i deployed through Cloudfront it is giving 503 Error. But i had tried the same code withcode Dynamodb Client it worked perfectly fine. Here is the working one

'use strict';
const AWS = require("aws-sdk");
const docClient = new AWS.DynamoDB.DocumentClient();
exports.handler = (event, context, callback) => {
    /* Get request */
    const request = event.Records[0].cf.request;
    const requestbody = Buffer.from(request.body.data, 'base64').toString();
    const data = JSON.parse(requestbody);

    /* Generate body for response */
    const body = 
     '<html>\n'
     + '<head><title>Hello From Lambda@Edge</title></head>\n'
     + '<body>\n'
     + '<h1>You clicked more than 10 Times </h1>\n'
     + '</body>\n'
     + '</html>';
     if(data.Value >= 10){
    const response = {
        status: '200',
        body: body,
    };

    callback(null, response);
}
else {
 callback(null, request);
}
};

I had given full dynamodb permissions to the lambda@edge.

Any help is appreciated Thanks


回答1:


Where have you specified region for DyanamoDB?
It is possible that Lambda@Edge is executing in a region where your DDB table is missing. Have a look at AWS doc on region's order of precedence. You can also look at this L@E workshop code and documentation for more details on calling DDB.
On a side note: A viewer facing Lambda function, making a call to a cross region dynamodb table will have negative effects on your latency. Not sure about your use case but see if it is possible to move this call to an origin facing event or make async call to ddb.



来源:https://stackoverflow.com/questions/53235627/lambdaedge-when-triggered-dynamodb-giving-503-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!