3

I would like to be able to get usage data from Twilio from within an AWS Lambda function. I am following the examples on twilio's REST API page but am not having any success. I am using version 3 of Twilio's Node Helper Library. Below is the code that I have in the handler:

    'use strict';
    exports.handler = async(request, context) => {
        const accountSid = 'my account sid'; 
        const authToken = 'my auth token'; 
        const client = require('twilio')(accountSid, authToken);
        client.usage.records.today.each(record => console.log(record.count));
    };

The Lambda "feels" like it is at least trying to get the data from Twilio. It runs for ~10 seconds before ending without any errors. However I never get the 'here' message.

Thanks in advance, Scott

  • I am running the lambda manually in the AWS console. I have the lambda's timeout set to 30 seconds. Here is the output: Function Logs: END RequestId: b0c1d674-ba92-4a2c-8ed0-6cc9cf52e22d REPORT RequestId: b0c1d674-ba92-4a2c-8ed0-6cc9cf52e22d Duration: 11034.23 ms Billed Duration: 11100 ms Memory Size: 128 MB Max Memory Used: 128 MB Init Duration: 463.96 ms – Scott Jensen Feb 27 '20 at 17:17
  • Increase memory, 128 MB might not be enough. – Alex Baban Feb 27 '20 at 17:20
  • I just saw that as I was reviewing my comment. I will let you know what happens. – Scott Jensen Feb 27 '20 at 17:22
  • @AlexBaban Set the lambda to max memory (3008 MB). The lambda now executes in 700ms, uses 143 MB of memory. Still not getting any output. – Scott Jensen Feb 27 '20 at 17:33
  • Do you have anything in the "Execution Result" tab (underneath function code editor)? – Alex Baban Feb 27 '20 at 17:46
  • Nope. I don't return anything from the lambda so the Response is null. For Logs I have just the entries that AWS inserts (the START, END and REPORT log entries). – Scott Jensen Feb 27 '20 at 17:49

1 Answers1

1

This is my Lambda code:

exports.handler = (event, context, callback) => {

    // Your Account SID from www.twilio.com/console
    const accountSid = process.env.TWILIO_ACCOUNT_SID;

    // Your Auth Token from www.twilio.com/console
    const authToken = process.env.TWILIO_AUTH_TOKEN;

    // Import Twilio's Node Helper library
    // Create an authenticated Twilio Client instance
    const client = require('twilio')(accountSid, authToken);

    client.usage.records.lastMonth.each(record => console.log('here'));


};

and this is what I see in the "Function code" section after I run the function (Status: Succeeded).

enter image description here

Alex Baban
  • 11,312
  • 4
  • 30
  • 44
  • OK, my Lambda was using async function. I didn't see anywhere in the twilio docs that this was for non-async functions. Thanks Alex! – Scott Jensen Feb 27 '20 at 19:08