paint-brush
How to Debug AWS Lambda Code with PyCharmby@susamn
4,473 reads
4,473 reads

How to Debug AWS Lambda Code with PyCharm

by Supratim SamantaJuly 5th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

PyCharm allows you to debug your code line by line in your local environment. Using the project structure, we can set up the code in VS Code also. We will create the environment where the Lambda function will run and use the real event to trigger the call. We must have the AWS credentials available in the home/aws/credentials configure. The full project is here. The events folder will have all the events which will invoke my function locally. The python folder and all the components inside it are the layer codes. We can add any number of layers here. We will add any third-party layer code also.
featured image - How to Debug AWS Lambda Code with PyCharm
Supratim Samanta HackerNoon profile picture

Recently I have been doing a lot of lambda coding in AWS. After doing a lot of web services, I was really missing the ability to debug my code line by line in my local environment. I use PyCharm CE a lot, and to have PyCharm debug my lambda code, was to me, an awesome idea. Regardless, using the following way, we can set up the code in VS Code also.

I could not find the proper tutorial on the internet. All the topics were around using SAM CLI, which I didn’t want to use.

Prerequisites

We must have the AWS credentials available in the home 

~/.aws/credentials
. See this link to configure AWS CLI. The full project is here.

Let’s start…

Here is my sample lambda function.

A sample lambda function with 2 triggers, one from S3-Put and another from SQS receive message.

This function also has some environment variables.

The function also has a layer. Here is the code for the layer

# dummy_layer.py
def call_dummy():
    return "Hello from dummy layer"

To properly set it up in PyCharm we will use this project structure.

Things to note

1. The events folder will have all the events which will invoke my lambda function locally.

2. The python folder and all the components inside it are the layer codes. We can add any number of layers here.

3.

.env
 file will have all the environment variables for the lambda

4.

lambda_function.py
 is the actual function code. We can just copy the function from AWS console and put it here.

5.

run.py
 is the file that will run the lambda function after loading the proper event.

Setup

We will copy the lambda code from AWS, if available already, or will write the code, debug it and then deploy it. We will create the environment where the lambda function will run and use the real event to trigger the call.

AWS resources can also be referenced like shown in the code. The 

boto3
 client will use the 
~/.aws/credentials
 file to create a connection with AWS, so the CLI configuration is a must.

We will add any third-party layer code also.

1. Copy the lambda code from AWS console to the lambda_function.py file.

In our case, we have:

import json

import boto3

from dummy_layer import call_dummy

s3 = boto3.client("s3")


def lambda_handler(event, context):
    print(call_dummy())
    print(s3.list_buckets())
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

2. Add the layer code as source set.

We need to add the lambda layer code as the python source set so that we can import them into the lambda function.

3. Where to get the events

We can get sample events from the test configuration in the lambda code console:

The cloudwatch logs, will be actual events invoking the function.

The AWS Toolkit plugin in PyCharm:

4. Code the run.py file

Let’s load the s3 put file to simulate a put event and invoke the function.

import json

from lambda_function import lambda_handler


class Context:
    def __init__(self, arn: str):
        self.invoked_function_arn = arn


if __name__ == "__main__":
    event = {}
    with open("events/s3-put.json", 'r') as f:
        event = json.load(f)
        lambda_handler(event, Context("Sample ARN"))

5. Configure .env file

Install the .env file support plugin from jetbrains marketplace.

Add all the environment configuration in the .env file for local debugging. This is simulating the environment variable configuration in the AWS lambda console.

6. Create run configuration.

Finally, create a python run configuration for the run.py file with the .env file setup. This is how we simulate the environment variable, just like in AWS console:

That’s it …

We are done.

Now just run the run configuration and debug away.

Also published on Medium's subdomain behind a paywall.