Enabling logging in API Gateway for your stage is fairly easy. You go into the Console, setup a role for API Gateway to use for logging, find the stage and enable logs. It will enable logging for all methods within that stage. Doing the same configuration using CloudFormation is not completely obvious though, as the stage object’s MethodSettings property seems to allow you to only do that for a specific resource and method.

In short, this is what we are trying to accomplish within the settings for the whole Stage:
api-gateway-stage-logging

MethodSetting in CloudFormation requires a specific ResourcePath and HttpMethod, which seems to suggest, you need to specify those properties for every path and method in your API. Luckily, that’s not entirely true. While you can use this property for a very specific resource and method, you can also use it for ALL resources and method.

To do that, using troposphere, you can specify it like this:

MethodSettings=[
    apigateway.MethodSetting(
            DataTraceEnabled=True,
            HttpMethod="*",
            LoggingLevel="INFO",
            ResourcePath="/*",
            MetricsEnabled=True
        )
],

or directly in JSON:

"MethodSettings": [
          {
            "DataTraceEnabled": true,
            "HttpMethod": "*",
            "LoggingLevel": "INFO",
            "MetricsEnabled": true,
            "ResourcePath": "/*"
          }
]

In order for that to work, you also need to create a Role for API Gateway to write to logs and let API Gateway know what that role is:

from awacs import aws, sts
from troposphere import GetAtt
from troposphere import apigateway, iam

cloudwatch_role = iam.Role(
    "CloudWatchRole",
    Path="/",
    AssumeRolePolicyDocument=aws.Policy(
        Statement=[
            aws.Statement(
                Effect=aws.Allow,
                Action=[sts.AssumeRole],
                Principal=aws.Principal(
                    "Service", ["apigateway.amazonaws.com"]
                )
            )
        ]
    ),
    ManagedPolicyArns=[
        "arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
    ],
)

api_gateway_account = apigateway.Account(
    "APIGatewayAccount",
    CloudWatchRoleArn=GetAtt(cloudwatch_role, "Arn")
)

You only have to set the above once (i.e. in one CloudFormation stack in a region within your AWS account), as the API Gateway account setting is global for the whole region.

1 person found this post helpful
Was this post helpful to you? Yes!


Leave a comment