1

I am trying to call my rest api endpoint in AIRTABLE from inside an AWS Lambda with no success. I get no errors, no outputs.
If I call the same code using node - it works.
I am able to use Axios in my code.

Pure airtable code (works)

var Airtable = require('airtable');
var base = new Airtable({apiKey: 'keyoMYSECRETKEY'}).base('Mybaseid');

base('MyBase').select({maxRecords: 3,view: "MyView"}).eachPage(function page(records, fetchNextPage) {
    // This function (`page`) will get called for each page of records.
    records.forEach(function(record) {
        console.log('Retrieved',JSON.stringify(record.get('Session Information')));
    });
    fetchNextPage();

}, function done(err) {
    if (err) { console.error(err); return; }
});

If I put it inside a Lambda handler - I get nothing.

const axios = require('axios')
const url = 'https://checkip.amazonaws.com/';
var Airtable = require('airtable');
var base = new Airtable({apiKey: 'keySECRETKEY'}).base('MYBASEID');
let response;

exports.lambdaHandler = async (event, context) => {
    try {
        base('MyBase').select({maxRecords: 3,view: "MyView"}).eachPage(function page(records, fetchNextPage) {
            records.forEach(function(record) { //HERE - NOTHING HAPPENS
                console.log('Retrieved',JSON.stringify(record.get('Session Information')));
            });
            fetchNextPage();

        }, function done(err) {
            if (err) { console.error(err); return; }
        });
        
        const ret = await axios(url); //THIS WORKS
        response = {
            'statusCode': 200,
            'body': JSON.stringify({
                message: 'hello world - boo',
                location: ret.data.trim()
            })
        }
    } catch (err) {
        console.log(err);
        return err;
    }

    return response
};

What am I missing so I can call Airtable API from inside an AWS Lambda?

Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
  • Just guessing: are you missing access to the internet? - one maybe answer is that the lambda request is timing out while your client tries to reach the network. You might need a VpcConfig? – VoiceOfUnreason Jul 26 '21 at 19:28
  • @VoiceOfUnreason This is why I added the code example with Axios - which succeeds. So internet access is not an issue. Timeout, might be it, Right now, I am testing it using SAM locally (although I did try as a true lambda with same results). Any idea how I increase TTL locally? – Itay Moav -Malimovka Jul 26 '21 at 19:31

2 Answers2

0

It seems that your lambda terminates before the API call execution your trying to perform. I believe this will be solved using a synchronous lambda or with a correct usage of promises with await calls.

Shoty
  • 1
  • 2
  • kindly use comment option with question – Haseeb Sep 02 '21 at 17:58
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Sep 02 '21 at 17:59
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). – JW Geertsma Sep 03 '21 at 12:39
0

Best way to troubleshoot this is to go back to the basics.

See if you can at least get a meaningful console log by wrapping a simpler fetch request into a lambda handler:

const baseId = 'exampleAppId123';
const tableName = 'Table 1';
const api_key = 'keyExample123';

const url = `https://api.airtable.com/v0/${baseId}/${tableName}?api_key=${api_key}`;

exports.lambdaHandler = async () => {
    const res = await fetch(url)
            .then(res => res.json())
            .then(data=>console.log(data))
            .then(() => {
            //do more stuff
           })
}

Then report back if you can't. Or better yet, report back either way as that's bound to help more people in the future.

Worst case? The above code still doesn't do anything. If that happens, I suggest going with @Shoty's first instinct and turning this code into a synchronous fetch request by removing the async/await syntax and returning chained thenables. Not that blocking behavior of this sort is acceptable from a UX perspective, but it should at least help with debugging.

  • the ```await axios``` piece actually works well. Just the Airtable specific API above does not. What is funny is, I am trying other ways to fetch the same data (they have several ways) and the other ways work. I guess my main problem here is, Why don't I see proper logs of what is going on. – Itay Moav -Malimovka Sep 06 '21 at 15:02