Monitoring Your App in Prod

Once your app has been deployed to production, it's useful to be able to monitor your Lambda functions. There are a few different services that you can use for this. We'll look at them below.

Datadog#

Datadog offers an End-to-end Serverless Monitoring solution that works with Lambda functions. The best way to integrate is by using the CDK construct they provide.

Start by adding it to your project.

# npm
npm install --save-dev datadog-cdk-constructs
# Yarn
yarn add --dev datadog-cdk-constructs

Next, you'll need to import it into a stack. Add pass in the functions you want monitored.

import { Datadog } from "datadog-cdk-constructs";
const datadog = new Datadog(this, "Datadog", {
apiKey: "<DATADOG_API_KEY>"
});
datadog.addLambdaFunctions([myfunc]);

To monitor all the functions in a stack, you can use the Stack construct's getAllFunctions method and do the following at the bottom of your stack definition.

datadog.addLambdaFunctions(this.getAllFunctions());

For more details, check out the Datadog docs.

Sentry#

Sentry offers Serverless Error Monitoring for your Lambda functions. Integration is done through a Lambda Layer.

Head over to the Layer that Sentry provides, select your region and copy the layer ARN.

Then add the Layer to your stack.

import { LayerVersion } from "@aws-cdk/aws-lambda";
const sentry = LayerVersion.fromLayerVersionArn(
this,
"SentryLayer",
`arn:aws:lambda:${scope.region}:943013980633:layer:SentryNodeServerlessSDK:34`
);

You can then set it for all the functions in your stack using the addDefaultFunctionLayers and addDefaultFunctionEnv. Note we only want to enable this when the function is deployed, not when using Live Lambda Dev.

if (!scope.local) {
stack.addDefaultFunctionLayers([layer]);
stack.addDefaultFunctionEnv({
SENTRY_DSN: "<SENTRY_DSN>",
NODE_OPTIONS: "-r @sentry/serverless/dist/awslambda-auto"
});
}

Sentry also offers performance monitoring for serverless. To enable, add the SENTRY_TRACES_SAMPLE_RATE environment variable.

stack.addDefaultFunctionEnv({
SENTRY_DSN: "<SENTRY_DSN>",
SENTRY_TRACES_SAMPLE_RATE: "1.0",
NODE_OPTIONS: "-r @sentry/serverless/dist/awslambda-auto"
});

This can be tuned between the values of 0 and 1. Where 0 means that no performance related information is sent, and 1 means that information for all the invocations are sent. This should be tuned based on the volume of invocations and the amount of transactions available in your Sentry account. A value of 0.5 should work for most projects.

For more details, check out the Sentry docs.

Epsagon#

Epsagon is an end-to-end Application Monitoring Service and can monitor the full lifecycle of your serverless requests.

To get started, sign up for an Epsagon account. Deploy their stack in your production AWS account. Then to enable Lambda monitoring, add a layer to the functions you want to monitor.

To figure out the layer ARN, follow these steps from the Epsagon docs.

With the layer ARN, you can use the layer construct in your CDK code.

import { LayerVersion } from "@aws-cdk/aws-lambda";
const epsagon = LayerVersion.fromLayerVersionArn(this, "EpsagonLayer", "<ARN>");

You can then set it for all the functions in your stack using the addDefaultFunctionLayers and addDefaultFunctionEnv. Note we only want to enable this when the function is deployed, not in live debugging mode.

if (!scope.local) {
stack.addDefaultFunctionLayers([epsagon])
stack.addDefaultFunctionEnv({
EPSAGON_TOKEN: "<token>",
EPSAGON_APP_NAME: "<app_name>",
NODE_OPTIONS: "-r epsagon-frameworks"
})
}

For more details, check out the Epsagon docs.

Lumigo#

Lumigo offers a Serverless Monitoring and Debugging Platform.

To get started, sign up for an account. Then follow their wizard to deploy their stack in your AWS production account. Then to enable Lambda monitoring, add a layer to the functions you want to monitor.

To figure out the layer ARN, use this repository.

With the layer ARN, you can use the layer construct in your CDK code.

import { LayerVersion } from "@aws-cdk/aws-lambda";
const lumigo = LayerVersion.fromLayerVersionArn(this, "LumigoLayer", "<ARN>");

You can then set it for all the functions in your stack using the addDefaultFunctionLayers and addDefaultFunctionEnv. Note we only want to enable this when the function is deployed, not in live debugging mode.

if (scope.local) {
stack.addDefaultFunctionLayers([layers])
stack.addDefaultEnv({
LUMIGO_TRACER_TOKEN: "<token>",
AWS_LAMBDA_EXEC_WRAPPER: "/opt/lumigo_wrapper"
})
}

For more details, check out the Lumigo docs.