Get Started Building Intelligent, Serverless Apps Using AWS Lambda and Algorithmia

In this walkthrough, we’ll show you how to quickly make a serverless photo app that creates digital art pieces in less than 300 lines of code using AWS Lambda and Algorithmia. We’ll be using the Quadtree Art Generator algorithm to create our art, and push the new image to our S3 bucket automatically:


AWS Lambda is great, because you can run code without provisioning or managing servers. Similarly, Algorithmia let’s you tap into the power of the algorithm economy with just a single API call. Together you can quickly build and deploy serverless apps within minutes.

Ready? Okay, let’s get started.

Step 1: Create Accounts

You’ll need a free AWS account, as well as an Algorithmia account. We provide you with 10,000 credits to get started, which will be more than enough for this demo and beyond.

Step 2: Create Your S3 Bucket

Now we need to create your S3 bucket for this project.

Start by selecting S3 from the AWS dashboard.


Then select “Create Bucket” from the Actions menu. Give your bucket a unique name (remember: only lowercase names, and no spaces), and then select a region where you want this bucket hosted


Once your bucket is created, you want to create two folders: input, and output. The input folder is what Lambda will watch for new images. The images will get processed and then returned to the output folder.

Step 3: IAM Role Configuration

Before we can create a Lambda function, we need to first make an IAM execution role. IAM stands for “Identity and Access Management,” and is an AWS service that helps you control access.

First, go to the AWS IAM Roles page, and select “Create Role.”


Under “Select Role Type,” find and select AWS Lambda. Search for AWSLambdaExecute, and select that.

If you need it, find the complete AWS documentation for this step here.

Step 4: Create the Lambda Function

Create a Lambda function by going to the Services menu in the AWS console, and select Lambda from the list.


Hit the blue “Get Started Now” button. On the “Select Blueprint” page, scroll to the bottom and hit “Skip.”

Give your function a name, description, and set the runtime to Node.js.

Now, copy our SDK from this Gist, into the Lambda function code box below.


Replace ‘YOUR_API_KEY_HERE’ with your Algorithmia API key in the Gist. Your API key can be found on the dashboard of your Algorithmia account.


Let’s walk through the top half of the Gist so we can understand how this works. We first define which Algorithmia algorithm we want to use. In this case we’re using the Quadtree Art Generator:

var algo = "algo://besirkurtulmus/quadtree_art/0.1.x"; 
Then we grab the new image from our S3 bucket
var s3 = new AWS.S3();
var bucket = event.Records[0]
var key = decodeURIComponent(event.Records[0].s3.object.key.replace(/+/g, " "));
var params = {Bucket: bucket, Key: key};
var signedUrl = s3.getSignedUrl('getObject', params);

We process the image, turning it into quadtree art, and upload the image back to our bucket.

var client = algorithmia(apiKey);
    client.algo(algo).pipe(signedUrl).then(function(output) {
        if(output.error) {
            // The algorithm returned an error
            console.log("Error: " + output.error.message);
            // We call context.succeed to avoid Lambda retries, for more information see: 
        } else {
            // Upload the result image to the bucket
            var outputKey = 'output/'+key.substring(key.lastIndexOf('/') + 1);
            var params = {Bucket: bucket, Key: outputKey, Body: output.get()};
            s3.upload(params, function(err, data) {
                if (err) {
                    console.log("Error uploading data: ", err);
                } else {
                    console.log("Successfully uploaded data to bucket");
                    context.succeed("Finished processing");

Got that? Okay, great.

When you’re ready, select the IAM role you created in Step 3. It is best to be on the safe side and adjust Timeout to be maximum, so 5 min, and hit “Next.“

Step 5: Configure Event Sources

Once your function is created, we need to setup the event for Lambda to respond to. Start by clicking the "Event Sources” tab on your functions detail page. Then select “Add event source.”

Select S3 from the event source type drop-down. Select the bucket you created in Step 2. The event type you want to select is “Object Created (All).” We also want to add a prefix to tell Lambda to watch for new images here. In this case, we’ll use the prefix “input/”. Hit submit and you’re done.

Congrats, your AWS Lambda + Algorithmia function is ready to go. Lambda will now listen for new events in your S3 bucket, and automatically pass those images to Algorithmia where they will get processed by the Quadtree Art Generator, and then added back to S3 in the /output folder.

Test this out by logging into your S3 bucket, and navigating to the input folder, and uploading an image. Then, navigate to the output folder, where you’ll have your own piece of digital quadtree art!

Here’s out founders Diego Oppenheimer, and Kenny Daniel before:


…and after quadtree art generation:


What’s Next

You now have a working prototype that uses AWS Lambda and Algorithmia. You could use this same workflow to easily detect and crop photos using Smart Thumbnail, transcribe videos using speech to text, or check images for nudity. Learn more about how to leverage Algorithmia and Lambda here.

In a follow-up guide, we’ll teach you how to create a simple Android photo sharing app for uploading photos to S3, where Lambda will pick them up and turn them into digital art for others to enjoy.