最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Lambda funcionality works from its own "test" button, but it doesn´t triggering it from

programmeradmin3浏览0评论

The function simply inserts a new column on a DynamoDB´s table. I can verify it works by clicking the "test" button in the lambda function tab (it responds with a 200), but it returns an error when I attach it to an API Gateway´s POST Request, using the "test" button too, in this case the test button of the API Gateway´s method test tab.

This are the errors:

Response Body

{ "message": "Internal server error" }

Response Headers

{"x-amzn-ErrorType":"InternalServerErrorException"}

Logs

Lambda execution failed with status 200 due to customer function error: One or more parameter values were invalid: Missing the key site in the item.

Here´s the code of the lambda function:

function response( message) {
  return message
}

const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'eu-west-2'});

exports.handler = function(event, context) {
   
    let scanningParameters = {
        Item: {
            "site":event.site
           
        },
        TableName: 'Galleries'
    }
    
     return docClient
    .put(scanningParameters)
    .promise()
    .then(() => {
        return {
        "statusCode": 200,
        'headers': { 'Content-Type': 'application/json' }
    }
    })
   
}

The function simply inserts a new column on a DynamoDB´s table. I can verify it works by clicking the "test" button in the lambda function tab (it responds with a 200), but it returns an error when I attach it to an API Gateway´s POST Request, using the "test" button too, in this case the test button of the API Gateway´s method test tab.

This are the errors:

Response Body

{ "message": "Internal server error" }

Response Headers

{"x-amzn-ErrorType":"InternalServerErrorException"}

Logs

Lambda execution failed with status 200 due to customer function error: One or more parameter values were invalid: Missing the key site in the item.

Here´s the code of the lambda function:

function response( message) {
  return message
}

const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'eu-west-2'});

exports.handler = function(event, context) {
   
    let scanningParameters = {
        Item: {
            "site":event.site
           
        },
        TableName: 'Galleries'
    }
    
     return docClient
    .put(scanningParameters)
    .promise()
    .then(() => {
        return {
        "statusCode": 200,
        'headers': { 'Content-Type': 'application/json' }
    }
    })
   
}

However I don´t get why is asking for a key since from the lambda tab it does insert the column in the table correctly. Here is another function which does work in API Gateway too and the schema in absolutely the same:

function response( message) {
  return message
}

const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'eu-west-2'});

exports.handler = function(event, context) {
   
    let scanningParameters = {
        Item: {
            "email":event.email
        },
        TableName: 'Users'
    }
    
     return docClient
    .put(scanningParameters)
    .promise()
    .then(() => {
        return {
        "statusCode": 200,
        'headers': { 'Content-Type': 'application/json' }
    }
    })
   
}

Edit: I just solved it by unchecking the check of "Use Lambda Proxy integration" in the integration request tab

Share Improve this question edited Apr 15, 2020 at 17:00 J. Cake asked Apr 15, 2020 at 16:41 J. CakeJ. Cake 3511 gold badge4 silver badges20 bronze badges 6
  • Can you also please show us the console view/API gateway configuration where you specify the route to the Lambda function? – xinkecf35 Commented Apr 15, 2020 at 16:43
  • Mmm, I´m not sure if I know what you refer too, you mean the method request settings? – J. Cake Commented Apr 15, 2020 at 16:48
  • 1 Something like that. I can't describe the exact screen, but that will be part of yes. Also the screen where you can select how you proxy the request and what parameters/headers to pass will also be useful. – xinkecf35 Commented Apr 15, 2020 at 16:50
  • @xinkecf35 Oh man I just sort it out by checking those other tabs by some reason in the other example that it worked the "proxy integration" check, it was unchecked. In this case I had checked it. Once I unchecked it worked. Funny enough I thought this check was neccesary to lambda functions to work from API Gateway. – J. Cake Commented Apr 15, 2020 at 16:59
  • That's good to hear :-) – xinkecf35 Commented Apr 15, 2020 at 17:18
 |  Show 1 more ment

4 Answers 4

Reset to default 2

Unchecking the "Use Lambda Proxy integration" check box fixed the issue.

If you are passing dynamic params queryParams or pathParams,Then you need to use the Proxy integration and return in following way.

return {
        statusCode: 200,
        body: JSON.stringify(response)
    }

I was facing this error when i was returning in following way

  return {
        statusCode: 200,
        body: message,
        timeStamp: new Date().toISOString()
    }

you have to disable the default enabled button named as Lambda proxy integration and note written as Send the request to your Lambda function as a structured event. and where you can find this button is here,

first goto aws gateway portal then apis search your api in list click on it then it must have opened the resources ok then below create resources you see tree stuctured of your apis here third one Nothing but the method type, click on it for me its ANY in uppercase written for me, it will open flow diagram architecture of your api call, below you see navigation bar with options Method Request|integration request|Integration responses|Test select Integration request and click on edit and boom just disable this Lambda proxy integration field and just save now try to Test your api gateway Must working for you by now

Looks like they broke (or never fixed) it with Node.js 18.x, but changing to Node.js 20.works also with proxy integration.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论