Hi all,
I am trying to access an 3rd party API from my javascript lambda function.
So far I have tried the http standard library, axios and superagent. All do not work, console.logs from within their success or fail callbacks/promises are not being triggered. I am not able to even get a failed or timeout response.
I can confirm that the code and 3rd party API works, as I am able to get a response from a local node instance I have setup.
I have read through http://forum.serverless.com/t/solved-aws-python-call-to-external-apis-from-my-lambda-function/4177 and http://forum.serverless.com/t/how-do-i-grant-internet-access-to-my-function/1275, however my issue appears to be different. I am not getting a null response from the API call and I do not have any VPC configs.
Here is my serverless.yml snippet
myFunc:
handler: src/myFunc.main
events:
- http:
path: myRoute
method: get
cors: true
authorizer: aws_iam
and my lambda snippet
export async function main(event, context, callback) {
console.log('main function triggered') // appears in cloudwatch
try {
console.log('try block triggered') // appears in cloudwatch
const superagent = require('superagent')
console.log('superagent loaded', superagent) // appears in cloudwatch
superagent.get('https://api.nasa.gov/planetary/apod')
.query({
api_key: 'DEMO_KEY',
date: '2017-08-02'
})
.end((err, res) => {
console.log('end function triggered') // MISSING in cloudwatch
if (err) {
return console.log(err) // MISSING in cloudwatch
}
console.log(res.body.url) // MISSING in cloudwatch
console.log(res.body.explanation) // MISSING in cloudwatch
callback(null, success(res.body.explanation))
})
console.log('after superagent') // appears in cloudwatch
} catch (e) {
console.log('catch block ran') // does not appear in cloudwatch, as expected
console.log(e) // does not appear in cloudwatch, as expected
callback(null, failure({
status: false
}))
}
}
Any help will be greatly appreciated.