Top best practices you should follow when building with AWS Lambda

George Mao
4 min readMar 10, 2023
Photo by Ales Krivec on Unsplash

Set CloudWatch log retention policies

Lambda creates a log group that matches the name of your function in this format: /aws/lambda/[lambda name]. All log streams in this group persist forever by default. You pay for every byte written to the log group. These costs pile up forever (at $0.53/GB) unless you set a retention period.

Set low retentions in dev environments and be aggressive in load test environments where many logs are generated. Set your productions logs to match your business requirements.

Optimize your function’s memory

Lambda bills you based on the memory you configure, not the memory your function uses! Every invocation of your function generates logs that includes a REPORT. Here’s an example:

REPORT RequestId: 8a040ba5-2add-45b5-a178-ba7d0880f90b Duration: 20.29 ms Billed Duration: 21 ms Memory Size: 128 MB Max Memory Used: 88 MB

Pay attention to Memory Size and Max Memory used. In this case, I configured memory to 128MB and this invoke used 88MB. I’m billed at 128MB. See James Beswick’s blog post for a bunch of useful Cloudwatch Insights Queries that can help you review memory usage at scale.

--

--

George Mao

Head of Specialist Architects @ Google Cloud. I lead a team of experts responsible for helping customers solve their toughest challenges and adopt GCP at scale