Better Stack AWS Lambda logging

Which language is your AWS Lambda using?

Logging in AWS Lambda running Node.js

Collect logs from your AWS Lambda running Node.js code.

1. Install

Build and upload stockstory/logtail-lambda-extension Lambda layer to your AWS:

Build and upload Lambda layer
git clone git@github.com:stockstory/logtail-lambda-extension.git
cd logtail-lambda-extension
pnpm install
pnpm run build
aws lambda publish-layer-version \
    --layer-name "logtail-lambda-extension" \
    --zip-file "fileb://./dist/extension.zip" \
    --compatible-runtimes nodejs16.x nodejs18.x \
    --compatible-architectures x86_64 arm64

Take care to publish the layer in the region your Lambda function reside in. If that's not your default region, you can change it by adding --region <region-name> parameter to the command above.

2. Add layer to your Lambda functions

In your AWS Console, add the logtail-lambda-extension layer to the Node.js Lambda functions you want to collect logs from.

This can be done in Lambda โ†’ Functions โ†’ your function โ†’ Layers section.
For details, check AWS docs.

3. Configure your source token

In your AWS Console, set up LOGTAIL_TOKEN environment variable for the AWS Lambda functions with your source token as value. You can get your source tokens in your sources in Better Stack Logs.

This can be done in Lambda โ†’ Functions โ†’ your function โ†’ Configuration โ†’ Environment variables. For details, check AWS docs.

4. Start logging ๐ŸŽ‰

Use the logger as usual:

Send logs to Better Stack
console.error('Something bad happened.')
console.info('Log message with structured logging.', {
    'item': "Orange Soda",
    'price': 100.00
})

You should see your logs in Better Stack โ†’ Live tail.

Node.js version 16.0 or higher is required.

Logging in AWS Lambda running Python

Collect logs from your AWS Lambda running Python code.

1. Install

Install Logtail Python and AWS Lambda Powertools PyPI packages:

Install PyPI packages
pip3 install logtail-python
pip3 install aws-lambda-powertools

2. Setup

Set up AWS Lambda logger with Better Stack:

Set up Logtail handler
from logtail import LogtailHandler
from aws_lambda_powertools import Logger

handler = LogtailHandler(source_token="$SOURCE_TOKEN")
logger = Logger(service="example-service", logger_handler=handler, level="INFO")

3. Start logging ๐ŸŽ‰

Use the logger as usual:

Send logs to Better Stack
logger.error('Something bad happened.')
logger.info('Log message with structured logging.', extra={
    'item': "Orange Soda",
    'price': 100.00
})

You should see your logs in Better Stack โ†’ Live tail.

Python version 3.7 or higher is required.
Pip version 20.0.2 or higher is required.

Need help?

Please let us know at hello@betterstack.com.
We're happy to help! ๐Ÿ™

Additional information

Interested in learning more about enriching log context and using formatters?
Head over to official AWS Lambda Powertools documentation.