S3 connected to a Lambda Example

...
  • S3
  • Lambda
  • CloudWatch Logs
  • CloudWatch Alarms
About 2 min

# S3 connected to a Lambda Example

A really great use case for Lambda, is to have it connected to and S3 bucket, so that every time a file is created, updated, or deleted, a Lambda is triggered to process the file in same way. In this example we will briefly walk you through managing this setup within Commandeer. If you do not have Commandeer on your computer, you can download with the link below.

Download the Commandeer App - 15-day Free Trial - The #1 developer IDE to manage your serverless and container infrastructures, both locally and in the cloud. With support for 30+ of the best cloud services out there.

# Image Bucket Example

Let's say you have an image bucket, and users upload files to it, and you have a Lambda that is triggered that will take the image, and create thumbnails for use on a website.

You can get to the System Diagram tool from the side navigation inside the app.

Once there, you can view all your buckets, and any Lambdas, CloudWatch Logs, and CloudWatch Alarms that they are connected to.

Below you can see that our image bucket is connected to a Lambda which in turn is connected to a CloudWatch Log. You can also see that there are two CloudWatch Alarms setup to monitor the bucket.

You can also view and navigate to each of these services in the side navigation on the left of the app. Below you can see the bucket and information about the Lambda that is connected to it, along with the Alarms.

# Test Out The Connection

Testing out S3 events has been a difficult process up until now for developers. You could copy files in from the command line, or use an S3 viewer such as Transmit to save the file, but then seeing the Lambda and the logs that was invoked required either going to the AWS Console, or using a 3rd party logging service like Loggly or Sumo Logic, and logging into it, to view the logs.

Below you can see the actual code that is in our lambda for this example. It is quite basic, and the actual brains of the work to generate the thumbnail has not been implemented. But you can see a console.log statement that will then be viewable when you run the invocation.

    // Sample Lambda Invocation
    exports.handler = async (event) => {
        const response = {
            statusCode: 200,
            body: JSON.stringify('Image Received!'),
        };
    
        // TODO: implement image thumbnail creation
        
        // log out that the lambda was run
        console.log('inside the lambda');
    
        return response;
    };

Now you can test out this plumbing directly in Commandeer. Below you can see an example of the S3 Lambda Tester where a file has just been selected and pressing the 'Run' button will actually save the file to S3, and then show you the results of the subsequent Lambda invocation.

Click run, and then you will see the actual logs for your Lambda invocation.

The log statement 'inside the lambda' that we showed earlier can be viewed in the log.

# Conclusion

S3 to Lambda is one of the bread-and-butter use cases for serverless technology. Even if you are on a project that is not using serverless at the moment, setting up a Lambda to run when a file is created on S3, and doing some sort of work on the file is a great way to get your company thinking and working in a serverless mindset.

Happy Developing!

Last update: February 2, 2021 19:44