Lambda @ Edge触发Dynamodb时出现503错误 [英] Lambda@Edge when triggered Dynamodb giving 503 Error

查看:142
本文介绍了Lambda @ Edge触发Dynamodb时出现503错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过cloudfront viewer请求调用 Lambda 。这是我的Lambda代码

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);
});
};

当我通过控制台触发相同的lambda时,它会给出正确的响应。但是,当我通过Cloudfront进行部署时,它给出了 503错误。但是我曾尝试使用代码 Dynamodb Client 使用相同的代码,但效果很好。这是工作中的

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);
}
};

我已将完整的dynamodb权限授予了lambda @ edge。

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

感谢任何帮助
谢谢

Any help is appreciated Thanks

推荐答案

您为DyanamoDB指定了区域吗?

Lambda @ Edge可能在缺少DDB表的区域中执行。
区域的优先顺序。您还可以查看此 L @ E车间代码文档,以获取有关调用DDB的更多详细信息。

附带说明:观看者面对Lambda函数,调用跨区域动态表将对您的延迟产生负面影响。不确定您的用例,但看看是否可以将此调用移至面向原始事件或对ddb进行异步调用。

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.

这篇关于Lambda @ Edge触发Dynamodb时出现503错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆