-1

This is the "code" section of my AWS Lambda function. It works fine when I use this on our staging environment (non-SSL.)

I've tried to use the answer from this stackoverflow post, but I am receiving an npm error Cannot find package ssl-root-cas, and I'm not entirely sure if I'm able to install node packages.

'use strict';

const https = require('https');

var rootCas = require('ssl-root-cas').create();

https.globalAgent.options.ca = rootCas;

/**
 * Pass the data to send as `event.data`, and the request options as
 * `event.options`. For more information see the HTTPS module documentation
 * at https://nodejs.org/api/https.html.
 *
 * Will succeed with the response body.
 */
exports.handler = (event, context, callback) => {

    var url = 'https://admin.domain.com/api/schedule/checkForPublishedScreeners?authentication_token=mytoken';
    https.get(url, (res) => {

        let body = '';
        console.log('Status:', res.statusCode);
        console.log('Headers:', JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', (chunk) => body += chunk);
        res.on('end', () => {
            console.log('Successfully processed HTTPS response');
            // If we know it's JSON, parse it
            if (res.headers['content-type'] === 'application/json') {
                body = JSON.parse(body);
            }
            callback(null, body);
        });

    }).on('error', (e) => {
        console.error(e);
    });
};
D'Arcy Rail-Ip
  • 11,505
  • 11
  • 42
  • 67

1 Answers1

3

You need to include ssl-root-cas in package.json, and include it when you publish your lambda code.

npm init -y
npm install --save ssl-root-cas

Zip up the node_modules along with your lambda code, then publish it to Lambda

Chris Rouffer
  • 743
  • 5
  • 14
  • I'm not actually submitting a package.json, for example I never include anything for the `require('https')` line. Perhaps I'm missing something on the Lambda implementation? – D'Arcy Rail-Ip May 23 '17 at 19:03
  • 1
    the `https` module is built into the NodeJS Lambda runtime. The `ssl-root-cas` module isn't. – Mark B May 23 '17 at 19:10
  • You need to include ssl-root-cas in your node_modules that you zip and upload. – Chris Rouffer May 23 '17 at 19:11