Trying to call API with a list of URLs but Lambda is timing out


I'm trying to call the Pagespeed Insights API and save the response back in Dynamo. The Lambda timeout is 15 min. I will eventually need to call the API with about 100 URLs with an average response time of 20-30 sec.

What is the best approach on doing this?

My current code looks like this:

const { v4 } = require('uuid');
const axios = require('axios');
const urls = require('urls.json');
const endpoint = "";
const API_KEY = "";
const dynamodb = require('aws-sdk/clients/dynamodb');
const docClient = new dynamodb.DocumentClient();
const tableName = process.env.SAMPLE_TABLE;

var id;

const insertRecords = async (_id, _url, _lighthouseResults) => {

const metrics = _lighthouseResults.lighthouseResult.audits.metrics.details.items[0];

    const params = {
        TableName: tableName,
        Item: { id: _id,
                created_at: new Date().toISOString(),
                URL: _url,
    console.log("got to db entry")

    return docClient.put(params).promise(); // convert to Promise

exports.putItemHandler = async (event) => { // Async function
  for (const url of urls) {
    id = v4();
    console.log(url + " - " + id);

    const lighthouseResults = await getLighthouse(url);     

    await insertRecords(id, url, lighthouseResults) // wait until it finish and go to next item
      .catch((error) => {
        // throw error; don't care about this error, just continue

const getLighthouse = async (url) => {
    console.log("inside getLighthouse")

    try {
        const resp = await axios.get(endpoint, {
            params: {
                key: API_KEY,
                url: url,
                category: 'performance',
                strategy: 'mobile'


    catch (err) {

asked 2 years ago919 views
3 Answers

One option here is to decouple the database read from the API query. You can do this by using SQS: As you read each entry from the database, send it to SQS and then trigger Lambda functions from the SQS queue. That way, each Lambda function has a separate runtime and doesn't have a shared fate with any of the other queries. You probably want to limit the concurrency there so that you're not overloading the target API with calls. This method will also let you scale well beyond 100 URLs.

profile pictureAWS
answered 2 years ago

Please take a look at this workshop -

It appears very similar to what you are trying to achieve - 1 front-end call branching off to multiple backend call and responses of the backend calls getting written to DynamoDB. Front-end waits for all responses or a finite amount of time, before responding back to the client.

profile pictureAWS
answered 2 years ago

I would go for an approach with a StepFunction.

Step1 - call lambda that returns urls to process 
Step2 - parallel iterator (you can configure parallellism)
  Step2.1 - call PageSpeed for 1 url + store in DDB (you can configure error handling, and gather a result)

The result of the StepFunction might be a analysis report of the whole run

profile picture
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions