Getting StartedTutorialsDeveloper GuidesAutomatically Add Assets to the Asset Manager Using AWS Lambda

Automatically Add Assets to the Asset Manager Using AWS Lambda

While it’s best to upload assets directly into the Asset Manager, there are cases where you may not be able to.

For example, users may upload assets directly to an S3 bucket if they have user-generated content. In this case, you can use an AWS Lambda function to add the asset to the Asset Manager automatically.

In this tutorial, you’ll learn how to create an AWS Lambda function that listens for new assets in an S3 bucket and adds them to the Asset Manager.

The Asset Manager is not required to use the Rendering API

Any asset added to your Origin is automatically available with the Rendering API via the Origin path. See the Serving Assets guide to learn how to serve images.

This article explicitly addresses adding assets to the Asset Manager to leverage additional features such as metadata and tagging. It can also be used to automatically encode videos served with the Video API to transcode videos before users request them.

Prerequisites

  • AWS S3 bucket
  • AWS Lambda (Runtime: Node.js 22.x)

Implementation

Step 1: Create a Lambda Layer

First, create the Lambda Layer by following the two guides below to connect a Lambda function to your S3 bucket.

We recommend following these tutorials to create and understand Lambda functions connected to S3. We recommend making sure you understand the following:

  • Creating and modifying code in your Lambda function
  • Using Cloudwatch to view logs for testing
  • Setting up triggers (S3 -> Lambda) for your Lambda function

Once you understand how to create and test functions, you can proceed to copy the code into your function.

Step 2: Copy this code into your Lambda function

Copy the following code into your Lambda function. This code does the following:

  1. Listens to an S3 bucket for new assets
  2. Retrieves the path
  3. Construct a request to imgix using the add endpoint
import https from "https";
 
export async function handler(event) {
  try {
    // Extract bucket name and object key from the event
    const record = event.Records[0];
    const objectKey = decodeURIComponent(
 record.s3.object.key.replace(/\+/g, " ")
 );
 
    // Retrieve environment variables
    const imgixApiKey = process.env.IMGIX_API_KEY;
    const imgixSourceId = process.env.IMGIX_SOURCE_ID;
 
    if (!imgixApiKey || !imgixSourceId) {
 console.error(
        "IMGIX_API_KEY or IMGIX_SOURCE_ID is not set in environment variables."
 );
      throw new Error(
        "Server configuration error: Missing IMGIX_API_KEY or IMGIX_SOURCE_ID."
 );
    }
 
    // Prepare the Origin path for the imgix add endpoint
    const originPath = encodeURIComponent(objectKey);
 
    // Create the request body
    const requestBody = JSON.stringify({});
 
    // Options for the HTTPS request
    const options = {
 hostname: "api.imgix.com",
 path: `/api/v1/sources/${imgixSourceId}/assets/add/${originPath}`,
 method: "POST",
 headers: {
 Authorization: `Bearer ${imgixApiKey}`,
        "Content-Type": "application/json",
        "Content-Length": Buffer.byteLength(requestBody),
      },
    };
 
    // Make a request to the imgix add endpoint
    const response = await makeHttpsRequest(options, requestBody);
 
 console.log("Asset added successfully:", response);
    return {
 statusCode: 200,
 body: JSON.stringify({ message: "Asset added successfully" }),
    };
  } catch (error) {
 console.error("Error adding asset to imgix:", error);
    return {
 statusCode: 500,
 body: JSON.stringify({ error: "Failed to add asset to imgix" }),
    };
  }
}
 
// Helper function to make HTTPS requests
function makeHttpsRequest(options, body) {
  return new Promise((resolve, reject) => {
    const req = https.request(options, (res) => {
 
 res.on("end", () => {
        if (res.statusCode >= 200 && res.statusCode 300) {
          resolve();
        } else {
          reject(new Error(`HTTP ${res.statusCode}`));
        }
      });
    });
 
 req.on("error", (error) => {
      reject(error);
    });
 
    // Send the request body
 req.write(body);
 req.end();
  });
}

Step 3: Set up environment variables

Next, set up the following environment variables in your Lambda function:

Step 4: Test and confirm your function is working

Once you’ve:

  1. Created and saved the Lambda function code above
  2. Configured your environment variables
  3. Set up the S3 trigger

You can test your function by uploading a new asset to your S3 bucket. You should see the asset added to the Asset Manager in your imgix dashboard.

If you do not see the asset added:

  • Check the Cloudwatch logs for your Lambda function for any errors
  • Ensure you’re looking at the proper Source ID in your Asset Manager
  • Recheck the Asset Manager in a few minutes, as there may be a delay
  • Utilize an endpoint testing tool like Pipedream to test the Lambda function

Conclusion

In this tutorial, you learned how to automatically add assets to the Asset Manager using an AWS Lambda function. This function enables you to pull assets from an S3 bucket to your Asset Manager at scale.