DEV Community

Cover image for Enable API Gateway logs
Paweł Piwosz for AWS Community Builders

Posted on • Updated on

Enable API Gateway logs


What did we do so far? We enabled tracing on API and Lambda and we enabled enhanced monitoring for Lambda as well. We should do one more thing in order to start collecting information in proper way

API logs

By default logs for API Gateway are disabled.

In API service navigate to your API, then Stages, Prod (if you created the resource from the template), select Logs/Tracing tab (yes, you were here before). For the test enable all three settings and change level to INFO.

Enable API logs

Trigger your API multiple times to generate logs. After this you should see API logs in CloudWatch.

Watch API logs

SAM template

Ok, it is time to add it to our SAM template. But wait! It is not "just add". Unfortunately, we need to rewrite almost whole template.

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: simple Lambda

    Type: AWS::Serverless::Api
      StageName: Prod
      Description: 'Prod stage'
      TracingEnabled: true
      - HttpMethod: '*'
        LoggingLevel: INFO
        ResourcePath: '/*'
        MetricsEnabled: true
        DataTraceEnabled: true

    Type: AWS::Serverless::Function
      Handler: simplefunction.handler
      CodeUri: lambdafunction/
      Runtime: python3.8
        - CloudWatchLambdaInsightsExecutionRolePolicy
      AutoPublishAlias: live
      Description: Simple demo function
      MemorySize: 128
      Timeout: 10
      Tracing: Active
        - !Sub "arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension:14"
          Type: Api
              !Ref lambdaDemoApi
            Path: /
            Method: get
Enter fullscreen mode Exit fullscreen mode

I redesigned the template, removed part of the Events configuration and I created a new resource - lambdaDemoApi.

That's all, logs are enabled and all requests will be catched and stored in CloudWatch Logs. Please remember, I enabled here standard logs. There is a possibility to shape logs as you wish or need. We will come back to it. Earlier than you expect!

Cover image by Hebi B. from Pixabay

Top comments (0)