Run AWS Lambda Function Locally

Amirhossein Soltani
3 min readJan 14, 2023

--

In this practical article we will see how we can run a single lambda function on our local machine.

Required Tools

First of all you need to have some tools installed on your computer:

  1. AWS SAM CLI
    To run an AWS Lambda function locally, you need to use the sam local command provided by the AWS SAM CLI.
    Run pip install aws-sam-cli in command line to install AWS SAM CLI or download the installation file from AWS sam installation page.
  2. Docker
    You need to have Docker installed and running to be able to run your lambda function locally, to install Docker check the Docker installtion page.
  3. Binary file of your Code
    You need to generate a binary file of your code, as an example I’m going to use a simple GoLang hello world application and create a binary file out of it.(If you don’t have the GoLang installed you can see the instructions on how to install it in my GoLang Installation article)
    This function expects to receive a JSON object with a “name” field in the input event. It will return a string containing the message “Hello, [name]!”, where [name] is the value of the “name” field.
    Create a “main.go file in your workspace and put the below code in it.
package main

import (
"context"
"fmt"

"github.com/aws/aws-lambda-go/lambda"
)

type MyEvent struct {
Name string `json:"name"`
}

func HandleRequest(ctx context.Context, event MyEvent) (string, error) {
return fmt.Sprintf("Hello, %s!", event.Name), nil
}

func main() {
lambda.Start(HandleRequest)
}

Now in the same directory you need to also have a “Makefile”, to do so create a new file and call it “Makefile” and put the below code in it.

build:
mkdir -p bin/hello_world
env GOOS=linux go build -ldflags="-s -w" -o bin/hello_world/main main.go

By Executing the “Makefile” your code binary file will be generated, To execute the “Makefile” you can run the command make build.

Then The output should be generated in “bin/hello_world”.

Generate Binary File

Now You have all The required tools and you are almost ready to run your lambda function Locally.

Sam Template

Now, navigate to the directory containing your Go-based Lambda function code and create a template.yaml file that specifies the function configuration. Here is an example template.yaml file that defines a Go-based Lambda function:

Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: hello-world-function
CodeUri: bin/hello_world/
Handler: main
Runtime: go1.x

Make sure to give a correct path to the directory of the generated binary file in the “CodeUri” section, in our case it should be “bin/hello_world”.

Then, create an event.json file that specifies the input event to be passed to the function. For example, this event.json file might contain a JSON object with the input event data:

{
"name": "world"
}

Run the following command to execute your function locally :)

sam local invoke -t template.yaml -e event.json

This will execute your Go-based Lambda function using the input event specified in event.json, and the output will be displayed in the terminal.

Note that the very first time you run the execution command,Runtime may take longer since the Docker needs to build the application image.

From the output of the the invocation you can get some other insights about how much memory your application used and how long did the execution take.

Now you know how to run your AWS Lambda functions locally, Congratulations!

I have more articles about the serverless world and I also write articles and stories about Life, Coding, IT Lessons and Technology, If you’d care to read more, follow me on Medium :)

--

--

Amirhossein Soltani

📍Amsterdam🇳🇱 /n *Tech Lover /n *Software Engineer /n *Tryna be a lovely geek