Reduce Log Output

Cut AWS Lambda CloudWatch Logs costs by tuning log levels, adopting structured logging, and logging only what you need.

Overview

Lower CloudWatch Logs data ingestion costs by reducing log volume. CloudWatch Logs charges separately for data ingested and data stored, so fewer and smaller log lines reduce what you pay to get data in, and then log-retention policies determine how long you keep it.

Best Practices

1. Disable Debug Logs in Production

Use environment variables to control logging—turn verbosity up in dev, down in prod:

import os
import logging

LOG_LEVEL = os.environ.get('LOG_LEVEL', 'INFO')
logger = logging.getLogger()
logger.setLevel(LOG_LEVEL)

def lambda_handler(event, context):
    logger.debug('Detailed debug info')  # Not logged in prod
    logger.info('Important info')         # Logged

2. Use Structured Logging

JSON format is more efficient to query and filter with CloudWatch Logs Insights (for example, filtering on fields instead of parsing free-form strings), even if the payload size in bytes is similar:

from aws_lambda_powertools import Logger

logger = Logger()

@logger.inject_lambda_context
def lambda_handler(event, context):
    logger.info("Processing order", extra={"order_id": 123})

3. Define Error Codes

Instead of verbose error messages:

Before:

logger.error(f"Failed to process order {order_id} for customer {customer_id} due to invalid payment method")

After:

logger.error("ERR_001", extra={"order_id": order_id})

4. Use Lambda Powertools1

Configurable logging with levels:

from aws_lambda_powertools import Logger

logger = Logger(service="order-processor", level="INFO")

Combine this strategy with Cost-Effective Log Storage so you control both how much you log and how long you keep it.

Footnotes

  1. Powertools for AWS Lambda – Developer Guide