Ruan Bekker's Blog

From a Curious mind to Posts on Github

Testing AWS Lambda Functions Locally on Docker With LambCi

Say Thanks! Slack Status Chat on Slack GitHub followers

I discovered a Docker image called LambCi that allows you to test lambda functions locally on docker and wanted to share with you how it works.

Python Lambda Function

We will create a basic lambda function to demonstrate how it works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ mkdir task
$ cat > task/lambda_function.py << EOF
import json

def lambda_handler(event, context):
    if event:

        try:
            event['name']
            name = event['name']
            output_string = 'My name is {}'.format(name.capitalize())

        except KeyError:
            output_string = 'A name was not defined in the event payload'

    return output_string
EOF

Now that we’ve created the function, run the docker container with the parameters of the functions handler method and the event parameters:

1
2
3
4
5
$ docker run --rm -v "$PWD/task":/var/task lambci/lambda:python3.7 lambda_function.lambda_handler '{"name": "ruan"}'
START RequestId: 70025895-1233-1362-8006-c2784b5d80b6 Version: $LATEST
END RequestId: 70025895-1233-1362-8006-c2784b5d80b6
REPORT RequestId: 70025895-1233-1362-8006-c2784b5d80b6    Duration: 7.51 ms   Billed Duration: 100 ms Memory Size: 1536 MB    Max Memory Used: 23 MB
"My name is Ruan"

And another call:

1
2
3
4
5
$ docker run --rm -v "$PWD/task":/var/task lambci/lambda:python3.7 lambda_function.lambda_handler '{"nam": "ruan"}'
START RequestId: f7ab2e97-05db-1184-a009-11b92638534f Version: $LATEST
END RequestId: f7ab2e97-05db-1184-a009-11b92638534f
REPORT RequestId: f7ab2e97-05db-1184-a009-11b92638534f    Duration: 5.32 ms   Billed Duration: 100 ms Memory Size: 1536 MB    Max Memory Used: 23 MB
"A name was not defined in the event payload"

Checkout the dockerhub page for more info: - https://hub.docker.com/r/lambci/lambda/

Thank You

Let me know what you think. If you liked my content, feel free to checkout my content on ruan.dev or follow me on twitter at @ruanbekker

-->

Comments