AWS Lambda Command Line Automation

As one begins to write more sophisticated AWS Lambda functions, it quickly becomes clear that using shell scripts and the CLI to package, deploy and test will speed up development time. I often start in the AWS Console; then as the code becomes more complex, move to a local editor.

Packaging at the Command Line – OS X

Here’s a helpful script to package up your function. I called mine zip.sh.

#!/bin/sh
clear
rm -f Archive.zip
zip -r Archive.zip * -x *.sh *.txt

The first two lines are optional. clear is just a personal preference.

rm -f will remove any existing Archive.zip. The -f, force, means that rm won’t complain if Archive.zip can’t be found.

zip -r will recursively get all files in the current folder and associated subdirectories. -x instructs zip to ignore any .sh or .txt files such as the .sh doing the zipping.

Don’t forget to chmod +x your shell script or it won’t execute!

Deploying at the Command Line – OS X

#!/bin/sh
aws lambda update-function-code \
--function-name YOUR_FUNCTION_NAME_HERE \
--zip-file fileb://Archive.zip

Assuming you have already created your function using the AWS Console, update-function-code is what you’ll need to use do deploy the Archive.zip created above.

Again, chmod +x your shell script.

Test the Deployment at the Command Line – OS X

The following example can also find its way into a shell script.

aws lambda invoke \
--invocation-type RequestResponse \
--function-name YOUR_FUNCTION_NAME_HERE \
--region us-east-1 \
--log-type Tail \
--payload '{"key1":"12", "key2":"value2", "key3":"value3"}' \
outputfile.txt 

In my case, three shell scripts were created, then a master script to call the three individually.

Feature Photo by Dan Freeman on Unsplash

(Looks like a shell, doesn’t it?)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s