Deploying to AWS Lambda doesn't have to be painful and involve many manual steps. It can be quick, automated and a matter of a few seconds.

In my first days of using AWS Lambda functions, my deployment process was the following: Change the code inside Visual Studio Code, open the file explorer, zip the necessary files, switch to the AWS console, upload the zipped code and run the tests. But it can be done way quicker than that.

This how-to isn't the only way to go, but it's a quick way for deploying applications. There are however more sophisticated solutions, if you see the need using those.

Final result

In this how-to we are going to deploy a basic Hello World (written in Node.js) application to AWS Lambda by using the AWS CLI.

The complete source code for this how-to can be downloaded from GitHub.

Requirements

  • Linux Bash (or WSL (Windows Subsystem for Linux) on Windows)
  • AWS Account (can be created for free here)
  • AWS CLI (you can find the instructions here)
  • Visual Studio Code or any other code editor (download here)
  • Node.js installed on your PC (can be downloaded here
  • AWS Lambda function (detailed tutorial can be found here

Preparation

To be able to follow this how-to, make sure you have installed the AWS CLI correctly and that you have configured your account correctly by invoking aws configure (More information can be found in the official documentation). It's enough to configure the AWS Access Key ID and AWS Secret Access Key for now.

Code

To start, create an empty directory, run npm init and fill in the values if you want or simply confirm the defaults to initialize the package.json.

Next, create an index.js file and place the following code inside it:

'use strict';

exports.handler = (event, context, callback) => {
  callback(null, 'Hello, world!')
};

The above function exported by exports.handler will be called, when our AWS Lambda is being invoced. Inside the handler, the callback function is being called, which tells Lambda, that the execution completed without an error (That's why the first argument passed is null) and what the output of the function is ('Hello World' in our case).

As we want to automate most of the tasks, create a file called deploy.sh at the same level as your package.json and place the following code inside of it (make sure to change lambda-deployment-cli-tutorial with your created function's name and use the AWS region you want; if you need additional files/folders in your deployment package, you can just add them after the index.js inside the script - it also supports zipping of sub-folders, so it's enough to specify the parent folder, e.g. node_modules):

zip -rq function.zip index.js

aws lambda update-function-code \
  --function-name lambda-deployment-cli-tutorial \
  --zip-file fileb://function.zip \
  --region eu-west-1

rm function.zip

If you're using WSL (Windows Subsystem for Linux), I recommend you saving it as a one-liner (zip -rq function.zip index.js && aws lambda update-function-code --function-name lambda-deployment-cli-tutorial --zip-file fileb://function.zip --region eu-west-1 && rm function.zip), as Windows saves the files with CLRF newlines and the bash can't interpret them correctly. Alternatively you can use dos2unix to convert the newline characters, if you want to keep the more structured multi-line version.

The script does the following:

  • line 1: creates our .zip file with all the specified files
  • line 3-6: invokes the AWS CLI command lambda update-function-code with the following parameters: --function-name (the name of your lambda function), --zip-file (the location/name of the zip file created on line 1), --region (the AWS region in which you want your function to be deployed). This command can be interpreted as: Update my lambda function named 'lambda-deployment-cli-tutorial', which is located in the AWS region eu-west-1 with the code inside function.zip
  • line 8: removes the zip again (cleanup)

Deployment script

Inside the scripts section of your package.json, add a new script called deploy (or whatever you want to name it). This will call our previously written deploy.sh script.

Of course you can call the deploy.sh script directly from your command line without adding it as an NPM script, but the reason we are adding it to our NPM scripts inside the package.json is, because this way we are able to add possible integration/unit tests in a later moment before deploying it to our lambda function.

"scripts": {
  "deploy": "./deploy.sh"
}

Run it!

Make sure, that your deploy.sh script is executable (you can make it executable by running chmod +x deploy.sh on Linux/WSL).

Now, if everything has been done correctly, you should be able to deploy your function to AWS Lambda by invoking npm run deploy.

Further reference

  • AWS CLI (docs)
  • AWS CLI lambda (docs)
  • AWS CLI lambda update-function-code (docs)

If you have any further questions, aspects you are unsure with or found anything, that is incorrect in this article, don't hesitate to leave a comment below or reach out to me on Twitter: @ivansieder