More and more companies are migrating to the cloud and adopting serverless technologies.
It's not only important to write efficient code but also to choose the right programming language for the job.
Lambda pricing model is based on the amount of memory it consumes as well as the duration of running your code.
If you choose the wrong programming language, it can significantly impact the cost of your infrastructure.
importosimportdatetimeimportrandomimportboto3bucket=os.getenv('BUCKET_NAME')key='thumbnail.png'table='Meta's3=boto3.client('s3')dynamodb=boto3.resource('dynamodb')# Download the S3 object and return last modified datedefget_s3_object(bucket,key):object=s3.get_object(Bucket=bucket,Key=key)object_content=object['Body'].read()print(object_content)returnobject['LastModified']# Save the item to the DynamoDB tabledefsave(table,date):table=dynamodb.Table(table)table.put_item(Item={'LastModified':date.isoformat()})# Lambda handlerdeflambda_handler(event,context):date=get_s3_object(bucket,key)random_number_of_days=random.randint(0,100000)date+=datetime.timedelta(days=random_number_of_days)save(table,date)
constaws=require('aws-sdk');consts3=newaws.S3();constddb=newaws.DynamoDB({apiVersion:'2012-08-10'});// Download the S3 object and print contentconstgetS3Object=(bucket,key,callback)=>{constparams={Bucket:bucket,Key:key};s3.getObject(params,(err,data)=>{if(err)returnerr;console.log(data);callback(data.LastModified);});};// Save the item to the DynamoDB tableconstsave=(table,date,callback)=>{constmodifiedDate=date.toISOString()constparams={TableName:table,Item:{'LastModified':{S:modifiedDate}}};ddb.putItem(params,(err,data)=>{if(err)returnerr;callback(null,200);});};// Returns a random number between min and maxconstgetRandomInt=(min,max)=>{returnMath.random()*(max-min)+min;};exports.lambda_handler=(event,context,callback)=>{constbucket=process.env.BUCKET_NAME;constkey='thumbnail.png';consttable='Meta';getS3Object(bucket,key,(date)=>{constrandomNumberOfDays=getRandomInt(0,100000);date.setDate(date.getDate()+randomNumberOfDays);save(table,date,callback);});};
# Generate a random string to create a unique S3 bucketresource"random_pet""lambda_bucket_name"{prefix="lambda"length=2}# Create an S3 bucket to store lambda source code (zip archives)resource"aws_s3_bucket""lambda_bucket"{bucket=random_pet.lambda_bucket_name.idforce_destroy=true}# Disable all public access to the S3 bucketresource"aws_s3_bucket_public_access_block""lambda_bucket"{bucket=aws_s3_bucket.lambda_bucket.idblock_public_acls=trueblock_public_policy=trueignore_public_acls=truerestrict_public_buckets=true}
# Generate a random string to create a unique S3 bucketresource"random_pet""images_bucket_name"{prefix="images"length=2}# Create an S3 bucket to store images for benchmark testresource"aws_s3_bucket""images_bucket"{bucket=random_pet.images_bucket_name.idforce_destroy=true}# Disable all public access to the S3 bucketresource"aws_s3_bucket_public_access_block""images_bucket"{bucket=aws_s3_bucket.images_bucket.idblock_public_acls=trueblock_public_policy=trueignore_public_acls=truerestrict_public_buckets=true}# Upload test image to S3 bucketresource"aws_s3_object""image"{bucket=aws_s3_bucket.images_bucket.idkey="thumbnail.png"source="../thumbnail.png"etag=filemd5("../thumbnail.png")}
To conclude, Python's function duration time is almost 30% less than Node.js.
Since lambda charges you by duration, you can potentially save up to 30% simply by choosing Python language over Node.js to perform exactly the same thing.
If you want to find out the results of the benchmark test between Golang and Node.js, you can watch this video.