> For the complete documentation index, see [llms.txt](https://docs.p0.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.p0.dev/integrations/access-logs/aws-cloudwatch-logs.md).

# AWS CloudWatch Logs

Install the AWS CloudWatch Logs log source so Access Logs can show the CloudTrail events P0 attributes to each AWS access request.

The **AWS CloudWatch Logs** log source lets [Access Logs](https://docs.p0.dev/integrations/access-logs.md) show CloudTrail events for grants made through the AWS integration. P0 reads the events from a CloudTrail trail that delivers to CloudWatch Logs in your account, through a connector you deploy on AWS Lambda. P0 invokes the connector through a role that can do nothing else.

{% hint style="info" %}
AWS CloudWatch Logs is in beta. Functionality and configuration may change. Contact P0 to turn on Access Logs for your organization.
{% endhint %}

## Plan your log sources

Each log source reads one AWS account and region. When you add one, choose which CloudTrail it reads:

* **This account's trail**: a trail that logs this AWS account. It covers grants in this account only.
* **An organization trail**: a trail that logs every account in your AWS organization. It covers grants in the AWS accounts you have installed for IAM management in P0. Add it in the account and region that hold the trail's log group. P0 doesn't check which accounts are in the trail's organization, so an installed account outside it shows no activity rather than a gap note.
* **No CloudTrail log group**: reads only the RDS database logs in this account and region, for database auditing.

You can add one log source with an organization trail, and one with this account's trail in each AWS account. When both cover an account, P0 reads the account's own trail. You can add only one log source for each account and region.

A multi-region trail writes every region's events to one log group, so one log source covers the whole account. The region you choose is where the log group and the connector are.

## Prerequisites

* The AWS integration's IAM management installed for each account whose grants you want to see (see [Setting up AWS IAM management](https://docs.p0.dev/integrations/resource-integrations/aws/installation-methods.md)).
* A CloudTrail trail that delivers to a CloudWatch Logs log group, in an AWS commercial region. GovCloud and China regions aren't supported.
* Terraform 1.4 or later, with version 3.3 or later of the `kreuzwerker/docker` provider.
* Docker and the AWS CLI wherever you apply the Terraform, with the AWS CLI signed in to the log source's account. The Terraform copies P0's connector image into Amazon ECR with them.

## Deliver CloudTrail to CloudWatch Logs

P0 reads CloudTrail only from CloudWatch Logs, not from the trail's S3 bucket or CloudTrail Lake. If no trail delivers to CloudWatch Logs yet:

1. **Open the trail** in the [CloudTrail console](https://console.aws.amazon.com/cloudtrail/), or create one. Make it a multi-region trail, and an organization trail if you want one log source to cover your organization.
2. **Turn on CloudWatch Logs delivery** in the trail's settings, with a new or existing log group. AWS creates the role CloudTrail delivers with. See [Sending events to CloudWatch Logs](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/send-cloudtrail-events-to-cloudwatch-logs.html).
3. **Note the log group's name**, such as `aws-cloudtrail-logs`.
4. **Set the log group's retention.** Access Logs can only show events the log group still keeps.

To manage the trail in Terraform, start from the provider's [example organization trail](https://github.com/p0-security/terraform-provider-p0/blob/main/examples/resources/p0_aws_cloudwatch_logs/cloudtrail.tf), which creates the trail, its log group, and the role CloudTrail delivers with. It is your own Terraform: P0 doesn't manage the trail.

Events can take up to 15 minutes to reach the log group. When you add the log source, P0 checks that the log group holds a CloudTrail event from the last 24 hours.

## Turn on CloudTrail data events

A trail records management events, such as creating or changing a resource. Reads and writes of data are data events, which a trail records only when you turn them on. P0 checks for data events on these services:

* S3 objects
* Lambda functions
* DynamoDB tables

When a grant's actions include data events your trail doesn't record, the card shows a gap note for that service. To record them:

1. **Open the trail** in the CloudTrail console and edit its **Data events**.
2. **Add the resource types** you want, such as `AWS::S3::Object`. See [Logging data events](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/logging-data-events-with-cloudtrail.html).
3. **Choose read events, write events, or both.** P0 shows a gap note for the kind you leave out.

A data event selector can be limited to some buckets, functions, or tables, and P0 counts it as covering that resource type. A selector that picks only some event names doesn't count, because P0 can't tell whether it holds the events a grant uses.

In Terraform, add an `advanced_event_selector` to the trail for each resource type:

```terraform
resource "aws_cloudtrail" "this" {
  # ... the trail's other settings

  advanced_event_selector {
    name = "Management events"
    field_selector {
      field  = "eventCategory"
      equals = ["Management"]
    }
  }

  advanced_event_selector {
    name = "S3 data events"
    field_selector {
      field  = "eventCategory"
      equals = ["Data"]
    }
    field_selector {
      field  = "resources.type"
      equals = ["AWS::S3::Object"]
    }
  }
}
```

Data events are billed per event. See [Access Logs](https://docs.p0.dev/integrations/access-logs.md#what-access-logs-costs).

## Install in the P0 Dashboard

1. Navigate to **Integrations** on [p0.app](https://p0.app). Under **Access Logs**, select **AWS CloudWatch Logs**.
2. Click **Add log source** and fill in the log source's details, then click **Next**:
   * **AWS account ID**: the account whose logs P0 reads, and where the connector runs.
   * **Region**: the region of the CloudTrail log group, such as `us-west-2`.
   * **CloudTrail**: **This account's trail**, **An organization trail**, or **No CloudTrail log group**. For a trail, enter the **Log group name** your trail delivers to.
3. P0 generates a Terraform configuration that deploys the connector. Copy it into your Terraform project, apply it in the log source's account, then click **Next**. It creates:
   * An Amazon ECR repository with a copy of P0's connector image. The plan fails if the image on Docker Hub no longer matches the version P0 pinned.
   * A Lambda function for the connector, with no VPC access, and its role. The role can run Logs Insights queries on your CloudTrail log group and on the RDS log groups in this account and region, and read your trails' settings.
   * A role that P0 assumes to invoke the connector, and nothing else.
4. P0 checks the log source. See [#install-checks](#install-checks "mention"). If you just applied the Terraform, give the Lambda function a few minutes to deploy, then click **Next** again.
5. Review the summary and click **Finish**.

## Install with Terraform

Instead of the P0 Dashboard, you can install the log source with the P0 Terraform provider (see [Manage integrations as code with the P0 Terraform provider](https://docs.p0.dev/getting-started/get-started-with-the-p0-terraform-provider.md)). Create the resources in this order:

1. **Stage the log source** with `p0_aws_cloudwatch_logs_staged`. Set `account_id` and `region`, and a `cloudtrail` block with the trail's `log_group_name` and its `coverage`. Leave out `cloudtrail` for a log source that reads only RDS logs. P0 gives you the names and policies the connector's deployment needs.
2. **Deploy the connector** in that account and region, as the Terraform from the P0 Dashboard does.
3. **Install the log source** with `p0_aws_cloudwatch_logs`, with the same `account_id`, `region`, and `cloudtrail`. P0 runs the checks in [#install-checks](#install-checks "mention").

```terraform
resource "p0_aws_cloudwatch_logs_staged" "example" {
  account_id = "123456789012"
  region     = "us-west-2"

  cloudtrail {
    log_group_name = "aws-cloudtrail-logs"
    coverage       = "organization"
  }
}
```

## Install checks

When you add the log source, P0 invokes the connector and checks that:

* The log group exists in the account and region.
* The log group holds a CloudTrail event from the last 24 hours.
* For **An organization trail**, the trail that delivers to the log group is an organization trail.

These problems don't stop the install. P0 shows them as a warning on the log source:

* The trail doesn't deliver data events for S3, Lambda, or DynamoDB. See [#turn-on-cloudtrail-data-events](#turn-on-cloudtrail-data-events "mention").
* You chose **This account's trail**, but the trail is an organization trail. Add the log source again as **An organization trail** to cover your other accounts.
* No trail in this account and region delivers to the log group, so P0 can't tell which data events it records.

After the install, P0 checks every hour that it can still invoke the connector. That check reads no logs.

## Next steps

* See what the card shows in [Access Logs](https://docs.p0.dev/integrations/access-logs.md).
* See how P0 matches events to each kind of AWS grant in [Attribution criteria](https://docs.p0.dev/integrations/access-logs/attribution-criteria.md#aws).
