Member-only story
Developing on AWS Lambda (Part 3): Some tips you can use!
Control your Lambda Logging Costs
By default, a Lambda function will emit all of the standard out generated during the invocation to a CloudWatch log (CWL) group with a CWL group named in this format: /aws/lambda/[Name of function here]
. CWL groups by default are stored indefinitely — which means you will incur costs , measured in GB of logs written. Today, there is no way to specify the name of the Log group you want Lambda to write into. However, the Log group will not exist until the first execution of your Lambda function. You can take advantage of that behavior by pre-creating the Log group and setting a retention policy that ages out old logs.
LambdaFunction:
Type: AWS::Lambda::Function
LogsLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${LambdaFunction}"
RetentionInDays: 7
Use Layers for common dependencies
This is standard programming best practice: Centralize common code so you don’t have to manage it in many places. If you place the Layer in the same SAM template as a function, you can have that Lambda function automatically reference the newest version of the layer every time it’s published. Here’s how: