How to calculate lambda cost??

0

So I am implementing a pay-for-your-usage pricing model in one of my projects. Here, I will be using AWS Lambda for processing for user (i will invoke this lambda from my server). structure will be like: client -> serverless expressjs -> lambda (i need to calculate usage of this lambda invoke)

so i need to calculate how much bandwidth a lambda invocation have used and what is per mb bandwidth cost. then i also need to calculate total second of computation and per second computation rate.

Then, according to it, I will deduct credits from the user's total credit.

chetaN
asked 3 months ago164 views
3 Answers
1
Accepted Answer

Lambda charges you for number of invocations and duration. To find the duration, In CloudWatch logs, after each Lambda invocation, there is a REPORT line that includes the billed duration. If you multiply that by the amount of memory you allocated to the function, you will get the GB-s value that you need to calculate the cost (based on region). E.g, if the report line indicated that the billed duration is 2 seconds, and you allocated 1.5 GB for the function, the function will be billed for 3 GB-s. To that you need to add the number of invocation.

You indicated that you invoke the function from serverless express.js. What does it mean? Where is that running? Do you mean that you are running express.js in the function? Is it running somewhere else? Are you using API Gateway?

profile pictureAWS
EXPERT
Uri
answered 3 months ago
profile pictureAWS
EXPERT
reviewed 3 months ago
  • So I have a normal Express.js app running in a Lambda function and using API Gateway, just to save costs of EC2. Additionally, I have another separate Lambda to handle users' ffmpeg-related tasks, which can be long-running, taking anywhere from 30 seconds to 5 minutes easily.

    I will provide an Express.js API to clients, which they will call from their code with their API key attached in the request (my service is for developers). Then, in my Express.js app, I will check their API credits and invoke another Lambda for their ffmpeg-related task. In the end, it will return a S3 URL.

    Since this ffmpeg task Lambda is long-running, I want to calculate its cost when a user invokes it and then update their total credit in the database.

  • Note that if you invoke the second function in a synchronous manner, you will also pay for the duration of the first function (as it waits for the second to complete) and also, API Gateway has a maximum timeout of 30 seconds, so your client will get an error.

    Also, the REPORT line is emitted only after the function completes. If you need to check if the user has enough credit, you will need to estimate the cost prior to the invocation.

    I saw that you want to calculate the cost immediately when the function is done. For that you will probably need a Lambda extension to get the information in the function itself. Otherwise, you will need to subscribe to CloudWatch Logs to calculate the cost asynchronously. You can always just meter the time it took to run the function and do an approximate cost.

  • Oops, I forgot that the API Gateway has a timeout limit. Now i think instead of using an Express.js server as a middleware, I need to handle this cost calculation inside the FFmpeg Lambda itself. I will start a timer before the FFmpeg task and then end it after FFmpeg finishes. Then, I will calculate the cost and update it in the database. I will use a normal function URL for ffmpeg lambda instead of the API Gateway. This way, users will directly call the FFmpeg Lambda.

    Is there any other better way to do this?

  • Using function URL is one way of doing that. You will need to handle the auth, as your function will need to be public (unless you can use IAM auth, which means that your clients need to have AWS credentials).

    An alternative might be to use asynchronous methods, i.e., API GW -> express -> ffmpeg. Express invokes your ffmpeg function asynchronously and immediately returns. You will then need to notify the client when the processing is done. You can do that with a websocket, mobile push notification, using polling, etc.

1

Hello.

I thought that setting cost allocation tags to Lambdas would make it easier to understand the charges for individual Lambdas.
https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/cost-alloc-tags.html

profile picture
EXPERT
answered 3 months ago
profile pictureAWS
EXPERT
reviewed 3 months ago
  • In my requirement, I need to calculate this cost in main server just after the lambda has finished executing. Everything will be done automatically. I will calculate the cost and then deduct that amount from the user's total credit.

1

Hi,

In addition to Riku's proposal re. CPU, for network bandwidth, you want to use Lambda Extensions to intercept calls to Lambda and not the size of the payload of request and response to aggregate them for your accounting purposes.

See https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html

Best,

Didier

profile pictureAWS
EXPERT
answered 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions