Gpcnec76
Gpcnec76

Reputation: 69

Twilio: Lamba Function not making Programable Web call

Relatively new to AWS Lambda, and I'm trying to integrate Twilio Programmable Voice into a Lambda function. The code in Function is the following:

'use strict';

module.exports.hello = async event => {
  console.info("Program Started");  
  const accountSid = 'AAAAAA';
  const authToken = 'BBBBBB';
  const client = require('twilio')(accountSid, authToken);

  client.calls
        .create({
           twiml: '<Response><Say>Ahoy, World!</Say></Response>',
           to: '+1XXXXXXXXXX',
           from: '+1YYYYYYYYY'
         })
        .then(call => console.log(call.sid));

  console.info("Program Ended");
};

The accountSid and authToken are correct in the implementation. Twilio is inside of a Layer and the test is able to find the dependency. The logging shows both "Program Started" and "Program Ended", so the code is being called. But there is no actual call when testing. Any suggestions??

Upvotes: 0

Views: 107

Answers (1)

Ashish Modi
Ashish Modi

Reputation: 7770

You are not returning promise from your function so there is no way for lambda to identify if your execution has completed. The last line which is console is being executed before client.calls finishes the execution as that is asynchronous. You have two choices here

  • Either change it to return the promise like this
'use strict';

module.exports.hello = async event => {
  console.info("Program Started");  
  const accountSid = 'AAAAAA';
  const authToken = 'BBBBBB';
  const client = require('twilio')(accountSid, authToken);

  return client.calls
        .create({
           twiml: '<Response><Say>Ahoy, World!</Say></Response>',
           to: '+1XXXXXXXXXX',
           from: '+1YYYYYYYYY'
         })
        .then(call => console.log(call.sid))
        .then(() => console.info("Program Ended"));

};
  • OR change it to use await style
'use strict';

module.exports.hello = async event => {
  console.info("Program Started");  
  const accountSid = 'AAAAAA';
  const authToken = 'BBBBBB';
  const client = require('twilio')(accountSid, authToken);

  const call = await client.calls
        .create({
           twiml: '<Response><Say>Ahoy, World!</Say></Response>',
           to: '+1XXXXXXXXXX',
           from: '+1YYYYYYYYY'
         });
  console.log(call.sid);
  console.info("Program Ended");
};

Upvotes: 1

Related Questions