Serverless is the new talk of the town. Serverless essentially means running code without servers or running code on servers which don’t have to provisioned in advance. The user only pays for the amount of CPU and RAM used for running the code and not for the entire time the server was running.
In a server world, to run code we first provision a server. A server can cost from anything between USD 3.50 (on AWS LightSail) to several 100 dollars for a large EC2 instance. After that code is deployed on the server. The user pays for the entire month, it doesn’t matter if the server is processing any requests or not.
How does Serverless work (AWS Lambda)?
AWS Lambda is the de facto leader and torch bearer in the serverless world. In the serverless world the code is executed only when it receives a request. AWS has a large number of shared servers which execute customer’s lambda requests.
So when a request to a lambda function is received then AWS first deploys this code on their cluster and then it executes the code. After the execution, the response is returned.
The above might look a bit hectic and may appear as if serverless functions are slow. But AWS has spent a lot of time in optimizing AWS Lambda. A simple function has latency less than 10 ms.
Deploying code to AWS Lambda.
You can use the UI to deploy code and test a lambda function. However, the recommended way is to use the AWS Serverless Application Model or AWS SAM. SAM is a lot like AWS
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: A starter AWS Lambda function.
Parameters:
IdentityNameParameter:
Type: String
Resources:
helloworldpython3:
Type: 'AWS::Serverless::Function'
Properties:
Handler: lambda_function.lambda_handler
Runtime: python3.6
CodeUri: .
Description: A starter AWS Lambda function.
MemorySize: 128
Timeout: 3
Policies:
- SESSendBouncePolicy:
IdentityName: !Ref IdentityNameParameter
AWS let’s you create a sample SAM application with a few clicks. You can also
This is the sample python code for “Hello World”:
import json
print('Loading function')
def lambda_handler(event, context):
#print("Received event: " + json.dumps(event, indent=2))
print("value1 = " + event['key1'])
print("value2 = " + event['key2'])
print("value3 = " + event['key3'])
return event['key1'] # Echo back the first key value
#raise Exception('Something went wrong')
What about APIs? Or remote invocation?
AWS Lambda is inside AWS’s account and can not be invoked from outside world. To invoke lambda from an endpoint, it needs to integrated with API Gateway. API Gateway is used to filter, structure and pass all requests it received to AWS Lambda. API Gateway supports swagger models as well which is part of Open API initiative.
After AWS lambda’s success, various companies have launched their own serverless systems. Users have a wide variety of services to chose from. Users can run their serverless code on Microsoft functions, IBM’s serverless, Google functions or even run serverless node.js functions on Ethereum’s blockchain.
Comments
0 comments