0

I have a lambda written in node.js where i want to push the data to my dynamodb table. My end goal is for my api request to pull in the response and then push the results in the items section directly into dynamodb but at the moment my lambda is only pulling through the query and the createmessage function is being ignored (currently testing with static data). I presume my code is muddled somehow so need some help on how to have this running correctly, please see below:

// Loads in the AWS SDK const AWS = require('aws-sdk'); // Creates the document client specifing the region // The tutorial's table is 'in eu-west-2' const ddb = new AWS.DynamoDB.DocumentClient({region: 'eu-west-2'}); exports.handler = async (event, context, callback) => { // Captures the requestId from the context message const requestId = context.awsRequestId; // Handle promise fulfilled/rejected states await createMessage(requestId).then(() => { callback(null, { statusCode: 201, body: '', headers: { 'Access-Control-Allow-Origin' : '*' } }); }).catch((err) => { console.error(err) }) }; // Function createMessage // Writes message to DynamoDb table Message function createMessage(requestId) { const params = { TableName: 'splunk-lambda', Item: { 'id' : '101', 'message' : 'Hello from lambda' } } return ddb.put(params).promise(); } var request = require('request'); var options = { 'method': 'POST', 'url': 'myurl', 'headers': { 'x-api-key': 'my-api-key', 'Content-Type': 'text/plain' }, body: 'query my graphql query' }; request(options, function (error, response) { if (error) throw new Error(error); console.log(response.body); }); 

my response from the lambda:

Test Event Name test Response null Function Logs {my graphql response} END RequestId: bdf23337-ca51-4f8b-868c-314f4d048055 
5
  • 2
    This is probably something to do with your use of async. Since you are using async handler, your lambda finishes before the rest of the code has a chance to run. Commented Oct 18, 2021 at 8:46
  • Marcin is probably correct - I am not 100% certain for node.js, but the SDK for Python are async blockers - have to use a specific async version of the SDK to use it within async functions. I would hazard that the node.js version may have some similar issues. Commented Oct 18, 2021 at 12:14
  • is there a particular reason it has to be async? the SDK will always wait for a response from its call before continuing. If this is part of a much larger and more complicated process, try pulling it into its own lambda and putting your lambdas into parallel steps in a StepFunction Commented Oct 18, 2021 at 12:16
  • 1
    @Marcin your right, the async was the blocker and removal and rewrite resolved the issue. Could you convert your comment as an answer? Commented Oct 18, 2021 at 12:48
  • @FreshX Thanks. Answer added. Commented Oct 18, 2021 at 21:45

1 Answer 1

2

Based on the comments.

The issues was caused by using async handler. This leads to your function to finish before it has a chance run all its code.

One way to overcome this you can wrap your code in the handler in new Promise as shown in the AWS docs.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.