The primary focus of this post is to demonstrate how to trigger an AWS Lambda function when a new object is placed into an S3 bucket. The following post will demonstrate how to use a Lambda function to create a tweet when a new post is available. This was a larger topic than I originally intended so I will separate it into two parts.
Serverless architecture/computing seems to be a fairly buzzword-worthy topic today. In this post, I’m going to begin to demonstrate how to use this technology to improve your software delivery. Before doing that, if you’re not familiar with serverless architecture/computing, I’ll do my best to give a brief overview.
Serverless computing is a way to execute code without having to maintain servers yourself; the cloud provider is maintaining those resources and you’re submitting jobs to them that will run based on your criteria. This can be event driven or time-based, it’s up to you. There are several commercial providers of this type of service and they include AWS Lambda (used here) and Azure Functions. This is just another way to help deliver your product or project without having to maintain your own servers, whether they are in the cloud or on premises.
As we build our site and business, we are striving to maintain a constructive social media presence. To that end, we want to notify our clients and anyone else interested when a new post is available. There are several ways to do that but we decided to start with automating our tweets. We didn’t want to create an application and run a container or compute instance just to do this task so we decided to create a Lambda function. Because we use S3 to host our site, we have the ability to generate events when actions are performed on the bucket.
When a new post is uploaded to S3, an event is created that notifies a Lambda function directly. We could have chosen to use Simple Notification Service (SNS) but instead chose to directly notify the Lambda function. Plus, we can add that later when needed.
If you look closely at the event structure that is linked to above, you can see there is detailed information provided; we know exactly what was added and don’t need to query S3 and somehow determine what post is new because that event object will be provided to the function at runtime.
Below is an example of how to create an event. This was created in the Events section in the Properties tab of the bucket that holds the site. Amazon provides a tutorial on how to accomplish this as well so be sure to review that for details.
Once an event is received, the Lambda function will execute, but only when it receives that event notification. This means you won’t be consuming resources (consequently you won’t be paying for resources) the rest of the time like you would if you were maintaining the service yourself.
At this point we could update a database, write to a topic or queue to be consumed by other services, or as mentioned above, send a tweet. Admittedly this is a simple use case but the idea of serverless computing can even be extended much further to things like web servers. Zappa is an example of a project to do just that.
In the next post, we’ll review the Lambda setup and the function itself. Thanks for reading and be sure to follow us on Twitter
or subscribe. Well, no subscriptions yet. We will need more event handlers for that.