DEV Community

Rushi Patel
Rushi Patel

Posted on

Jump into Datadog With AWS Serverless CDK App

This article will help you to start using DataDog with your AWS Serverless CDK app. Now, we will see some background knowledge of DataDog.

Image description

What is Datadog?

Through a SaaS-based data analytics platform, Datadog offers monitoring of servers, databases, tools, and services for cloud-scale applications.

You must first register with DataDog, which offers a free 14-day trial, by clicking this link

You might need to configure IAM Role Policy for DataDog after successfully signing up, both manually and automatically are options.

For Automatic (Recommend), use this link
For Manual, use this link

We’re about to start developing our AWS CDK app in typescript using typescript template. You can use the command below.

mkdir DatadogCDK

cd DatadogCDK

cdk init app — language typescript
Enter fullscreen mode Exit fullscreen mode

The NodejsService function will now be created in the file lib/constructs/nodejs-service-function.ts and used for all of the Lamda functions in our app.
You can use the following code and modify it according to your needs.

Image description

Note: You must install the necessary dependencies such as aws-lambda, aws-cdk, middy, esbuild and winston.

We are now prepared to rock ’n’ roll in our primary stack by installing the necessary Datadog dependencies.

npm i datadog-cdk-constructs-v2 datadog-lambda-js
Enter fullscreen mode Exit fullscreen mode

It’s time to write our first lambda, which should generate a single metric in DataDog and then just return an object. This is how your lambda should look.

Image description

Now, we’re defining our Datadog with the appropriate site and API key from your Datadog account. Then, for monitoring, we develop a single lambda function and tie it to Datadog. If you want logs of every resource in your AWS account, you can use DataDog Forwarder; more information can be found in the section below this article.

Your stack appears below:

Image description

Now that we’ve had DataDog Monitoring, we’re prepared to test our lambda. You must perform the commands listed below to bootstrap and deploy your CDK application:

npx cdk bootstrap

npx cdk deploy
Enter fullscreen mode Exit fullscreen mode

Run your lambda function when it has successfully deployed, and feel free to tweak the code if you wish to see error logs.

Please wait at least 10 minutes after invoking lambda (to allow the data from lambda to Datadog to sync)

I repeatedly called my function using the happy and unhappy paths, and I could clearly see the logs and fascinating information about your lambda functions.

You can also see your metrics logs because our lambda includes coffee.order value. The logs panel allows you to view detailed logs. I’ve provided an example set from my DataDog Dashboard logs here.

Image description

Image description

Image description

If you have the associated services then the logs are also visible for your lambda function and may be seen by exploring other DataDog features. You need to refer to the documentation for that.

We will now have a look at a fascinating lambda function created by DataDog, called DataDog Forwarder.

What is DataDog Forwarder?
An AWS Lambda function called the Datadog Forwarder transports logs, custom metrics, and traces from your environment to Datadog. The Forwarder is able to forward logs of AWS Services like: Cloudwatch, ELB, S3, CloudTrail, SNS, AWS lambda, Kinesis data stream, and many more.

For installation, you need to follow the documentation by DataDog.

Here, You can find my [GitHub](https://github.com/rushi308/cdk-datadog) code if you want to have a look.

That’s it; you now have a foundational understanding of how DataDog fits into your Serverless architecture.

Happy Coding!! Cheers

Top comments (2)

Collapse
 
benbpyle profile image
Benjamen Pyle

Well done. I love DataDog as well and find it's the perfect compliment for a Serverless App and gives me and my teams all the observability and tracing that I need.

Collapse
 
xushiii profile image
Rushi Patel

Thanks @benbpyle
Yes pretty cool monitoring as well :)