Better Stack AWS Lambda logging

Which language is your AWS Lambda using?

Collecting from more than one AWS service?

Our AWS integration deploys a CloudFormation stack that forwards your CloudWatch log groups — Lambda's included — along with CloudWatch metrics and X-Ray traces. The setup below ships logs from individual functions without touching the rest of your account.

Logging in AWS Lambda running Node.js

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

1. Install

Node.js version 22.0 or higher is required.

The extension is built for the nodejs22.x and nodejs24.x runtimes. AWS deprecated Node.js 20 and older.

Already running on nodejs20.x? Update your function's runtime to nodejs22.x or newer, then republish the layer with the command below.

Download the logtail/logtail-lambda-extension Lambda layer and publish it in your AWS account:

Download and publish Lambda layer
curl -fsSLO https://github.com/logtail/logtail-lambda-extension/releases/latest/download/logtail-lambda-extension.zip
aws lambda publish-layer-version \
    --layer-name "logtail-lambda-extension" \
    --zip-file "fileb://logtail-lambda-extension.zip" \
    --compatible-runtimes nodejs22.x nodejs24.x \
    --compatible-architectures x86_64 arm64

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

Using Windows?

The command above is for bash. Run it in AWS CloudShell

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 SOURCE_TOKEN and INGESTING_HOST environment variables for the AWS Lambda functions like this:

Environment variables
SOURCE_TOKEN:   $SOURCE_TOKEN
INGESTING_HOST: $INGESTING_HOST


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 → Logs & traces.

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

logtail_handler = LogtailHandler(
    source_token='$SOURCE_TOKEN',
    host='https://$INGESTING_HOST',
    flush_timeout=1,
)
logger = Logger(service="example-service", logger_handler=logtail_handler, level="INFO")


3. Start logging

Use the logger as usual, and call logtail_handler.flush() in a finally block so the logs are sent before AWS Lambda freezes the function:

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

You should see your logs in Better Stack → Live tail.

Python 3.10 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 docs.