question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to set context in AWS Lambda handler?

See original GitHub issue

My code works fine with this code:

const {dialogflow} = require('actions-on-google');
const app = dialogflow();
// bunch of app.intent()
exports.handler = app;

Now I want to set context.callbackWaitsForEmptyEventLoop in AWS Lambda handler function in order to make my backend code work, and I did some search and tried this:

const {dialogflow} = require('actions-on-google');
const app = dialogflow();
// bunch of app.intent()
exports.handler = async function(event, context) {
    logger.info("got event: " + event)
    context.callbackWaitsForEmptyEventLoop = false;
    try {
        let body = JSON.parse(event.body);
        let headers = event.headers;
        let response = await app.handler(body, headers)
        logger.info(response)
        return response
    } catch (err) {
        logger.info("err: " + err)
    }
}; 

The code above provides a good response, but dialogflow received the following:

Webhook call failed. Error: 502 Bad Gateway

The documents only give the line exports.fulfillment = app which is not informative about how to set the content inside.

Can someone please help me with this? Any help is appreciated.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Canaincommented, Dec 13, 2018

Try to see if this works:

app.middleware((conv, framework) => {
  if (framework.lambda) {
    framework.lambda.context.callbackWaitsForEmptyEventLoop = false
  }
})

// ... intent code

exports.handler = app
1reaction
Canaincommented, Dec 11, 2018

Hi @luomanke currently that code isn’t implemented yet in the library. You would have to use something like

exports.handler = async (event, context, callback) => {
  const body = event.body || event;
  const headers = event.headers || context.request.headers || [];
  const result = await app.handler(body, headers);
  if (result.status >= 200 && result.status < 300) {
    callback(null, result.body);
  } else {
    callback(JSON.stringify(result));
  }
}

The code in the documentation is for if you have AWS Lambda API Gateway/HTTP Proxy integration enabled.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AWS Lambda context object in Python
When Lambda runs your function, it passes a context object to the handler. This object provides methods and properties that provide information about...
Read more >
Chapter 2: What is the event & context?
context is a Python objects that implements methods and has attributes. It's main role is to provide information about the current execution environment....
Read more >
What are event and context in function call in AWS Lambda?
Context Provides information about the invocation, function, and execution environment of your lambda. So you can use this to check the memory ...
Read more >
Add types to your AWS lambda handler - Xolvio
Lambdas handlers can be invoked with many different, but always complex, event arguments. Add to that the context, callback, matching return type and...
Read more >
Add Context for AWS Lambda - Sentry Documentation
The best practice to attach custom data is via structured contexts. A context must always be a dictionary or map, and its values...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found