How to enable logging and tracing for AWS API GW via Swagger (or template)

Viewed 1633

I have an AWS SAM template and I referenced a Swagger file as API configs. I'm trying to enable logging and tracing for this API. Couldn't find a resource that has this configs. Not sure also if these enabling logs and tracing is a thing in the template or in Swagger file.

enter image description here

Any help?

2 Answers

I believe the MethodSettings property under api gateway can help you out

  Type: AWS::Serverless::Api
    Properties:
      EndpointConfiguration: REGIONAL
      MethodSettings:
      - LoggingLevel: ERROR # ERROR/INFO level logging 
        ResourcePath: '/*' # allows for logging on any resource
        HttpMethod: '*' # allows for logging on any method
        DataTraceEnabled: true # enabling Log full requests/responses data in console
        MetricsEnabled: true    # Enable detailed metrics (error 404, latence, ...)

You're right, there isn't an option to enable logs via Swagger. Swagger just lets you define the structure of a REST API - resources, integrations, mappings etc. Also, Swagger can be used for defining any REST API; not just Amazon API Gateway. So, it makes sense that you have option to enable logs on a property that's specific to API Gateway - which is a Stage.

If you are creating your API via CloudFormation or some other automated way, you would need to create a deployment for your API. Once you associate this deployment to a API Gateway stage, you can configure settings for logging on it. For example with CloudFormation, you would use AWS::ApiGateway::Deployment StageDescription. Check out this link for details.

Related