Real Microservices with AWS Lambda
January 5, 2016
Lately, I started working on my first Cloud project, which runs exclusively on Amazon AWS resources. I read before many things about the AWS cloud and played some little time with this and that, but made no real experience. Now I was forced to - and made a great experience!
A very special use case led me to AWS Lambda. I had the requirement to execute a function on a scheduled basis. For this, running a whole server, no matter if EC2, Elastic Beanstalk, Docker, or whatever, seamed to me too much oversized (in infrastructure and money). There’s just this small function, calculating some data, interacting with a persistence layer and sending some messages depending on the data. First, I was thinking about starting and stopping an EC2 instance on a schedule. Then, a co-worker told me about Lambda. This was exactly what I was looking for: just deploying code and functions (no whole instances) and executing them event-driven.
As I already said, the functions will be executed based on events. And there are a lot of possible events available:
- S3 events, such as upload and download of files
- received emails/messages/notifications via SNS and SES
- DynamoDB events, as data is modified in any way (like a DB trigger)
- events based on CloudWatch logs and metrics
- based on data streams of Kinesis
- scheduled events, based on cron-expressions
- manually triggered from the command line
- and some others
All executions of your functions can be monitored with CloudWatch, where the logs and metrics of your services are available.
If you have the need to call your functions via HTTP - or as REST API call, you can add one or more API endpoints to your Lambda code, whether additionally to the events, or just instead of events, then the API call is your event. This is made possible with AWS API Gateway, which is not only available for Lambda, but as well for many other AWS services.
With this Lambda approach, it’s possible to deploy real microservices, acting on events and on API calls. But every service just has one purpose, not more! And because the AWS APIs are available in general, Lambda makes it easy to use the AWS infrastructure and not depending on too much 3rd party code.
In case you need it - there’s a tutorial availabe.