Leveraging only three out of AWS Serverless’ services, one can create a production ready application smoothly. These services are AWS Lambda, Amazon API Gateway and Amazon DynamoDB and will be explained further in the following.
AWS Lambda functions offer a fast and easy way to build, deploy and maintain small service units. In contrast to EC2 instances or Kubernetes containers, a lambda function does not run continuously. Rather, it gets triggered by some sort of event or request. Which also means that the price model is tied to how often a lambda function is triggered and how long it runs after being triggered (with the free tier option, 1.000.000 requests per month are for free as well as 400.000 GB-seconds of compute time per month, for more information about AWS Lambda’s pricing model, see: https://aws.amazon.com/lambda/pricing/) which makes AWS lambda a cost efficient option to deploy microservices. While a lambda function runs on-demand, scaling the lambda function is managed by AWS. One thing to keep in mind though, is that there are certain limits to lambda functions. These include the following:
- triggering up to 1000 concurrent lambda functions per AWS region (anything higher than that will result in throttling; however, limit increase can be requested)
- each lambda function cannot run longer than 15 minutes (note that this fits the scope of a microservice nicely)
- the size of the compressed zip file to upload to the lambda function must not exceed 50 MB
All that is needed to deploy a service using a lambda function, next to an AWS account and necessary permissions, is to create the lambda function with a valid runtime (including Golang, Python, Java runtimes and many more. For all current runtime options see: https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html) and upload the code, which the lambda should execute, to the function. The code needs to be a compressed zip file and can be uploaded directly or picked from an Amazon S3 location. Within that code, a handler function is needed, which will serve as the entry point of the application. The above steps can either be done via the AWS Management Console, AWS CLI or frameworks like AWS Serverless Application Model (SAM) or the Serverless Framework.
Using Amazon API Gateway and Amazon DynamoDB along with AWS Lambda is a fitting choice as all these services are serverless and integrate seamlessly (see figure 3). With Amazon API Gateway you can add authentication and authorization, caching, API versioning as well as rate limiting to your services. It offers a great way to enhance security, manage different environments as well as manage traffic to your services. There is no charge for the API Gateway but for the traffic that goes through it. An API Gateway exposes the REST API provided by a lambda function and can easily invoke the function on incoming requests. Authentication for your service can be smoothly added by enabling authentication through either
- IAM roles
- Amazon Cognito
- Lambda Authorizer (custom authorization through a dedicated lambda function)
Another benefit is that you can deploy different versions of a service by deploying to different stages, where each stage can have its own configuration parameters and can be rolled back while keeping a history of deployments (useful for managing e.g. dev, test, prod environments). Another nice addition to Amazon API Gateway is that you can create usage plans for your customers that define who can access an API as well as how fast and often the API can be accessed per customer adding an additional layer of security to the service.
Either one central API Gateway can be used in front of all microservices of an application or a dedicated API Gateway per microservice can be setup, enabling more fine-grained configurations on a per service basis.