Jump into Datadog With AWS Serverless CDK App
Tags: aws, serverless, datadog, awscdk
Published: 2023-05-24 09:41:15

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k5j88grsljmvh191xtjg.png)

**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](https://app.datadoghq.com/signup)

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](https://medium.com/r/?url=https%3A%2F%2Fdocs.datadoghq.com%2Fgetting_started%2Fintegrations%2Faws%2F)
For Manual, use [this link](https://medium.com/r/?url=https%3A%2F%2Fdocs.datadoghq.com%2Fintegrations%2Fguide%2Faws-manual-setup%2F%3Ftab%3Droledelegation)**
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
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5u4baxnudplliya69edn.png)

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
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfpy7qab6rditxjufes8.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vkiimx3f88bmlhtx95g.png)

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
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4shupcscrlc4hivv7qng.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxuezjnlnwr8iv3d01b8.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ewk0lr8tg28j071o9xno.png)

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](https://docs.datadoghq.com/logs/guide/forwarder/?tab=cloudformation) 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