CloudWatch Logs and alarms

Overview

By default, the PostgreSQL log of an RDS DB instance stays on the instance and is kept for a few days. When you publish it to Amazon CloudWatch Logs, you can search it, keep it as long as you need, and analyze it together with the logs of your applications.

You also create a CloudWatch alarm on the CPUUtilization metric. When the average CPU is at least 50% for one minute, the alarm sends an email through an Amazon SNS topic.

Publish the PostgreSQL log to CloudWatch Logs

  1. In the Amazon RDS console, choose rdspg-lab-db, then choose Modify. In Additional monitoring settings > Log exports:

    • (1) Select PostgreSQL log and Upgrade log.

    Log exports

  2. Choose Continue:

    • (1) Check the change Enable publish to CloudWatch logs: PostgreSQL log, Upgrade log.
    • (2) Choose Apply immediately.
    • (3) Choose Modify DB instance.

    Summary of modifications

Publishing logs does not restart the database. RDS uses the service-linked role AWSServiceRoleForRDS to write to CloudWatch Logs, so you do not need to create an IAM role.

  1. After a few minutes, open the CloudWatch console:

    • (1) Choose Log Management.
    • (2) Enter /aws/rds/instance/rdspg-lab-db in the filter box.
    • (3) RDS creates one log group for each log type: .../postgresql and .../upgrade.

    Log groups

  2. Choose the .../postgresql log group, then the log stream rdspg-lab-db:

    • (1) The line FATAL: pg_hba.conf rejects connection … database “rdsadmin” is the failed vacuumdb --all command from step 2.3.
    • (2) The checkpoint starting and checkpoint complete lines show PostgreSQL writing changed pages to storage.

    Log events

New log groups keep data forever (Never expire). For your own environments, set a retention period on the log group (for example 7 or 30 days) to control the storage cost.

Create a CPU utilization alarm

  1. Go back to rdspg-lab-db in the RDS console:

    • (1) Open the Logs & events tab.
    • (2) In CloudWatch alarms, choose Create alarm.

    Create alarm

  2. In Settings:

    • Send notifications: Yes
    • (1) Send notifications to: New email or SMS topic
    • (2) Topic name: rdspg-lab-alarm-topic
    • (3) With these recipients: your email address
    • (4) Metric: Average of CPUUtilization
    • (5) Threshold: >= 50 Percent

    Alarm settings

  3. Continue in Settings:

    • (1) Evaluation period: 1 consecutive period(s) of 1 Minute
    • (2) Keep the default Name of alarm: awsrds-rdspg-lab-db-High-CPUUtilization
    • (3) Choose Create alarm.

    Create the alarm

  4. Amazon SNS sends a message AWS Notification - Subscription Confirmation to your email address. Open it and choose Confirm subscription. Then open the Amazon SNS console, choose Topics > rdspg-lab-alarm-topic:

    • (1) The email subscription is Confirmed.

    SNS subscription

SNS sends the alarm email only to confirmed subscriptions. If you do not see the confirmation message, check your spam folder. The link in the message expires after 3 days.

(Optional) AWS CLI

Publish the logs and create the alarm with the AWS CLI