title: Using the Node Request module with AWS Lambda and API Gateway
body: <p>This lambda function works:</p>
  
  <pre><code>exports.handler = function(event, context, callback) {
    const done = (err, res, func) =&gt; callback(null, {
      statusCode: '200',
      body: res,
      headers: { 'Content-Type': 'application/json' },
    })
  
    done(null, 'works'))
  }
  </code></pre>
  
  <p>I can hit the API Gateway end point and receive the response message, <code>'works'</code>.</p>
  
  <p>However, when I add a request call (<code>POST</code>ing a message to Slack) that looks like this:</p>
  
  <pre><code>const request = require('request')
  const moment = require('moment-timezone')
  
  exports.handler = function(event, context, callback) {
    // the json object is built following these docs:
    // https://api.slack.com/docs/message-attachments
    const SLACK_WEB_HOOK_URL = process.env.SLACK_WEB_HOOK_URL
    const uri = SLACK_WEB_HOOK_URL
    const method = 'POST'
    const options = {
      json,
      uri,
      method,
    }
  
    const slack = (opts) =&gt; {
      request.post(opts, (err, response, body) =&gt; {
        if (response.statusCode &lt; 300) {
          return 'Success!'
        } else {
          return 'Err!'
        }
      })
    }
  
    const done = (err, res, func) =&gt; callback(null, {
      statusCode: '200',
      body: res,
      headers: { 'Content-Type': 'application/json' },
    })
  
    done(null, slack(options))
  }
  </code></pre>
  
  <p>The server hangs when I hit the API end point ...</p>
  
  <p>And I get a <code>Task timed out after 10.00 seconds</code> error:</p>
  
  <pre><code>{
    "errorMessage": "2017-06-23T16:41:21.483Z c385e32e-5832-11e7-8c4f-673b524cf783 Task timed out after 10.00 seconds"
  }
  </code></pre>
  
  <p>But my Slack <code>POST</code> request gets sent and I see the message in the channel.</p>
  
  <p>How do I send the <code>POST</code> request and then wait for it to return, and then exit the lambda function with a custom response message?</p>
  
  <p>Thanks for the help!</p>