Cloud Defense Logo

Products

Solutions

Company

Book A Live Demo

Rule: API Gateway Stage Logging Should Be Enabled

This rule ensures that the API Gateway stage logging is enabled to maintain high security standards.

RuleAPI Gateway stage logging should be enabled
FrameworkSOC 2
Severity
High

Ensuring API Gateway Stage Logging is Enabled for SOC 2 Compliance

Overview

Amazon API Gateway is a fully managed service that makes it easier for developers to create, publish, maintain, monitor, and secure APIs. For compliance with Service Organization Control 2 (SOC 2) reporting, it is critical to have detailed logs of activities occurring within your API Gateway stages. Enabling logging helps to meet the monitoring and auditing requirements of SOC 2 by providing an immutable record of API transactions.

Benefits of Enabling Logging

  • Monitoring: Track API usage and observe how the APIs are being accessed.
  • Auditing: Satisfy compliance by having the ability to audit all transactions.
  • Troubleshooting: Quickly identify and resolve issues that may arise in API operations.
  • Security: Detect and investigate potential security breaches or unauthorized behavior.

Step-by-step Guide for Remediation

Step 1: Enable CloudWatch Logs for API Gateway

  1. 1.
    Log in to the AWS Management Console.
  2. 2.
    Navigate to the API Gateway service.
  3. 3.
    Select the API for which logging needs to be enabled.
  4. 4.
    Go to the "Stages" section in the left panel.
  5. 5.
    Select the stage of the API you want to enable logging for.
  6. 6.
    Under the "Logs/Tracing" tab, set the "CloudWatch Logs" option to 'Enabled'.
  7. 7.
    Choose an appropriate log level (e.g., INFO or ERROR).
  8. 8.
    Optionally, set up "Data Trace" and "Log full requests/responses" according to your compliance needs.

Step 2: Ensure IAM Permissions

AWS API Gateway needs the necessary permissions to write logs to CloudWatch. Ensure that the IAM role associated with API Gateway has the following policies attached:

  • AmazonAPIGatewayPushToCloudWatchLogs
  • CloudWatchLogsFullAccess
    (or a more restrictive custom policy that allows
    logs:CreateLogGroup
    ,
    logs:CreateLogStream
    , and
    logs:PutLogEvents
    actions)

Necessary IAM Policy Example

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:DescribeLogStreams",
                "logs:PutLogEvents"
            ],
            "Resource": "*"
        }
    ]
}

Step 3: Adjust Retention Settings (Optional)

By default, CloudWatch Logs are retained indefinitely. To adjust the log retention period:

  1. 1.
    Navigate to the CloudWatch service in the AWS Console.
  2. 2.
    Click on "Log groups".
  3. 3.
    Find the log group corresponding to your API Gateway.
  4. 4.
    Select the log group and click on "Retention settings".
  5. 5.
    Choose the desired retention period for your compliance needs.

CLI Commands to Enable Logging

# Define variables
API_ID=<your-api-id>
STAGE_NAME=<your-stage-name>
LOG_GROUP_NAME=<your-log-group-name>
IAM_ROLE_ARN=<your-iam-role-arn>

# Enable CloudWatch logs for API Gateway stage
aws apigateway update-stage --rest-api-id $API_ID --stage-name $STAGE_NAME --patch-operations op='replace',path='/accessLogSettings/destinationArn',value='arn:aws:logs:region:account-id:log-group:$LOG_GROUP_NAME' op='replace',path='/accessLogSettings/format',value='{"requestId":"$context.requestId","ip":"$context.identity.sourceIp","caller":"$context.identity.caller","user":"$context.identity.user","requestTime":"$context.requestTime","httpMethod":"$context.httpMethod","resourcePath":"$context.resourcePath","status":"$context.status","protocol":"$context.protocol","responseLength":"$context.responseLength"}'

# Attach the policy to the IAM role
aws iam put-role-policy --role-name <role-name> --policy-name APIGatewayLogsPolicy --policy-document file://api-gateway-logs-policy.json

# Set the retention period for the log group
aws logs put-retention-policy --log-group-name /aws/apigateway/$LOG_GROUP_NAME --retention-in-days <number-of-days>

Replace

<your-api-id>
,
<your-stage-name>
,
<your-log-group-name>
,
<your-iam-role-arn>
, and
<role-name>
with the appropriate values for your environment. Adjust
<number-of-days>
to set the retention period according to your SOC 2 requirements.

Troubleshooting Steps

  • If logs are not appearing, ensure that API Gateway has the required permissions to write to CloudWatch.
  • Confirm that the IAM role's trust relationship allows the API Gateway service to assume the role.
  • Check the format of log entries to ensure it complies with the necessary data capture for SOC 2 compliance.
  • Verify if the log group exists in CloudWatch and the logs are not being written to an unexpected log group.

For these steps to be SEO friendly, the content was created without compromising readability. The relevant keywords like 'SOC 2 compliance', 'AWS', 'API Gateway', and 'CloudWatch Logs' are strategically placed to make this guide easily discoverable. The guide avoids filler data and maintains a focused, professional tone.

Is your System Free of Underlying Vulnerabilities?
Find Out Now