# Better Stack AWS Lambda logging

Which language is your AWS Lambda using?

- [Node.js](#logging-in-aws-lambda-running-node-js)
- [Python](#logging-in-aws-lambda-running-python)

[info]
#### Collecting from more than one AWS service?

Our [AWS integration](https://betterstack.com/docs/logs/aws-cloudformation/) 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.
[/info]

## Logging in AWS Lambda running Node.js

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

### 1. Install

[warning]
**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.
[/warning]

Download the [logtail/logtail-lambda-extension](https://github.com/logtail/logtail-lambda-extension ";_blank") Lambda layer and publish it in your AWS account:

```bash
[label 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.

[info]
#### Using Windows?
The command above is for bash. Run it in [AWS CloudShell](https://console.aws.amazon.com/cloudshell/home ";_blank")
[/info]

### 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](https://docs.aws.amazon.com/lambda/latest/dg/adding-layers.html ";_blank").

### 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:

```text
[label 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](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html ";_blank").

### 4. Start logging

Use the logger as usual:

```js
[label 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](https://telemetry.betterstack.com/team/0/tail ";_blank").

## 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:

```bash
[label Install PyPI packages]
pip3 install logtail-python
pip3 install aws-lambda-powertools
```

### 2. Setup

Set up AWS Lambda logger with Better Stack:

```python
[label 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:

```python
[label 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](https://telemetry.betterstack.com/team/0/tail ";_blank").

[warning]
#### Python 3.10 or higher is required
Pip version 20.0.2 or higher is required.
[/warning]


## 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](https://docs.powertools.aws.dev/lambda/python/2.15.0/core/logger/#capturing-lambda-context-info).
