You are here reading this blog.. I hope you have selected or about to select AWS Lambda for your application development. If yes, you are thinking about how effectively the event-driven Lambda, Function As a Service (FAAS), serverless computing platform could be used as the pricing model is not so conventional – It is pay per use. Then it is time to have a look at my analysis on AWS Lambda… Looks good.
All we as enterprise product developer want is how better (one of the major criterias – cost effectiveness) a tool/ platform could be utilized for the betterment of our product. So AWS Lambda is not exceptional from this analysis.
“With AWS Lambda, you pay only for what you use. You are charged based on the number of requests for your functions and the time your code executes”.
We have seen the above line in AWS Lambda pricing documentation. AWS mentions clearly that “you pay only for what you use”. Do you really know how much your Lambda function uses so that you can pay? If you are not aware of that, it will cost you more.
I have asked this question “Are there any metrics which would be helpful to get me the answer?” I doubt if I found one. So I would like to take you in the path I travelled to figure this out.
Proof Of Concept:
AWS Lambda – Implemented a simple function in java. It does, posting employee details into AWS RDS. Then I have created a deployment package and tested it in my local machine (using aws-sam-local). Since everything went good so far, created a function in AWS Lambda console by uploading the deployment package and allocated 512 MB memory. Then have created REST API using API Gateway and deployed in stage environment.
Wanted to benchmark my Lambda function invocations; so with the help of jmeter invoked the function 2040 times for 3 hours as shown below.
Observed the logs and metrics in cloudwatch. It was giving me few metrics but I wanted to dive in further with respect to number of executions, I have looked at couple of options and found Sumologic to visualize logs with minimal effort.
In function logs you can see lines as shown below.
2017-11-14T17:42:13.536Z REPORT RequestId: 2180bde0-c963-11e7-94e0-21fbfba70e99 Duration: 7180.67 ms Billed Duration: 7200 ms Memory Size: 512 MB Max Memory Used: 67 MB
In Sumologic, the below table is shown for the logs where in which you can execute queries to visualize the data.
Here are my observations
- First invocation took more time to respond back. As it has to start a docker container and create a database connection pool with the configured AWS RDS. From next time onwards, it started reusing the connections.
- If there are no further invocations for some time say 30 mins, next first invocation takes almost the same time as that of first invocation as it destroys the container and relevant resources and recreates it.
- For each invocation, it gets rounded up to nearby 100ms as shown below.
- Try to avoid using heavy frameworks like spring boot as it takes good amount of time to load.
- Don’t add unused libraries into deployment package – Keep your deployment package as thin as possible.
Function memory usage :
Per requestid, plotted memory usage of my function against the allocated memory.
It is so explicit that the memory which i allocated to function was very high than it required. It was taking average memory of 71 MB. But i should pay compute cost $2.681/month for 2040 invocations.
Then, I allocated 120 MB memory for my function. Compute cost $0.62/ month for 2040 invocations. I have reduced the cost to the company by $2.061 / month for 2040 invocations.
Note : Didn’t minus free tier cost from total cost, since my invocations (In POC) were lesser than 1M.
- Come up with an approximation of number of executions that you think could happen in a month
- Then benchmark your result so that you can configure your resources properly to well utilize your cost.
AWS Lambda Pricing
I got this information from AWS Lambda pricing documentation.
Request(s) cost :
- First 1 million requests per month are free.
- $0.20 per 1 million requests thereafter ($0.0000002 per request).
- Total requests – Free tier requests = Monthly billable requests
- Monthly request charges = Monthly billable requests (in Million) * $0.2/Million
Compute cost :
- 400,000 GB-seconds of compute time per month are free.
- $0.00001667 for every GB-second used.
- Total compute (seconds) = Total requests * (Duration in seconds)
- Total compute (GB-s) = Total compute (seconds) * Memory size in MB /1024 MB
- Monthly billable compute GB- s = Total compute (GB-s) – Free tier compute (GB-s)
- Monthly compute charges = Monthly billable compute GB-s * $0.00001667 GB-s
Other cost (if any) :
- Data transfer.
- Ex : Read / Write requests of S3 and DynamoDB.
- If any.
Total cost :
- Total cost = Request(s) cost + Compute cost + Other cost (if any).
Note: The AWS Lambda free tier (Non-expiring Offers) includes 1M free requests per month and 400,000 GB-seconds of compute time per month.