Home > Categories > Aws cdk > Lambda Cost Optimization With AWS Lambda Power Tuning

Home > Categories > Lambda > Lambda Cost Optimization With AWS Lambda Power Tuning

Home > Lambda Cost Optimization With AWS Lambda Power Tuning

Lambda Cost Optimization With AWS Lambda Power Tuning

Updated:

27 Jan 2021

Published:

27 Jan 2021

Before I found lambda turning I would just look at the cloudwatch logs for execution time.

And try different lambda ram values and read cloudwatch logs again to see if that changes the cost for single execution.

This is very manual process and doesn't necessarily generate accurate results.

Then I found lambda power tuning and with right settings my performance improved like crazy 🤪 without increasing much cost.

Before I never believed that increasing RAM capacity will actually save money 🤑.

I always thought 🤔 this is cloud company conspiracy 🤫. But now we have proof that it can save money 💰. So let's get started.

To read rest of the article please Purchase Subscription

View Source code on github

Before we get started I would recommend to develop CDK project inside development docker container.

So we no longer have the issue of "this works on my computer" 💻.

And it will not mess with your existing workflow. So checkout my blog post for detailed video 🎞️ explanation.

Then checkout CDK development container settings on github.

Pro Tip: Enable Subtitle

Power Tuning Serverless Application

View Source code on github

So there are couple of ways to optimize lambda cost 💵.

  1. AWS Compute optimizer
  2. AWS Lambda Power Tuning

AWS Compute optimizer uses artificial intelligence to find out perfect settings for your lambda.

This is provided at free of cost to you by AWS. All you have to do is to opt in to give them permission to track your lambda execution information.

And over time you will receive recommendation for lambda.

This started for EC2 but they now have extended support for lambda.

You may not receive recommendations immediately. There can be few reasons.

  1. You haven't generating enough lambda execution information to generate meaningful recommendation.
  2. They only show recommendation if you can significantly save cost or significantly speed up your lambda.
  3. Lambda support is relatively new and not available in your region.

The last reason will soon become obsolete. As compute optimizer becomes available in all regions.

We are actually going to focus on using Lambda power turning in this article.

So Lambda power tuning is available on Serverless Application Repository.

And you can deploy with single click but we like CDK very much so we are going to deploy with CDK.

So in your CDK project install @aws-cdk/aws-sam package. Then deploy the power tuning as follows.

The application id and semantic version I got it from serverless app repo page.

You will need to keep track of change log and update semantic version with latest value.

With first parameter we are giving this serverless application access to all of our lambda functions. Without this access you cannot test lambda performance.

Now the power values are not necessary. We are going to override this power values while running actual tests.

But in case we forget to override it then these are fallback values.

Then time out in minutes is also fallback value. If you have made mistake and function executes recursively then we need to stop it after certain period.

If you think 15 minutes is too large then feel free to change it.

You will have access to stop execution button while running test so you can stop tests anytime you want. You don't need to wait till timeout is reached.

After you deploy this application then rest of the things we are going to do on the console.

If you open step function section on console then you will see new item there.

We are going to pass in following input.

Lambda arn
arn of lambda that you want to test
Power values
we override power values deployed at build time
num
Number of time lambda will execute per power value. e.g. in above example it will run lambda 50 times. i.e. 10 times for each of five power values.
Payload
payload sent to test lambda function
Parallel invocation
by defaults this is set to false. I am changing to true because this specific lambda doesn't create any side effect like creating record or deleting record.
Strategy
there are 3 different strategies
Cost
This is default strategy if you don't provide value. The test will try to find cheapest execution cost for given lambda function.
Speed
This will find fastest execution ram setting for given lambda function.
Balanced
This will try to balance between cheapest cost and fastest speed strategy.
Balanced Weight
This is only applicable if you set strategy to balanced. 0 is speed strategy 100% and 1 is cost strategy 100%. I have set it to right in the middle to 0.5. The default value is also 0.5 if unspecified.
Auto Optimize
If this is set to true then after test it will automatically apply the result settings to lambda. By defaults its set to false. So you don't have to write this. This is useful if you deploy lambda function with CI-CD pipeline. So after you deploy lambda function you can run your tests and based on results automatically apply settings to lambda.

This is just short intro you can see the rest of it in the video at the top.

View Source code on github.

Free users cannot comment below so if you have questions then tweet me @apoorvmote. I would really like to hear your brutally honest feedback.

If you like this article please consider purchasing paid