Ryan Chandler

Execute an AWS Lambda Function from the Terminal

2 min read

This post was published 3 years ago. Some of the information might be outdated!

Getting started

Before you go any further, you want to make sure you've got the AWS CLI installed. If you haven't already got it installed, you can follow the official instructions here.

Generating a payload

Unfortunately, the AWS CLI can't accept the raw contents of a file as the payload when invoking a Lambda function. Instead, you need to generate a base 64 encoded version of the file.

On Unix systems, this can be done using the base64 command. I typically store my generic payload inside of a payload-raw.json file and then output the base 64 encoded version to payload.json.

base64 payload-raw.json

This command will encode the raw JSON into base 64. To output the result of this command, we can just redirect stdout into a file.

base64 payload-raw.json > payload.json

Invoke the Lambda

You can invoke your Lambda function using the aws lambda command. This command requires a few options so that it knows which function to execute.

aws lambda \
--function-name NameOfFunctionHere

If we want to send through our base 64 encoded payload, we can use the --payload option and specify the file that needs to be used.

aws lambda \
--function-name NameOfFunctionHere
--payload file://payload.json

You need to prefix the file with file:// protocol as the AWS CLI also supports sending payloads using http:// and https://.

If you want to save the output of the execution to a file, you can specify the output file at the end of the command.

aws lambda \
--function-name NameOfFunctionHere
--payload file://payload.json
output.json