Skip to main content

Production Observability for a Lambda Service

Baseline production observability for a Lambda service on AWS - log retention, SNS alerting, error and latency alarms, a CloudWatch dashboard, and X-Ray tracing.

This example lives in the pulumi/examples repository. Check out just this directory to use it:

Get started with this example
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set aws-ts-production-observability
cd pulumi-examples/aws-ts-production-observability

Wiring up monitoring after the fact is tedious and easy to get wrong. This example provisions a baseline observability stack for an AWS Lambda service so a new service is watched from its first deploy.

It creates:

  • A CloudWatch log group with a 30-day retention policy.
  • An SNS topic with an email subscription for alarm notifications.
  • CloudWatch metric alarms for function errors and high latency.
  • A CloudWatch dashboard charting the function’s error count and duration.
  • A small sample Lambda function with AWS X-Ray active tracing enabled, standing in for the workload you want to observe.

Swap the sample function for your real workload (or point the alarms and dashboard at an existing function name) to reuse this as the monitoring layer for any Lambda service.

Prerequisites#

  1. Install Pulumi
  2. Configure your AWS credentials
  3. Install Node.js

Deploying and running the program#

  1. Create a new stack:

    Terminal window
    pulumi stack init dev
  2. Set the AWS region and the email address to notify:

    Terminal window
    pulumi config set aws:region us-west-2
    pulumi config set notificationEmail you@example.com
  3. Install dependencies:

    Terminal window
    npm install
  4. Run pulumi up to preview and deploy:

    Terminal window
    pulumi up
  5. AWS sends a confirmation email to the address you configured. Click the link in that email to activate the SNS subscription, otherwise alarm notifications won’t be delivered.

  6. Inspect the stack outputs:

    Terminal window
    pulumi stack output
    Current stack outputs (3):
    OUTPUT VALUE
    dashboardId dev-production-observability-dashboard
    notificationTarget arn:aws:sns:us-west-2:***:dev-production-observability-alerts
    traceHook Lambda dev-production-observability-sample has X-Ray tracing active

    Open the dashboard in the CloudWatch console to see the widgets.

Clean up#

To tear down the resources, run:

Terminal window
pulumi destroy
pulumi stack rm

Summary#

In this example you deployed a reusable observability baseline for an AWS Lambda service: centralized logs, email alerting through SNS, error and latency alarms, a dashboard, and X-Ray tracing. Point it at your own function to get production monitoring in place from day one.

Related

The infrastructure as code platform for any cloud.