2. CONTENT
What is AWS Lambda?
Advantage and Disadvantage of AWS Lambda
Supported programming languages
AWS Lambda Function (Node.js)
AWS Lambda invoke
Create a Lambda Function
Pricing
References
3. What is AWS Lambda?
AWS Lambda is an event-driven, serverless computing platform provide by
Amazon as part of the Amazon Web Services.
It is a compute service that runs code in response to events and automatically
manages the compute resources required by that code.
It was introduced in 2014.
4. Advantage and Disadvantage of AWS
Lambda
Advantages:
Only pay for the compute time(in milliseconds) used.
Don’t worry about server, spend more time in writing code.
Disadvantages:
Lambda is not well documented.
Lambda is a building block, not a tool.
Lambda is terrible at error handling.
6. AWS Lambda Function (Node.js)
AWS Lambda function handler has 3 parameters.
event(first parameter): AWS Lambda uses this parameters to pass data into handler. «event.variableName»
context(second parameter): AWS Lambda uses this parameters to provide your handler the runtime information of the
Lambda function that is executing. While AWS Lambda function is executing, it can be contact runtime informations.
Such as:
How much time is remaining before AWS Lambda terminates your Lambda function (timeout is one of the Lambda
function configuration properties).
The CloudWatch log group and log stream associated with the Lambda function that is executing.
The AWS request ID returned to the client that invoked the Lambda function. You can use the request ID for any follow up
inquiry with AWS support.
If the Lambda function is invoked through AWS Mobile SDK, you can learn more about the mobile application calling the
Lambda function.
7. Con’t
context(second parameter) can access this runtime information functions.
callback(third parameter): You can use this optional function to return information to the caller, otherwise return
value is null. Callback function has two parameters.
« Error error » is an optional parameter that you can use to provide results of the failed Lambda function execution. When a
Lambda function succeeds, you can pass null as the first parameter.
« Object result » is an optional parameter that you can use to provide the result of a successful function execution. The result
provided must be JSON.stringify compatible. If an error is provided, this parameter is ignored.
9. AWS Lambda invoke
You can call another lambda function in your lambda function. Remember that
your lambda function must has a permisson from AWS IAM(Identity and Access
Management)
Firstly you can access aws-sdk and lambda object.
Then you can call another function by using invoke function.
10. Create a Lambda Function
You must have an AWS account. (can be free tier)
Open AWS Lambda page.
Then click Create a Lambda function.
Select a blueprint. (You can select Blank Function or etc.)
Configure Trigger
You can select a trigger that invokes your lambda function.
These triggers can be: API Gateway, AWS IoT, CloudFront, CloudWatch Events-Schedule, CloudWatch Logs, CodeCommit, Cognito Sync Trigger, DynamoDB, Kinesis, S3, SNS.
Click Next.
You must enter a name for your lambda function.
You must select a programming language from Runtime text block.
You can enter your code by selecting Edit Code Inline from Code Entry Type. Otherwise you can upload a .zip file(import your code) or upload file from AWS S3.
You can enter handler name or default index.handler (it calls export.handler in your code)
You must select a role. Role can be created or use existing one. You can select one of them. These roles could be defined in IAM(Identity and Access Management)
You must determine memory and timeout for your lambda function. Your function is allocated CPU and memory.
Then you can click Next and Create Function.
12. Con’t
You can test the code written. And you can see the execution results.
13. Pricing
The Lambda free tier includes 1M free requests per month and 400,000 GB-seconds of compute time per month.
First 1 million requests per month are free
$0.20 per 1 million requests thereafter ($0.0000002 per request)
Duration is calculated from the time your code begins executing until it returns or otherwise terminates, rounded up to the nearest
100ms. The price depends on the amount of memory you allocate to your function. You are charged $0.00001667 for every GB-second
used.
Example:
128MB of memory, executed 25M times in one month, runs for 200ms each time
Total compute (seconds) = 25M * (0.2sec) = 5M seconds
Total monthly compute usage (GB-s) = 5M seconds * (128MB/1024) = 625,000 GB-s
The monthly compute price is $0.00001667 per GB-s and the free tier provides 400,000 GB-s.
Monthly charged compute usage = Total monthly compute usage – Free tier usage
Monthly charged compute usage = 625,000 – 400,000 = 225,000 GB-s
Monthly compute charges = 225,000 * 0.00001667 = $3,75075
Cheap? yeap