SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for AWS Amazon Web Services, Redshift, AWS Lambda Functions, S3 Buckets, VPC, EC2, IAM

Lambda Function Optimization for Performance and Cost


In this AWS Lambda tutorial, I want to introduce a tool named AWS Lambda Power Tuning for Lambda developers to optimize the Recommended Memory configuration parameter of their AWS Lambda functions so they can optimize performance and cost of their Lambda function executions. AWS Lambda Power Tuning open source tool executes the Lambda function by the payload you provide using a graphical user interface with varying recommended memory configurations and visualize the results for the developers to give a better decision on the performance and cost perspective.

AWS Lambda developers can find more details about the Lambda Power Tuning tool at project's GitHub page. For setting up the environment, developers or AWS cloud administrators can follow the step by step installation guide.

For testing the outputs of the open source tool, I worked with two AWS Lambda functions. One of them is translation Lambda function with input payload containing the text to translate and source/target languages. The second AWS Lambda function is a simple SNS notification sender application sending messages via Amason SNS from the Lambda function. By the way both functions are developed in Python. I shared the links which provide details about both functions within kodyaz.com

Here is the results and Recommended Memory amount for the notification sending AWS Lambda function via SNS.
You can see that the balanced tuning strategy provided me a suggestion to use 832 Mb of RAM for an optimization both on performance and cost.

AWS Lambda Power Tuning suggestion for SNS notification sender function

From the graph created by the results, developers can see that the best cost focused option is setting the RAM to 320MB On the other hand, it is visible that setting the RAM to 832 MB will not cause too much increase in cost but nearly completes the Lambda execution time in half.

At the end it is up to the developer based on which consideration, he or she will set the Lambda function configuration settings for Recommended Memory. But it is very worthy to have the figures that will create a baseline for the decision.

By the way, for AWS Lambda Power Tuning of the simple SNS function which I share the Python codes below, I got exactly 459 emails (due to my SNS topic subscription via email). So please keep it in mind, the optimizer tool will execute the Lambda function many times to provide you the figures about the affect of various memory settings on performance.

import json
import boto3

def lambda_handler(event, context):

   notification = "Here is the SNS notification message from sample AWS Lambda function"
   client = boto3.client('sns')
   response = client.publish (
      TargetArn = "arn:aws:sns:eu-central-1:111111111111:KodyazSNSLambdaFunction",
      Message = json.dumps({'default': notification}),
      MessageStructure = 'json'
   )

   return {
      'statusCode': 200,
      'body': json.dumps(response)
   }
Python Code Sample for AWS Lambda using Amazon SNS Service

And I have also configured a new Amazon SNS topic named KodyazSNSLambdaFunction and subscribed to the SNS topic via my email address.

As I said before, AWS Lambda developer can configure the Memory configuration based on the outcome and his/her optimization strategy on Lambda function's General Configuration section as below

AWS Lambda Memory settings in General Configuration section

The other sample Lambda function which I used AWS Lambda Power Tuning optimization tool to test is the translation function which is calling the AWS Translate function within Python codes which I share below
Previously I shared the source codes of the translate Lambda function and the API Gateway environment in AWS tutorial Amazon Tranlate Service with AWS API Gateway and Python Lambda Function
Developers can also refer to that tutorial for more details.

import json
import logging
import boto3
import os

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):

   translate = boto3.client(service_name='translate', region_name='eu-west-1', use_ssl=True)
   review_id = event["queryStringParameters"]['review_id']
   source_language = event["queryStringParameters"]['source_language']
   target_language = event["queryStringParameters"]['target_language']
   review = event["queryStringParameters"]['review']

   result = translate.translate_text(Text=review, SourceLanguageCode=source_language, TargetLanguageCode=target_language)

   print(result)
   logger.info(f'INFO:{result}')
   return {
      'statusCode': 200,
      'body': json.dumps(result.get('TranslatedText'))
   }
Python Code for calling Amazon Translate Service in AWS Lambda Function

Before running the AWS Lambda Power Tuning tool to decide on the memory configuration settings, I copied the ARN (Amazon Resource Name) of the AWS function, also I launched the test event configuration and copied the payload.

On the optimization tool, on Lambda Power Tuner UI, I provided these information (AWS function ARN and the payload)

AWS Lambda Power Tuner

The tuning suggestion which created by the AWS Lambda Power Tuner is as follows with the graph of execution time vs cost.

AWS Lambda Power Tuner cost and execution time optimization

As can be seen on the chart, there is a drop in execution time from 128 MB to 256 MB and the cost seems to be stable. After this amount of RAM the cost (blue line) seems to be increasing steadily. So it might be a good option to choose 256 MB for the recommendad RAM usage value of the AWS Lambda function which is under consideration

If you are interested in the tool, you can refer to AWS Lambda Power Tuner UI GitHub repo and follow the simple instructions for installing the tool on your AWS account easily.



AWS


Copyright © 2004 - 2024 Eralper YILMAZ. All rights reserved.