{"id":2919,"library":"datadog-lambda","title":"Datadog AWS Lambda Library","description":"The `datadog-lambda` library enables comprehensive monitoring, tracing, and logging for AWS Lambda functions in Datadog. It provides a Python wrapper to automatically instrument Lambda handlers, collect metrics, send traces, and forward logs to Datadog. The current version is 8.123.0 and it maintains a rapid release cadence, often several times a week, to keep pace with new features and bug fixes, typically bundling updates from `dd-trace-py`.","status":"active","version":"8.123.0","language":"en","source_language":"en","source_url":"https://github.com/DataDog/datadog-lambda-python","tags":["aws","lambda","monitoring","tracing","observability","serverless","datadog"],"install":[{"cmd":"pip install datadog-lambda","lang":"bash","label":"Install library locally"}],"dependencies":[],"imports":[{"symbol":"datadog_lambda_wrapper","correct":"from datadog_lambda.wrapper import datadog_lambda_wrapper"},{"symbol":"extract_trigger_tags","correct":"from datadog_lambda.trigger import extract_trigger_tags"}],"quickstart":{"code":"import json\nimport os\nfrom datadog_lambda.trigger import extract_trigger_tags\nfrom datadog_lambda.wrapper import datadog_lambda_wrapper\n\n# Datadog API Key is typically set as an environment variable or handled by the Datadog Lambda Extension.\n# No direct in-code API key needed for basic wrapping.\n\n@datadog_lambda_wrapper\ndef lambda_handler(event, context):\n    # Extract tags specific to the trigger (e.g., SQS queue name, API Gateway details)\n    trigger_tags = extract_trigger_tags(event)\n    print(f\"Trigger tags: {trigger_tags}\")\n\n    # Example of adding a custom tag to the trace span\n    if os.environ.get('DD_TRACE_ENABLED', 'true').lower() == 'true':\n        try:\n            from ddtrace import tracer\n            span = tracer.current_span()\n            if span: # Span might be None if tracing is not enabled or not yet started\n                span.set_tag('my_custom_tag', 'hello_datadog')\n        except ImportError:\n            pass # ddtrace is not installed or available\n\n    response_body = {\n        \"message\": \"Hello from Datadog Lambda!\",\n        \"input\": event\n    }\n\n    return {\n        \"statusCode\": 200,\n        \"body\": json.dumps(response_body)\n    }\n\n# To test locally (requires dummy event and context):\n# if __name__ == \"__main__\":\n#     class MockContext:\n#         aws_request_id = \"test-request-id-123\"\n#         function_name = \"test-function\"\n#         invoked_function_arn = \"arn:aws:lambda:us-east-1:123456789012:function:test-function\"\n#         memory_limit_in_mb = 128\n#         get_remaining_time_in_millis = lambda: 10000\n\n#     test_event = {\n#         \"httpMethod\": \"GET\",\n#         \"path\": \"/test\",\n#         \"queryStringParameters\": {\"param1\": \"value1\"}\n#     }\n#     os.environ['DD_SERVICE'] = 'my-lambda-service'\n#     os.environ['DD_ENV'] = 'dev'\n#     os.environ['DD_VERSION'] = '1.0.0'\n#     os.environ['DD_TRACE_ENABLED'] = 'true'\n#     print(lambda_handler(test_event, MockContext()))","lang":"python","description":"To instrument an AWS Lambda function, import `datadog_lambda_wrapper` and apply it as a decorator to your handler function. The library automatically captures metrics, logs, and traces. Remember that for actual deployment, using the Datadog AWS Lambda Layer is the recommended and most common approach, rather than `pip installing` the library into your deployment package, as the layer includes the necessary dependencies and potentially the Datadog Agent extension."},"warnings":[{"fix":"Disable profiling (`DD_PROFILING_ENABLED=false`) or use a Python runtime of 3.13 or older if profiling is critical. Monitor future releases for Python 3.14 support.","message":"Profiling is not supported on Python 3.14 Lambdas and enabling it (`DD_PROFILING_ENABLED=true`) will result in application crashes. This was first noted in v8.118.0.","severity":"breaking","affected_versions":">=8.118.0 for Python 3.14 runtime"},{"fix":"Be aware of your runtime's bundled `dd-trace-py` version when developing. Check the release notes of `datadog-lambda` for the exact `dd-trace-py` version bundled for your target Python runtime.","message":"The internal `dd-trace-py` dependency's version varies based on your Lambda's Python runtime. Python 3.8 and 3.9 runtimes use an older `dd-trace-py` version (e.g., v3.19.5 with `datadog-lambda` v8.122.0), while Python >= 3.10 uses a newer one. This can lead to unexpected behavior or API differences if not accounted for.","severity":"gotcha","affected_versions":"All versions, due to runtime-specific bundling"},{"fix":"Always review the Datadog documentation for a comprehensive list of required and optional environment variables and ensure they are correctly set in your Lambda function configuration.","message":"Many critical configurations for `datadog-lambda` are controlled via environment variables (e.g., `DD_API_KEY`, `DD_SITE`, `DD_TRACE_ENABLED`, `DD_LAMBDA_HANDLER`). Misconfiguring or omitting these can prevent data from appearing in Datadog.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For production deployments, utilize the Datadog AWS Lambda Layer. Consult the Datadog documentation for the latest ARN for your region and desired Python runtime. Avoid mixing `pip install` with the Lambda layer unless specifically instructed.","message":"While `pip install datadog-lambda` can be used for local development, the recommended and most robust deployment method for AWS Lambda is to use the official Datadog AWS Lambda Layer. The layer bundles the library, its dependencies, and sometimes the Datadog Agent Extension, ensuring compatibility and reducing deployment package size.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade to version 8.123.0 or newer to ensure more accurate cold start reporting with Lambda Managed Runtimes. Users upgrading might observe changes in their cold start metric trends.","message":"Previous versions might have had inconsistencies or issues in how cold start metrics were reported, especially when using Lambda Managed Runtimes (LMI). Version 8.123.0 includes a fix to 'omit creating cold start on LMI'.","severity":"gotcha","affected_versions":"<8.123.0"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}