softengaws

Python, Lambda and pydantic

Testing Lambda Functions Locally

It’s possible to test lambda functions on your local machine provided you have a working Docker or Podman instance running. You can also test integration with other AWS services inc AWS Sagemaker Endpoints.

Install Dependencies

  • Make sure you have the aws-cli and aws-sam-cli packages installed. On MacOS, assuming you have brew set up, you can run brew install aws-sam-cli awscli

  • Set up your AWS credentials. This is particularly important if your function is tightly integrated with other AWS systems like Sagemaker since the emulator will need to authorize API calls.

  • When working with Lambda functions and pydantic make sure to use the correct CPU architecture and Python version (see here).

Bundle Function

We need to bundle up the app and any dependencies ready for use by Lambda. We can create a zip file containing the application code and dependencies.

Configure SAM template

The main thing we need to do here is set up SAM so that an API gateway is correctly configured and points to the lambda function and that the lambda function uses the bundled zip that we created.

Below is a minimal example of this syntax.

 
Resources:
  ApiGateway:
    Type: AWS::Serverless::Api
    Properties:
      Name: example-api-gateway
      StageName: !Ref EnvironmentName
      ApiKeySourceType: HEADER
      Auth:
        ApiKeyRequired: true
        UsagePlan:
          CreateUsagePlan: PER_API
          UsagePlanName: example-usage-plan
          Description: Usage plan
          Quota:
            Limit: 1000
            Period: DAY
          Throttle:
            BurstLimit: 2
            RateLimit: 2
      MethodSettings:
        - ResourcePath: "/*"
          HttpMethod: "*"
          ThrottlingBurstLimit: 2
          ThrottlingRateLimit: 2
 
  LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: main.lambda_handler
      Runtime: python3.11
      CodeUri: ../lambda.zip
      FunctionName: example-lambda-function
      Role: !Ref LambdaRoleArn
      Timeout: 60
      Environment:
        Variables:
          ENVIRONMENT: !Ref EnvironmentName
      Events:
        ApiEvent:
          Type: Api
          Properties:
            RestApiId: !Ref ApiGateway
            Path: /{proxy+}
            Method: ANY
            Auth:
              ApiKeyRequired: true
 

Start Emulator

sam local start-api \
   --template-file ./sam.yaml \
   --region us-east-1 \
   --profile my-app