Using Cloud9 to Publish AWS Lambda Functions

I like AWS Cloud9. It's a conveniently available IDE that doesn't take too long to start, doesn't require demanding resources on your machine, and can also connect and continue where you left off from different computers. It is a technical advancement over a locally installed IDE.

I have begun writing a large number of Lambda functions for small tools. That is, each function implements one tool. And one of the challenges I have faced while writing them is editing and deploying each Lambda function quickly. Clearly, the Lambda console is insufficient for quick editing of several dozen functions because you'd either have to open several tabs or you have to slow down productivity to wait for each page to load.

That is not an ideal solution at all.

Fortunately, AWS has integrated Lambda function management into the Cloud9 IDE so that it is possible to upload your code to Lambda without leading the IDE. But this still comes short in some aspects.

For one thing, what if you make a catastrophic error that is duplicated in all of your functions, and you have to fix and deploy it all of them at once? Using the Cloud9 interface, you still have to point and click on each function and click on the buttons for uploading each one to Lambda, clicking on their directories and ZIP bundles, and so on. So when you have more than a handful of functions, it's not much of an improvement over the Lambda console.

And speaking of ZIP bundles, they have to be created in a special way to be uploaded to Lambda. In fact, ZIP bundles and root folders are the only ways you can bundle module dependencies with your function since the Lambda console still doesn't give you an easy way to add external dependencies from the code editor.

So given that all this is hard work, and the work is expected to culminate to the point of being unmanageable, as more functions are created, there has to be an easier way to do this.

Automation To The Rescue

We can use simple bash scripting from within the Cloud9 environment to automatically zip all the function folders and upload them to their respective functions.

To accomplish this, we need to have the AWS CLI installed. On Cloud9 running Amazon Linux, it is installed by default, but it is version 1. I personally prefer version 2, and there are instructions to install it in their documentation. Just make sure, however, that if you do end up installing version 2, that you either move its folder forward in the PATH variable (mine was installed under /usr/local) or uninstall version 1 using yum remove awscli followed by making symlinks to /usr/bin/aws and /usr/bin/aws_complete if necessary. And restarting your shell, of course.

Before we can use the AWS CLI, we need to generate an access key for AWS that can be used for non-interactive scripting commands. This can be created in the IAM console. You will need both the access key ID and the secret key since you'll be filling those in when you run aws config.

The scripts

I have created one script for zipping all the folders into their own zip file and another for uploading each zip folder to the corresponding Lambda function. This particular script is simple and assumes that the Lambda functions are named so that the last part of the name is the directory name. Both scripts should be placed in the ~/environment folder.

The other function is much more straightforward than this and uses the AWS CLI to upload the lambda-func.zip files to the Lambda functions. Remember to configure the CLI with your API key before you use it.

But what about tests?

Of course, no function is not ready for usage until it is thoroughly tested. Lambda provides test events in the console to create and run against the function, but I haven't found a way to replicate this in the CLI yet. I did ask a StackOverflow question about that, which didn't get much activity other than a suggestion to run the Lambda functions from the script and CLI with my test events, so I might try that solution.

16