How to Build Lambda Function with SAM

Build and Test your Lambda Locally with Pycharm

Gal Hever
4 min readFeb 3, 2024

Today we will be learning how to build and test lambda function locally with PyCharm!

Create AWS Key and Configure AWS

Before we start you need to create your AWS access key. Next, configure AWS in your terminal. To do so you can use this tutorial.

Install SAM Cli

Now, install AWS SAM CLI from here. Go to Program Files > Amazon > AWSCLI > bin and write:

sam --version

If everything was successful you will see the below screen:

Install AWS toolkit plugin

Go to settings > plugins and install AWS Toolkit.

Create a new project

Create a new project and choose AWS Serverless Application.

If you want to build a docker container select Image in the check box else check zip.

Configure the path in File > Settings > Tools > AWS:

(Usually it is supposed to be written as follow C:\Program Files\Amazon\AWSSAMCLI\bin\sam.cmd)

Press on the right toolbar AWS Explorer. There you will see all the resources you have in the cloud.

Build your Lambda Function

In order to debug your Lambda locally you have few options.

  1. Open the terminal and navigate to the folder where your template.yaml exists. write in the terminalsam build. Notice that the template.yaml contains the python version installed on your machine.
  2. You can right-click on your lambda function in the AWS Explorer toolbar and press Run, then in the window configure the test that you want to invoke. Then press on Edit Configurations:

Press on Templates>AWS Lambda>Local>SAM CLI and then check Build function inside a container. Click on Apply and then Ok:

Test your Lambda function

For that you also have two options:

  1. Run in the terminal sam local invoke or if you want to check a specific event you need to put a json file in the events folder and write in the terminal sam local invoke -e events/<event_name>.json
  2. Another option is to use an external package called python-lambda-local. Install the package by running pip install python-lambda-local. You can test if it works by going to your project directory and running

python-lambda-local -f lambda_handler <lambda_function_name>.py event.json --timeout <timeout in seconds>

For example:

You can also use your own event. For example, let’s add another PUT event and test it.

Add under events folder another json file called put_event.json.

Change the name and arn to your own bucket, I called mine input-pics. Change also the key name of the object to your own. Mine is called my_pic. Now run the code:

python-lambda-local -f lambda_handler <lambda_function_name>.py events\put_event.json --timeout <timeout in seconds>

{
"Records": [
{
"eventVersion": "2.0",
"eventSource": "aws:s3",
"awsRegion": "{region}",
"eventTime": "1970-01-01T00:00:00Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "EXAMPLE"
},
"requestParameters": {
"sourceIPAddress": "127.0.0.1"
},
"responseElements": {
"x-amz-request-id": "EXAMPLE123456789",
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH"
},
"s3": {
"s3SchemaVersion": "1.0",
"configurationId": "testConfigRule",
"bucket": {
"name": "input-pics",
"ownerIdentity": {
"principalId": "EXAMPLE"
},
"arn": "arn:{partition}:s3:::input-pics"
},
"object": {
"key": "my_pic.jpg",
"size": 1024,
"eTag": "0123456789abcdef0123456789abcdef",
"sequencer": "0A1B2C3D4E5F678901"
}
}
}
]
}

Change Configurations

If you received a timeout error or memory error, you can change the configuration in the template.yaml file as below:

Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
Environment:
Variables:
HOME: /tmp
EphemeralStorage:
Size: 10240
Timeout: 900
MemorySize: 3008

If you have more parameters you want to add, you can check out this link.

Deploy

Open the teminal and write sam deploy — guided , you can skip all except of HelloWorldFunction has no authentication > Press y and continue.

Go to CloudFormaion and check the status:

If it failed press on the stack and go to Events tab to check the reason.

Congratulation!

You have finished all steps and now you can go to Lambda Functions in the cloud and find your lambda there!

End Notes

You can also watch this video which explains the whole procedure shortly. If you upload Lambda function to the cloud you have a size limit of 250MB for your lambda package. To deal with this limit you can use Docker to package your custom code and dependencies with Lambda functions container and increase this up limit to 10 GB.

--

--