Send AWS services logs with the Datadog Lambda function

Send AWS services logs with the Datadog Lambda function

このページは日本語には対応しておりません。随時翻訳に取り組んでいます。翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください。

AWS service logs can be collected with the Datadog Forwarder Lambda function. This Lambda—which triggers on S3 Buckets, CloudWatch log groups, and CloudWatch events—forwards logs to Datadog.

To start collecting logs from your AWS services:

  1. Set up the Datadog Forwarder Lambda function in your AWS account.
  2. Enable logging for your AWS service (most AWS services can log to a S3 bucket or CloudWatch Log Group).
  3. Set up the triggers that cause the Forwarder Lambda to execute when there are new logs to be forwarded. There are two ways to configure the triggers.

Note: If you are in AWS us-east-1 region, leverage Datadog-AWS Private Link.

Note: Cloudformation creates an IAM policy which includes KMS:Decrypt for all resources. It is known that this does not align with Security hub best practice. The reason this permission is used is to decrypt objects from KMS encrypted S3 buckets to set up Lambda function, and which KMS key is used to encrypt the S3 buckets cannot be predicted. You can safely delete this permission after the installation successfully finished.

Set up triggers

There are two options when configuring triggers on the Datadog Forwarder Lambda function:

  • Automatically: Datadog automatically retrieves the log locations for the selected AWS services and adds them as triggers on the Datadog Forwarder Lambda function. Datadog also keeps the list up to date.
  • Manually: Set up each trigger yourself.

Automatically set up triggers

Datadog can automatically configure triggers on the Datadog Forwarder Lambda function to collect AWS logs from the following sources and locations:

Source Location
API Gateway Access Logs CloudWatch
API Gateway Execution Logs CloudWatch
Application ELB Access Logs S3
Classic ELB Access Logs S3
CloudFront Access Logs S3
Lambda Logs CloudWatch
Redshift Logs S3
S3 Access Logs S3
  1. If you haven’t already, set up the Datadog log collection AWS Lambda function.

  2. Ensure the policy of the IAM role used for Datadog-AWS integration has the following permissions. Information on how these permissions are used can be found in the descriptions below:

    "cloudfront:GetDistributionConfig",
    "cloudfront:ListDistributions",
    "elasticloadbalancing:DescribeLoadBalancers",
    "elasticloadbalancing:DescribeLoadBalancerAttributes",
    "lambda:List*",
    "lambda:GetPolicy",
    "redshift:DescribeClusters",
    "redshift:DescribeLoggingStatus",
    "s3:GetBucketLogging",
    "s3:GetBucketLocation",
    "s3:GetBucketNotification",
    "s3:ListAllMyBuckets",
    "s3:PutBucketNotification",
    "logs:PutSubscriptionFilter",
    "logs:DeleteSubscriptionFilter",
    "logs:DescribeSubscriptionFilters"
    
    AWS Permission Description
    cloudfront:GetDistributionConfig Get the name of the S3 bucket containing CloudFront access logs.
    cloudfront:ListDistributions List all CloudFront distributions.
    elasticloadbalancing:
    DescribeLoadBalancers
    List all load balancers.
    elasticloadbalancing:
    DescribeLoadBalancerAttributes
    Get the name of the S3 bucket containing ELB access logs.
    lambda:List* List all Lambda functions.
    lambda:GetPolicy Gets the Lambda policy when triggers are to be removed.
    redshift:DescribeClusters List all Redshift clusters.
    redshift:DescribeLoggingStatus Get the name of the S3 bucket containing Redshift Logs.
    s3:GetBucketLogging Get the name of the S3 bucket containing S3 access logs.
    s3:GetBucketLocation Get the region of the S3 bucket containing S3 access logs.
    s3:GetBucketNotification Get existing Lambda trigger configurations.
    s3:ListAllMyBuckets List all S3 buckets.
    s3:PutBucketNotification Add or remove a Lambda trigger based on S3 bucket events.
    logs:PutSubscriptionFilter Add a Lambda trigger based on CloudWatch Log events
    logs:DeleteSubscriptionFilter Remove a Lambda trigger based on CloudWatch Log events
    logs:DescribeSubscriptionFilters Lists the subscription filters for the specified log group.
  3. Navigate to the Collect Logs tab in the AWS Integration tile.

  4. Select the AWS Account from where you want to collect logs, and enter the ARN of the Lambda created in the previous section.

  5. Select the services from which you’d like to collect logs and click save. To stop collecting logs from a particular service, uncheck it.

  6. If you have logs across multiple regions, you must create additional Lambda functions in those regions and enter them in this tile.

  7. To stop collecting all AWS logs, press the x next to each Lambda ARN. All triggers for that function are removed.

  8. Within a few minutes of this initial setup, your AWS Logs appear in your Datadog log explorer page in near real time.

Manually set up triggers

Collecting logs from CloudWatch log group

If you are collecting logs from a CloudWatch log group, configure the trigger to the Datadog Forwarder Lambda function using one of the following methods:

  1. Select the corresponding CloudWatch Log Group, add the trigger, and optionally add a filter name.
  2. Once done, go into your Datadog Log section to start exploring your logs.

For Terraform users, you can provision and manage your triggers using the aws_cloudwatch_log_subscription_filter resource. See sample code below.

resource "aws_cloudwatch_log_subscription_filter" "datadog_log_subscription_filter" {
  name            = "datadog_log_subscription_filter"
  log_group_name  = <CLOUDWATCH_LOG_GROUP_NAME> # for example, /aws/lambda/my_lambda_name
  destination_arn = <DATADOG_FORWARDER_ARN> # for example,  arn:aws:lambda:us-east-1:123:function:datadog-forwarder
  filter_pattern  = ""
}

For AWS CloudFormation users, you can provision and manage your triggers using the CloudFormation AWS::Logs::SubscriptionFilter resource. See sample code below.

The sample code also work for AWS SAM and Serverless Framework. For Serverless Framework, put the code under the resources section within your serverless.yml.

Resources:
  MyLogSubscriptionFilter:
    Type: "AWS::Logs::SubscriptionFilter"
    Properties:
      DestinationArn: "<DATADOG_FORWARDER_ARN>"
      LogGroupName: "<CLOUDWATCH_LOG_GROUP_NAME>"
      FilterPattern: ""

Collecting logs from S3 buckets

If you are collecting logs from an S3 bucket, configure the trigger to the Datadog Forwarder Lambda function using one of the following methods:

  1. Once the Lambda function is installed, manually add a trigger on the S3 bucket that contains your logs in the AWS console:
  1. Select the bucket and then follow the AWS instructions:
  1. Set the correct event type on S3 buckets:

Once done, go into your Datadog Log section to start exploring your logs!

For Terraform users, you can provision and manage your triggers using the aws_s3_bucket_notification resource. See the sample code below.

resource "aws_s3_bucket_notification" "my_bucket_notification" {
  bucket = my_bucket
  lambda_function {
    lambda_function_arn = "<DATADOG_FORWARDER_ARN>"
    events              = ["s3:ObjectCreated:*"]
    filter_prefix       = "AWSLogs/"
    filter_suffix       = ".log"
  }
}

For CloudFormation users, you can configure triggers using the CloudFormation NotificationConfiguration for your S3 bucket. See the sample code below.

Resources:
  Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: "<MY_BUCKET>"
      NotificationConfiguration:
        LambdaConfigurations:
        - Event: 's3:ObjectCreated:*'
          Function: "<DATADOG_FORWARDER_ARN>"

Enable logging for your AWS service

Any AWS service that generates logs into a S3 bucket or a CloudWatch Log Group is supported. Find specific setup instructions for the most used services in the table below:

AWS service Activate AWS service logging Send AWS logs to Datadog
API Gateway Enable AWS API Gateway logs Manual log collection
Cloudfront Enable AWS Cloudfront logs Manual and automatic log collection
Cloudtrail Enable AWS Cloudtrail logs Manual log collection
DynamoDB Enable AWS DynamoDB logs Manual log collection
EC2 - Use the Datadog Agent to send your logs to Datadog
ECS - Use the docker agent to gather your logs
Elastic Load Balancing (ELB) Enable AWS ELB logs Manual and automatic log collection
Lambda - Manual and automatic log collection
RDS Enable AWS RDS logs Manual log collection
Route 53 Enable AWS Route 53 logs Manual log collection
S3 Enable AWS S3 logs Manual and automatic log collection
SNS There is no “SNS Logs”. Process logs and events that are transiting through to the SNS Service. Manual log collection
RedShift Enable AWS Redshift logs Manual and automatic log collection
VPC Enable AWS VPC logs Manual log collection

Scrubbing and filtering

You can scrub emails or IP address from logs sent by the Lambda function, or define a custom scrubbing rule in the Lambda parameters. You can also exclude or send only those logs that match a specific pattern by using the filtering option.