View a markdown version of this page

Database logs for your app - AWS Amplify

Database logs for your app

Database logs are the PostgreSQL log of your app’s backend database. They record errors with their statement. They also record statements of 1 second or longer, lock waits of 1 second or longer, and schema changes. For migrations, they record each migration file that changes the schema, as one statement, and failed statements. They don’t record faster statements, so they aren’t a record of every query. If your app has a backend, database logs are on by default in every environment.

Failed migrations

To see why a deployment’s migration failed with MIGRATION_FAILED, read its build log. For more information, see View a deployment’s migrations in its build log and Fix a failed migration.

Database log delivery

Lines go to the Amazon CloudWatch Logs (CloudWatch Logs) log group /aws/amplifygen3/app-name/environment-name/database-logs. On this page, app-name is your app’s name and environment-name is the environment’s name.

Lines take up to 3 minutes to arrive. Lines have no request ID, so match them to a call by time.

You pay CloudWatch Logs charges to ingest and store database logs. For more information, see Amazon CloudWatch pricing.

Each branch has a limit on the number of lines.

To see the settings or change the sampling rate for the log group, run the following AWS Command Line Interface (AWS CLI) commands:

aws amplifygen3 logs-config get -e environment-name aws amplifygen3 logs-config edit --database-log-cwl-sampling-rate rate -e environment-name

Replace rate with the percentage of lines to deliver, from 0 to 100. Set the rate to 0 to stop delivery to the log group, or above 0 to start it.

The following AWS CLI command stops database log delivery to CloudWatch Logs:

aws amplifygen3 logs-config edit --database-log-cwl-sampling-rate 0 -e environment-name

To start delivery again, run the same command with the rate that you want.

Deliver database logs to Amazon S3

You can also deliver database logs to an Amazon Simple Storage Service (Amazon S3) bucket. Each destination has its own sampling rate, from 0 to 100. To turn on delivery to Amazon S3, pass --database-log-s3-sampling-rate with a rate above 0. When you set one sampling rate, the other destination keeps its setting. You pay Amazon S3 charges to store the logs that you deliver to Amazon S3. For more information, see Amazon S3 pricing.

To deliver logs to Amazon S3, you need a bucket. In the same command as --database-log-s3-sampling-rate, pass one of the following options:

  • --database-log-s3-bucket – A bucket in your account, as a bucket name or an ARN, such as arn:aws:s3:::amzn-s3-demo-bucket. Its bucket policy must let CloudWatch Logs (delivery.logs.amazonaws.com) write to it. The command doesn’t change your bucket policy.

  • --managed-bucket – The app’s managed bucket. The app’s environments share one managed bucket for each log type. If none exists yet, the command creates it. A new managed bucket blocks public access and uses server-side encryption with Amazon S3 managed keys (SSE-S3).

The following AWS CLI command delivers every database log line to a bucket that you own:

aws amplifygen3 logs-config edit --database-log-s3-sampling-rate 100 --database-log-s3-bucket amzn-s3-demo-bucket -e environment-name

To use the app’s managed bucket instead, replace --database-log-s3-bucket amzn-s3-demo-bucket with --managed-bucket. The bucket stays set until you name another one. To move logs to another bucket, pass --database-log-s3-sampling-rate again with the new bucket.

To stop delivery to Amazon S3, pass --database-log-s3-sampling-rate 0. To see the bucket, run aws amplifygen3 logs-config get -e environment-name . For database logs, its output table has a row labeled CloudWatch for CloudWatch Logs and a row labeled S3 for Amazon S3.

Database log fields

The following table lists the fields of each database log line.

Field Description

resource_arn

The ARN of the environment, followed by the destination, /CWL or /S3.

event_timestamp

The time of the line, in milliseconds since the Unix epoch.

database_name

The name of the database.

branch

The backend branch whose database wrote the line.

error_severity

The severity of the line, such as LOG or ERROR.

sql_state_code

The SQLSTATE code of the line, such as 23505. A line without an error has 00000. For more information about SQLSTATE codes, see PostgreSQL error codes on the PostgreSQL website.

event_message

The text of the line.

user_name

The database user of the session. It’s api for the Data API’s session, migration for a deployment’s migration session, and amplify_admin for a direct connection.

application_name

The application name of the session. It’s api or migration for those two sessions.

session_id

The ID of the database session.

transaction_id

The ID of the transaction, or 0 if the transaction has no ID.

deployment_id

On a migration line, the ID of the deployment that ran the migration.

To list the database errors of the last hour, run the following AWS CLI command:

aws amplifygen3 logs get --log-type database -e environment-name --since 1h --filter '{ $.error_severity = "ERROR" }'

The filter returns only ERROR lines. Each error’s statement is on its own line with error_severity STATEMENT and the same session_id.

To read one backend branch, add --branch-arn branch-arn . Direct connections, such as aws amplifygen3 db connect, aws amplifygen3 db query, and SQL clients, log with user_name amplify_admin.

Database performance

To find when the database became slow or busy, start with the BackendLatency and DatabaseConnections metrics. For each metric, see CloudWatch metrics.

Then read the database logs for that time. A connection that the database refused at its connection limit is a FATAL line with the sql_state_code 53300. For the limit, see Quotas.

To read your database’s statistics views for one diagnostic, use aws amplifygen3 db inspect name . Diagnostics include table-stats, index-stats, bloat, locks, blocking, long-running-queries, and vacuum-stats. To run every diagnostic and write one comma-separated values (CSV) file for each to the directory in --output-dir, use aws amplifygen3 db report.

To use a branch other than the environment’s backend branch, add --branch-arn branch-arn .

The following AWS CLI commands inspect table statistics and write a report:

aws amplifygen3 db inspect table-stats --env environment-name aws amplifygen3 db report --output-dir db-report --env environment-name

For the BCU-hours that your backend used this month, see Capacity.