DEV Community

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

Posted on • Edited on

Enable API Gateway logs

Recap

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

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

  lambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: simplefunction.handler
      CodeUri: lambdafunction/
      Runtime: python3.8
      Policies:
        - CloudWatchLambdaInsightsExecutionRolePolicy
      AutoPublishAlias: live
      Description: Simple demo function
      MemorySize: 128
      Timeout: 10
      Tracing: Active
      Layers:
        - !Sub "arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension:14"
      Events:
        simpleApi:
          Type: Api
          Properties:
            RestApiId: 
              !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)