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

## 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](https://github.com/stockstory/logtail-lambda-extension ";_blank") Lambda layer to your AWS:

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

[warning]
**Using a Window machine?**

To avoid compatibility issues caused by different line endings, build and upload the Lambda layer using [WSL](https://learn.microsoft.com/en-us/windows/wsl/install).
[/warning]

### 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 `LOGTAIL_TOKEN` and `LOGTAIL_HTTP_API_URL` environment variables for the AWS Lambda functions like this:

```text
[label Environment variables]
LOGTAIL_TOKEN:        $SOURCE_TOKEN
LOGTAIL_HTTP_API_URL: https://$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").

[warning]
**Node.js version 16.0 or higher is required.**
[/warning]

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

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

### 3. Start logging 🎉

Use the logger as usual:

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

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