

# Access logs for your Amplify app

**Welcome to the new AWS Amplify**  
Amplify now puts hosting, a Postgres database, and sign-in in one AWS account. Deploy from your terminal or from Git, check every change in a preview first, and roll back in one step if something breaks.  
Looking for Amplify Hosting? See the [Amplify Hosting User Guide](https://docs.aws.amazon.com/amplify/hosting/userguide/welcome.html).

To help debug issues related to requests or client access to your app, you can use access logs. Amplify turns on access logs by default in the production environment of your app. By default, you get 5% of production access logs in Amazon CloudWatch Logs. Amplify keeps them for 1 week. In all other environments, such as `preview`, access logs are off until you turn them on. You can turn off access logs in any environment, including production.

Access logs cover your whole app. Requests to your app’s pages and assets and calls to its backend are in the same log, and the `surface` field tells them apart. For more information, see [Access log fields](#using-access-logs-fields).

Depending on your app usage, you might generate a lot of access logs. You don’t need to send all of them to your account. Instead, you can configure the sampling rate and retention period for your logs. These settings control what percentage of logs Amplify sends to your account and how long the logs persist. This helps you reduce costs while you continue to monitor your app.

You can also send your access logs to a new or existing Amazon S3 bucket for long-term retention and analysis. Amplify doesn’t deliver access logs to Amazon S3 by default in any environment, including production. To use Amazon S3, turn on S3 delivery for each environment.

As with [runtime logs](monitoring-runtime-logs.md), you can sort your logs and download them. You can search your logs by the following status types:
+ 2xx
+ 3xx
+ 4xx
+ 5xx

You can also sort your access logs by the HTTP method used to access your app.

Access logs incur charges in your AWS account, including the default delivery in the production environment. CloudWatch Logs charges for ingesting and storing your access logs. If you also deliver access logs to Amazon S3, Amazon S3 charges for storing them. To control costs, adjust the sampling rate and retention period.

## Access log destination
<a name="_access_log_destination"></a>

When you deliver access logs to CloudWatch Logs, Amplify writes them to a log group with the following name pattern:

```
/aws/amplifygen3/<app-name>/<environment-name>/access-logs
```

For example, the access logs for the production environment are in `/aws/amplifygen3/<app-name>/production/access-logs`.

Backend requests are in the same log group as web requests. Database logs are in a separate log group. For more information, see [Database logs](monitoring-database-logs.md).

## Access log fields
<a name="using-access-logs-fields"></a>

Amplify writes each access log line as a JSON object. The `surface` field is `web` for pages and assets, or `backend` for calls to `/rest`, `/rest/rpc`, and `/auth`. To filter on a field, use `--filter '{ $.<field> = "<value>" }'`, which uses CloudWatch Logs filter syntax and is case-sensitive.

The following table lists the fields.


| Field | Appears on | Description | 
| --- | --- | --- | 
|  `resource_arn`  | Both | The Amazon Resource Name (ARN) of the environment, followed by the destination, `/CWL` or `/S3`. | 
|  `event_timestamp`  | Both | The time of the request. | 
|  `surface`  | Both | The part of your app that served the request: `web` or `backend`. | 
|  `request_id`  | Both | The ID of the request. On a web line, it’s the page’s Amazon CloudFront ID, which is also the `x-amz-cf-id` header. On a backend line, it’s the `x-request-id` header, which the client SDK puts on a failed call’s `error.requestId`. | 
|  `client_ip`  | Both | The IP address of the client. | 
|  `user_agent`  | Both | The user agent of the client. | 
|  `method`  | Both | The HTTP method of the request. | 
|  `path`  | Both | The path of the request. | 
|  `query_string`  | Both | The query string of the request. | 
|  `host`  | Both | The host of the request. | 
|  `protocol`  | Both | The protocol of the request. | 
|  `referer`  | Both | The `Referer` header of the request. | 
|  `request_bytes`  | Both | The size of the request, in bytes. | 
|  `status_code`  | Both | The HTTP status code of the response. | 
|  `response_bytes`  | Both | The size of the response, in bytes. | 
|  `content_type`  | Both | The content type of the response. | 
|  `time_taken_ms`  | Both | The total time to serve the request, in milliseconds. | 
|  `client_country`  | Web | The country of the client. | 
|  `time_to_first_byte_ms`  | Web | The time to first byte, in milliseconds. | 
|  `origin_first_byte_latency_ms`  | Web | The origin first byte latency, in milliseconds. | 
|  `edge_location`  | Web | The Amazon CloudFront edge location that served the request. | 
|  `cache_status`  | Web | The cache status of the request. | 
|  `trace_id`  | Backend | The value of the `x-amplify-trace-id` header, or the line’s own `request_id` when the call has no such header. It’s never blank. Web lines don’t have this field. | 
|  `branch`  | Backend | The backend branch that served the call. | 
|  `error_reason`  | Backend | The code of the error, such as `42501`. Absent on success. | 
|  `auth_method`  | Backend | The credential that the call sent. For example, `apikey` means the publishable key, and `bearer` means a user’s JSON Web Token (JWT). The value `sigv4` means a request signed with AWS Signature Version 4. The value `signin_token` means an AWS Sign-In access token. | 
|  `auth_role`  | Backend | The role that the call ran as, such as `anon` or `authenticated`. | 
|  `auth_result`  | Backend | Whether the backend accepted the call’s credential: `success`, `failure`, or `unknown`. | 
|  `auth_event`  | Backend | The auth event on an `/auth` call, such as `sign_in` or `sign_up`. Other values are `sign_out`, `token_refresh`, `password_reset`, and `oauth_callback`. | 
|  `auth_user_id`  | Backend | The ID of the user who made the call. On the call that a user makes to sign in or sign up, it’s the ID of that user. | 
|  `auth_aws_principal`  | Backend | The ARN of the AWS identity that made the call. It appears when `auth_method` is `sigv4` or `signin_token`. | 
|  `auth_email_outcome`  | Backend | The result of the email that an `/auth` call sends. The values are `email_sent`, `email_rate_limited`, `email_refused_invalid`, `email_refused_reserved_domain`, `email_refused_provider`, `email_refused_unavailable`, `email_skipped_cooldown`, and `email_outcome_unknown`. The value `email_skipped_cooldown` means that no email was sent because the same address got one a short time before. The `/auth` call returns a 2xx status code before the email sends, so check this field for a refused email. | 
|  `auth_email_message_id`  | Backend | The message ID of the email that the `/auth` call sent. | 
|  `rows_returned`  | Backend | The number of rows that a Data API call returned. | 

For `anon` calls, the `client_ip` and `host` fields in backend access logs aren’t verified. Don’t use them to identify who made a call.

### What error\_reason means
<a name="using-access-logs-error-reasons"></a>

The `error_reason` field is on backend lines only. It holds the code of the error, such as the SQL state (SQLSTATE) code `42501` or the Data API code `PGRST116`. It’s absent on success.

On a 5xx response, it holds a general value such as `internal_server_error`. For a statement timeout, the response body and the database log show the `57014` code. It can also hold one of the following values:
+  `throttled` – A rate limit or a capacity limit held the request back.
+  `unavailable` – Something on the request path couldn’t serve the request right now. A retry might succeed.
+  `internal_server_error` – A server error.
+  `invalid_token`, `expired`, or `malformed` – The backend refused the credential.

For more information about Data API error codes, see [PostgREST error codes](https://docs.postgrest.org/en/stable/references/errors.html) on the PostgREST website. For more information about Auth errors, see [Better Auth errors](https://better-auth.com/docs/reference/errors) on the Better Auth website.

A unique, foreign key, or exclusion constraint can refuse a write. So can a row-level security check. When one does, your backend returns `409` with `WRITE_CONFLICT`. A write that runs as `service_role` gets the SQLSTATE of the cause, such as `23505`, instead of `WRITE_CONFLICT`. The database log has the real SQLSTATE, such as `23505`.

A statement that runs past its `statement_timeout` returns `500`, and the response body’s `code` is `57014`. Read the database lines for that minute.

The following AWS Command Line Interface (AWS CLI) command lists the failed backend calls of the last hour:

```bash
aws amplifygen3 logs get --log-type access -e production --since 1h --filter '{ $.surface = "backend" && $.status_code >= 400 }'
```

## Configure access logs in the console
<a name="using-access-logs-console"></a>

Use the following procedure to turn on access logs for an environment where they are off, such as `preview`. You can also use it to send your access logs to Amazon S3.

1. Sign in to the Amplify console at https://us-east-2.console.aws.amazon.com/amplifygen3/home.

1. Choose an app.

1. In the left navigation pane, choose **Logs**.

1. At the top of the **Logs** page, choose the environment. By default, the page shows the **production** environment.

1. Choose **Enable log delivery for CloudWatch logs**.

1. (Optional) The default sampling rate is 5%, which gives you enough information to monitor your app. If you want, you can change the sampling rate.

1. (Optional) The default retention period is 1 week. You can modify the retention period from 1 day to never expire.

1. (Optional) To send your access logs to an S3 bucket, choose **Additional delivery options** and do the following:

   1. Select **Amazon S3** to send your access logs to an S3 bucket.

   1. For **Sampling rate**, enter a sampling rate from 0 to 100 percent. This is the percentage of logs from your Amplify app that Amplify sends to your S3 bucket. If you use the default rate of 5% when you turn on access log delivery, Amplify sends 5% of all your logs to your S3 bucket. We recommend a sampling rate of 100% for delivery to Amazon S3. A higher sampling rate stores more logs and increases your Amazon S3 storage charges.

   1. For **S3 bucket**, select a bucket. Choose **Service managed bucket** to have Amplify create a bucket on your behalf, or choose **Custom bucket** and enter an S3 bucket that you own. You can choose **Browse S3** to view all the buckets in this AWS Region.

1. Choose **Enable**.

## Configure access log delivery using the CLI
<a name="_configure_access_log_delivery_using_the_cli"></a>

Use `aws amplifygen3 logs edit` to set the access log sampling rate for each destination. The rate is the percentage of requests (0 to 100) recorded in the logs sent to that destination. A rate of `0` turns delivery to that destination off. Settings that you don’t pass stay the same. Access logs are on by default in the production environment and off by default in all other environments.

Logs sent to Amazon S3 need a bucket. Name a bucket that you own with `--access-log-s3-bucket`, as a bucket name or an ARN, or pass `--managed-bucket` to use the app’s managed bucket. If you use your own bucket, its bucket policy must let CloudWatch Logs (`delivery.logs.amazonaws.com`) write to it. The following AWS CLI command delivers access logs to CloudWatch Logs at 5% and to an S3 bucket that you own at 100%:

```bash
aws amplifygen3 logs edit --env production \
  --access-log-cwl-sampling-rate 5 \
  --access-log-s3-sampling-rate 100 \
  --access-log-s3-bucket arn:aws:s3:::amzn-s3-demo-bucket
```

The app’s environments share one managed bucket per log type, and Amplify creates it if none exists yet. The following AWS CLI command delivers access logs to the app’s managed bucket instead:

```bash
aws amplifygen3 logs edit --env production \
  --access-log-cwl-sampling-rate 5 \
  --access-log-s3-sampling-rate 100 \
  --managed-bucket
```

To turn off access logs for an environment, including production, set both rates to `0`. Run the following AWS CLI command:

```bash
aws amplifygen3 logs edit --env <environment> --access-log-cwl-sampling-rate 0 --access-log-s3-sampling-rate 0
```

To view the access logs for an environment, run the following AWS CLI command:

```bash
aws amplifygen3 logs get --log-type access --env <environment>
```

For all log options, including firewall and database logs, see the [CLI reference](aws-cli-support-chapter.md).

## Sampling and defaults
<a name="using-access-logs-sampling"></a>

By default, 5% of `production` access logs go to CloudWatch Logs. Access logs are off in other environments. Backend lines use the same rate.

Rate-limited auth routes keep every line at any rate above 0. These include the routes that users call to sign in, sign up, and change or reset a password. They also include the routes for email change and verification, token mode, and OAuth 2.0. The `GET /auth/token` route isn’t one of them.

When a page’s backend calls send the page’s ID in `x-amplify-trace-id`, sampling keeps or drops them together with the page.

A rate change takes effect in up to 3 minutes. The new rate applies only to requests served after the change.

To see the sampling rates and then capture every request while you debug, run the following AWS CLI commands. Replace `<environment-name>` with the name of the environment, such as `production`:

```bash
aws amplifygen3 logs-config get -e <environment-name>
aws amplifygen3 logs-config edit --access-log-cwl-sampling-rate 100 -e <environment-name>
```

CloudWatch Logs charges you to ingest and store access logs and database logs. For more information, see [Amazon CloudWatch pricing](https://aws.amazon.com/cloudwatch/pricing/). To reduce cost, lower the sampling rate.

## Find a backend request by its request ID
<a name="using-access-logs-request-id"></a>

To find one backend call, filter on `request_id`. Use the value of the response’s `x-request-id` header, or the client SDK’s `error.requestId`. Replace `<request-id>` with the request ID:

```bash
aws amplifygen3 logs get --log-type access -e production --since 1h --filter '{ $.request_id = "<request-id>" }'
```

If the command returns no line, either the call is outside the time window or sampling dropped it.

## Find the database lines for a backend call
<a name="using-access-logs-database-lines"></a>

Database lines have no request ID, so match them to a call by branch, time, and SQLSTATE. From the call’s backend access line, note `branch`, `event_timestamp`, and `error_reason`. Then read the database lines of the same branch for the same minute. Filter on `branch` and `sql_state_code`.

When `error_reason` is `WRITE_CONFLICT`, the database line has the real SQLSTATE, such as `23505`. In that case, filter on `error_severity` `ERROR` instead of `sql_state_code`.

To read the database lines for the call, run the following AWS CLI command. Replace `<start-time>` and `<end-time>` with ISO 8601 times around the call, such as `2026-11-20T18:04:00Z`. Replace `<branch>` with the `branch` value from the access line, and `<sql-state-code>` with the SQLSTATE from `error_reason` or, for a 5xx call, from the response body:

```bash
aws amplifygen3 logs get --log-type database -e production --start-time <start-time> --end-time <end-time> --filter '{ $.branch = "<branch>" && $.sql_state_code = "<sql-state-code>" }'
```

Database lines can take up to 3 minutes to arrive. For more information, see [Database logs](monitoring-database-logs.md).

## Follow one page load through your app
<a name="using-access-logs-trace"></a>

A page’s backend calls share the page’s ID as `trace_id` when your server code sends the page request’s `x-amz-cf-id` value in the `x-amplify-trace-id` header. The page’s ID is the `request_id` on its web line. The following TypeScript example for Next.js sends the header:

```typescript
import { headers } from 'next/headers'
import { createClient } from '@aws-amplifygen3/backend-client-js'

const pageId = (await headers()).get('x-amz-cf-id')
const backend = createClient(process.env.AMPLIFY_BACKEND_URL, process.env.AMPLIFY_BACKEND_PUBLISHABLE_KEY, {
  global: { headers: pageId ? { 'x-amplify-trace-id': pageId } : {} },
})
```

To list the backend calls of one page load, run the following AWS CLI command. Replace `<page-id>` with the `request_id` of the page’s web line:

```bash
aws amplifygen3 logs get --log-type access -e production --since 1h --filter '{ $.trace_id = "<page-id>" }'
```

For a call that failed, read the database logs for the same minute. For more information, see [Database logs](monitoring-database-logs.md). If the problem started after a deployment, read that deployment’s log. For more information, see [View your build logs](deploy-your-app-build-logs.md).

The following example shows the web line, a backend line, and the database line for a save that failed because a note with the same title exists:

```json
{"event_timestamp":1795003859910,"surface":"web","request_id":"EXAMPLEcfId4Lb2Qx9Wm7Tz3Hp8Kd6Ns1Vf5Rj0Yc2Ue==","method":"POST","path":"/notes","status_code":200,"time_taken_ms":412}
{"event_timestamp":1795003859980,"surface":"backend","request_id":"EXAMPLEa1Bc2De3Fg4Hi5Jk6Lm7No8Pq9Rs0Tu1Vw2Xy3Za4Bc5De6Fg7Hi8Jk9==","branch":"production","trace_id":"EXAMPLEcfId4Lb2Qx9Wm7Tz3Hp8Kd6Ns1Vf5Rj0Yc2Ue==","method":"POST","path":"/rest/notes","status_code":409,"error_reason":"WRITE_CONFLICT","auth_method":"bearer","auth_role":"authenticated","auth_result":"success","time_taken_ms":38}
{"event_timestamp":1795003860000,"branch":"production","error_severity":"ERROR","sql_state_code":"23505","event_message":"duplicate key value violates unique constraint \"notes_title_key\"","user_name":"api","session_id":"EXAMPLE.1a2b"}
```