DEV Community

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

Posted on • Edited on

3 1

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

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

Create a simple OTP system with AWS Serverless cover image

Create a simple OTP system with AWS Serverless

Implement a One Time Password (OTP) system with AWS Serverless services including Lambda, API Gateway, DynamoDB, Simple Email Service (SES), and Amplify Web Hosting using VueJS for the frontend.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay