Bakir Jusufbegovic
Bakir Jusufbegovic

Reputation: 2976

How to locally debug AWS SAM Lambda function (Python) which references layer that exists on different git repo/project

Summary of my problem:

Project configuration:

What I'm able to do currently:

What happens when I try to step into method from the layer:

Techs/libs that I use:

Command that I run (before connecting through vscode to debugging session) is:

sam local generate-event apigateway aws-proxy | sam local invoke MyFunction -d 5890 --event inputs/my_event.json

AWS SAM template (function section) looks like this:

MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub "${Environment}_${Branch}_my_function"
      CodeUri: my_function.zip
      Handler: app.handler
      Timeout: 180
      MemorySize: 512
      Layers: # to be substituted with layer arn
        - <LAYER_ARN>:<LAYER_VERSION>

If I'm correct, when I run command from above, following happens:

  1. Docker container gets created from SAM Docker image
  2. Docker container contains layer downloaded from AWS
  3. SAM unzips content from my_function.zip and mounts content into the docker container
  4. Docker container has 5890 debugging port exposed so that we are able to connect from the vscode (for example) to the debugging session

My theory is that, as long as the layer is downloaded from the AWS, I will not be able to see that code in debugging session in vscode ? So, one thing that I'm thinking, but not sure if it is possible is to, instead of referencing LAYER_ARN, I reference specific folder on my local machine where layer code exists.

Question 1: If my theory is correct, is it possible to do this and how? Question 2: If my theory is not correct, are there any other ways to achieve debugging of the layer code in this setup?

Upvotes: 1

Views: 1441

Answers (1)

Vishnu Pradeep
Vishnu Pradeep

Reputation: 31

I am not sure, if we can invoke lambda layers using SAM. It was not working last I checked. However, You can invoke a lambda like this:

sam build "mylambda" && sam local invoke "mylambda"

The above one is just for Python. For node.js you can just do

sam local invoke "mylambda"

If you want to pass events, you can do

sam build "mylambda" && sam local invoke "mylambda" -e mylambdaevt.json

You can even pass environment varialbles in another json file.

sam build "mylambda" && sam local invoke "mylambda" --env-vars local-env.json -e events/mylambdaevt.json

Upvotes: 0

Related Questions