AWS Lambda in C# with Datadog Integration
To integrate Datadog with an AWS Lambda function written in C# (.NET), you can use the Datadog Lambda Extension and Datadog .NET Tracer. This setup allows you to collect metrics, traces, and logs from your Lambda function.
1.Set Up Datadog in AWS
- Go to your Datadog account and navigate to Integrations → AWS.
- Connect your AWS account using the Datadog AWS integration.
- Enable Lambda and CloudWatch Logs permissions for Datadog.
2.Add Datadog Lambda Extension
You can add the Datadog Lambda Extension as a Lambda Layer.
For .NET 6 or .NET 8 runtimes, use the following ARN (replace with your AWS region):
arn:aws:lambda::AWS_Account_ID:layer:Datadog-Extension:latest
Attach this layer to your Lambda function in the AWS Console or via the AWS CLI.
3.Install Datadog .NET Tracer
In your C# project, install the Datadog .NET tracer NuGet package:
dotnet add package Datadog.Trace
4.Instrument Your Code
You can manually instrument your Lambda handler or use automatic instrumentation.
Example:
using System;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Datadog.Trace;
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace MyLambda
{
public class Function
{
public async Task<string> FunctionHandler(string input, ILambdaContext context)
{
using (var scope = Tracer.Instance.StartActive("lambda.handler"))
{
scope.Span.SetTag("lambda.input", input);
await Task.Delay(100); // Simulate work
return $"Processed: {input}";
}
}
}
}
5.Set Environment Variables
In your Lambda configuration, add these environment variables:
| Variable | Value |
|---|---|
| DD_API_KEY | Your Datadog API key |
| DD_SITE | datadoghq.com (or your region, e.g., datadoghq.eu) |
| DD_LOGS_ENABLED | true |
| DD_TRACE_ENABLED | true |
| DD_ENV | production (or your environment name) |
| DD_SERVICE | my-lambda-service |
| DD_VERSION | 1.0.0 |
6.Enable Logging
Ensure your Lambda function sends logs to CloudWatch Logs. Datadog will automatically collect them if the AWS integration is configured.
7.Deploy and Verify
Deploy your Lambda function.
Then, in Datadog:
- Go to APM → Services to see traces.
- Go to Logs → Explorer to view logs.
- Go to Serverless → Functions to monitor Lambda metrics.
- Optional: Use Datadog Forwarder If you prefer not to use the extension, you can use the Datadog Forwarder Lambda to forward logs and metrics from CloudWatch to Datadog. You can find setup instructions in Datadog’s documentation: docs.datadoghq.com/serverless/forwarder/
This setup provides full observability—metrics, traces, and logs—for your C# AWS Lambda functions in Datadog.
💬 If you enjoyed reading this blog post and found it informative, please take a moment to share your thoughts by leaving a review and liking it 😀 and follow me in dev.to , linkedin, github
Top comments (0)