<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Matthias Sondermann</title>
    <description>The latest articles on DEV Community by Matthias Sondermann (@matzeso).</description>
    <link>https://dev.to/matzeso</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F223654%2Fc70766fb-7583-4e2a-b1ed-fc1784878de1.jpg</url>
      <title>DEV Community: Matthias Sondermann</title>
      <link>https://dev.to/matzeso</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/matzeso"/>
    <language>en</language>
    <item>
      <title>How we solved AWS Lambda timeouts... and improved performance</title>
      <dc:creator>Matthias Sondermann</dc:creator>
      <pubDate>Wed, 06 Oct 2021 10:08:07 +0000</pubDate>
      <link>https://dev.to/matzeso/how-we-solved-aws-lambda-timeouts-and-improved-performance-581i</link>
      <guid>https://dev.to/matzeso/how-we-solved-aws-lambda-timeouts-and-improved-performance-581i</guid>
      <description>&lt;blockquote&gt;
&lt;h5&gt;
  
  
  TL;DR
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;CDK sets default lambda memorySize to only 128MB&lt;/li&gt;
&lt;li&gt;CPU power of lambda scales with memorySize&lt;/li&gt;
&lt;li&gt;Node needs quite some RAM&lt;/li&gt;
&lt;li&gt;Performance of Lambdas can be improved considerably by increasing memorySize&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Our AWS tech stack
&lt;/h4&gt;

&lt;p&gt;One of our microservices uses a Node-v14 runtime and runs on AWS Lambda using AWS API Gateway.&lt;/p&gt;

&lt;p&gt;We build the infrastructure with &lt;a href="https://aws.amazon.com/de/cdk/"&gt;AWS CDK&lt;/a&gt;, which is a &lt;code&gt;Infrastructure as Code&lt;/code&gt; (IaC) framework to create AWS resources, and use &lt;a href="https://aws.amazon.com/de/serverless/sam/"&gt;AWS SAM&lt;/a&gt; with its CLI to do local development.&lt;/p&gt;

&lt;p&gt;This local development unfortunately is far from resembling the cloud environment, which has caused us already quite some trouble (thinking of CORS handling, binary media types, DynamoDB permissions, ...), and now we had another issue with a new Lambda even though it ran fine locally in a couple of seconds.&lt;/p&gt;

&lt;h4&gt;
  
  
  The lambda timeout... again
&lt;/h4&gt;

&lt;p&gt;When investigating why the newest function did not work as expected, we realized through the CloudWatch Logs, that the lambda timed out after 60 seconds. &lt;br&gt;
That is way too long for our lambda, which was just fetching some data from DynamoDB and creating and storing some of it in Google Sheets. So we spent quite some time figuring out whether there were configuration issues with DynamoDB or the newly introduced &lt;a href="https://www.npmjs.com/package/googleapis"&gt;googleapis&lt;/a&gt; had any problems connecting.&lt;/p&gt;

&lt;p&gt;But everything was setup correctly - so what could it have been? At the very end, we realized that we had been using all of the available &lt;code&gt;128 MB&lt;/code&gt; RAM allocated to the lambda, you can see that in your CloudWatch Logs in the last &lt;code&gt;REPORT&lt;/code&gt; entry. &lt;/p&gt;

&lt;h4&gt;
  
  
  Increasing the memory size
&lt;/h4&gt;

&lt;p&gt;128 MB seemed rather low anyway, so while looking up how pricing scaled up, I also found out that &lt;em&gt;lambda CPU power scales proportionally with memory size&lt;/em&gt; (&lt;a href="https://aws.amazon.com/de/about-aws/whats-new/2020/12/aws-lambda-supports-10gb-memory-6-vcpu-cores-lambda-functions/#:~:text=AWS%20Lambda%20customers%20can%20now,previous%20limit%20of%203%2C008%20MB.&amp;amp;text=Since%20Lambda%20allocates%20CPU%20power,to%20up%20to%206%20vCPUs."&gt;see this&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;So it was about time to give that Lambda some more power to play with. By simply increasing the &lt;code&gt;memorySize&lt;/code&gt; from &lt;code&gt;128&lt;/code&gt; to &lt;code&gt;384&lt;/code&gt; the functions were running fine, and even saved us money, since they ran a lot faster. A simple function that just created an entry in DynamoDB now ran &lt;strong&gt;4x faster&lt;/strong&gt; than before, so overall being even cheaper than the previous, slow and low RAM version.&lt;/p&gt;

&lt;p&gt;Now that we have this in mind, we are experimenting with different memory sizes to see what fits best in terms of performance/price for our use case.&lt;/p&gt;

&lt;p&gt;We never had issues with this prior, because our previous services were built with &lt;a href="https://www.serverless.com/"&gt;Serverless Framework&lt;/a&gt; which sets a default memory size of &lt;code&gt;1024 MB&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For CDK, you can set &lt;code&gt;memorySize&lt;/code&gt; as a property in your &lt;code&gt;lambda.Function&lt;/code&gt; &lt;a href="https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Function.html#memorysize"&gt;constructor props&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>lambda</category>
      <category>cdk</category>
      <category>api</category>
    </item>
  </channel>
</rss>
