
In our previous post we saw some of the fundamentals of AWS and an introduction on IAM and Lambda. In this post we will see more about Lambda, CloudFormation and API Gateway.
Advantages of Lambda:
The best thing about Lambda is that you don’t have to manage the servers or the infrastructure to run your code smoothly. Amazon will take care of every other issues. It essentially means that you don’t have to think about issues such as security updates for your OS, scalability, availability. It doesn’t matter if you have n number of users who hit your function. No slow responses or downtime due to overwhelming customer engagement. Also, you only pay for the time your functionality runs. This reduces the costs of your application considerably greater.
They analyzed one production ready app (16000 requests / day @ 200ms avg. response time) and calculated how much it would cost when hosting it with EC2 in contrast to lambda.The results are astounding:
Two EC2 instances $2.97 / day
Lambda $0.05 / day
This difference pretty huge, and imagine the amount earned if implemented in a large scale.
Different runtimes
Lambda supports different runtimes as well. As of now, Node.js, Python and Java are supported. This means that you can develop different parts of your application with different programming languages. Maybe your user management is better realized with Python while your other team works on a Node.js / JavaScript implementation of your users status updates. No problem! All can be done perfectly without much of a confusion.
What is CloudFormation?
Back in the starting days of AWS you would login to your AWS account and configure everything manually. You’ll have to create the necessary resources like S3 buckets, policies and users / roles and then orchestrate them to make it functional. The problem with this approach is that nothing is (obviously) automated. You can’t share and recreate an infrastructure from scratch without spending a huge amount of time configuring everything. CloudFormation was developed by Amazon to overcome these limitations.
How does it look like?
Here’s a simple example:
{
“Version”: “2012-10-17”,
“Statement”: {
“Effect”: “Allow”,
“Action”: “s3:ListBucket”,
“Resource”: “arn:aws:s3:::example_bucket”
}
}
The CloudFormation templates are defined with the help of JSON. This makes it quite easy, accessible and understandable if you have experiences with JSON. It also makes it very easy to interact with the templates in your favourite programming language. If you have no experience with JSON you can make use of the AWS CloudFormation designer to design your templates in your browser. The designer generates the JSON which you can then use for further processing.
Note: The usage of CloudFormation is free, but you’ll have to pay for the resources you’ll define in your template.
What is API Gateway?
API Gateway is another great service available through AWS. Nearly everyone who has developed an application has to face the task to defining an API. Now the bigger question is what is an API in the firsthand?
An API (application programming interface) is a service you’ll develop so that other users / applications can access certain data of your application. Imagine that you have developed the next social network and you want to enable other developers to use the data of your network to build cool new applications around them. You can expose the content with different markup languages (the most famous ones are XML or JSON).
Now the question is How does API Gateway helps you in this.
As soon as you have started to develop your API you might have encountered hurdles. What about versioning? Should it be RESTful? How do I implement a RESTful API? How do I handle different stages (development, testing, production)? These are the innumerous questions that ponders at this stage.
API Gateway is Amazons answer to target those pain points. It makes it easy to version APIs, monitor events (like usage of a certain API method), scale the API independently, roll out APIs for different stages and create RESTful services.
How Serverless uses the API Gateway
The Serverless framework uses API Gateway to help you setup routes to your Lambda functions. You can e.g. perform a GET request and fetch all related data which is then available in your related Lambda function. The API Gateway is the trigger which causes your Lambda function to run.
In the upcoming posts we will see in detail about the S3 and DynamoDB.