How do I create a *clean* AWS lambda function in python?

28 Views Asked by At
Problem

When I run my lambda API locally with sam local start-api, I get the error {"message":"Missing Authentication Token"} for a request that is not supposed to require an authentication token.

Code

In my cloudformation template file I create a lambda function (see below) and an API Gateway for the API calls. As this setup will fail to run locally with start-api (thank you AWS?), I added the Events section you can see below.

MyApi:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: myapi
      Handler: lambda_handler.lambda_handler
      Runtime: python3.10
      Architectures:
        - arm64
      ...
      Events:
        Any:
          Type: HttpApi
          Properties:
            Path: '/myapi/*'
            Method: any
Steps to reproduce
  • I run sam build && sam local start-api to run the API.
  • I run curl localhost:8080/myapi/ping
    • 8080 is configured in samconfig
    • /myapi/ping is configure in the API Gateway and the lambda_handler itself.
1

There are 1 best solutions below

0
DarkTrick On
Problem

I don't understand why, but Path: '/myapi/*' seems to have been the problem.

How to fix

Using Path: '/{proxy+}' instead, fixed it.

Warning: If you use this in your template that you use for deployment, you will have the same setting on your server. This is probably not what you want.

This is a community wiki. Whoever knows more, feel free to add details