Invoking Lambda Locally

Testing AWS Lambda functions using the AWS console can be tedious if you need to do it a lot. A good example is you have an IoT button and you want to keep testing your function before using the actual button, because it can be easy to burn through you ~1,000 clicks debugging a function.

Here’s a simple, example that I called addone. Create a function to add one to a number:

def handler(event, context):
    result = event['number'] + 1
    return {"Result": result}

Invoke the function using awscli, and pass in a number in the payload. Assuming your function is setup properly, and it has the right permissions, you should get a status code of 200 back:

$ aws lambda invoke --function-name addone \
    --payload '{"number": 5}' outputfile.txt
{
    "StatusCode": 200
}

Now you can look at outputfile.txt to see what was returned:

{"Result": 6}

Most of the times I’ll invoke and just cat the output file as a one liner:

$ aws lambda invoke --function-name addone \
    --payload '{"number": 100}' outputfile.txt ; cat outputfile.txt
{
    "StatusCode": 200
}
{"Result": 101}

I haven’t figured out a way to have the result from invoke just print to stdout without forking things. Anyways, hope this is useful!