Skip to content

Instantly share code, notes, and snippets.

@ruanbekker
Created November 13, 2019 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruanbekker/8a71077d0e4bce8f1379e0bea5208804 to your computer and use it in GitHub Desktop.
Save ruanbekker/8a71077d0e4bce8f1379e0bea5208804 to your computer and use it in GitHub Desktop.
Local Lambda testing with Lambci Docker Container

Source:

Create the function:

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

Run the docker container:

$ 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"
$ 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment