Deploy Amazon Web Services Lambda function(s) with a single command.
Deploying Lambda functions manually involves quite a few steps. Manually clicking buttons to upload zip files is fine the first few times but gets old pretty quickly. When you feel the pain, we have the cure.
Simplify the process of deploying a AWS Lambda Function without having to adopt a build tool/system.
There are 5 Steps to setup deployment for your Lambda Function:
npm install dpl --save-devYou need to have AWS_REGION and AWS_IAM_ROLE set:
Example:
export AWS_REGION=eu-west-1
export AWS_IAM_ROLE=arn:aws:iam::123456789:role/LambdaExecRolethese need to be your real values
Note: You also need to have your AWS Credentials set to use the
aws-sdkif you have not yet done this, see below for instructions.
In your package.json file, add the list of files & directories
you want to be included in your distribution.
Example:
"files_to_deploy": [ "package.json", "index.js", "lib/" ]Example:
"scripts": {
"deploy": "node ./node_modules/dpl/dpl.js"
}npm run deployIf you see an error message in your console, read the message and resolve it by correcting your setup. you have either not set your AWS Credentials or not defined the required environment variables. If you get stuck or have questions, ping us!
Deploying your Lambda function requires a few Environment Variables to be set.
As with all node.js code which uses the aws-sdk,
it expects to have your AWS credentials stored on locally.
Your credentials are expected to be at: ~/.aws/credentials
(e.g: if your username is alex, your AWS credentials will
be stored at /Users/alex/.aws/credentials)
If you have not yet set your AWS credentials on your machine
do this now.
The script needs to know which AWS_IAM_ROLE you want to use to deploy/run
the function.
Example:
export AWS_IAM_ROLE=arn:aws:iam::123456789:role/LambdaExecRoleExample:
export AWS_REGION=eu-west-1To make the deployment script's run faster,
and avoid forcing people to add entries into their .gitignore file,
we store the /dist directory and resulting .zip file
in your OS's Temporary storage.
For the deploy scrip to do its job,
we need to add two lines to our package.json
In your package.json file, add the list of files & directories
you want to be included in your distribution.
Example:
"files_to_deploy": [ "package.json", "index.js", "lib/" ]This tells dpl to copy these files and directory (with all contents)
to the /dist which will be zipped and sent to AWS.
Check our package.json if in doubt.
Instead of uploading all the files in a project to S3/Lambda we upload only
the required files. e.g: /src or /lib and ./index.js.
While we are preparing this package, these required files are copied to
the (temporary) /dist directory which will be zipped in Step 5.
Why a temporary directory? see: http://stackoverflow.com/questions/17946360/what-are-the-benefits-of-using-the-official-temp-directory-for-the-os
This typically includes the following:
lib/- the directory containing custom code your lambda function uses.package.json- the "manifest" file for your project, includes the Lambda function name, any configuration and dependencies.index.js- the "main"handlerof your Lambda function.
We only need the "production" dependencies to be zipped and shipped.
so instead of copying all the devDependencies in node_modules,
we simply install a fresh set using the --production flag.
Once the /dist directory has been created with the necessary files
and the dependencies have been installed in /dist/node_modules
we zip the "distribution" ready for uploading to AWS.
This can take a few seconds depending on how many dependencies your Lambda function has.
#### 5. Upload
Once the zip has been packaged we upload it to AWS using the aws-sdk.
Your Lambda function will be named according to the "name" in
the package.json file for your project.
Originally we were using Gulp to perform the tasks to deploy our
Lambda Functions. however this required us to duplicate a very similar
gulpfile.js in all our projects.
Disadvantages of using Gulp:
-
New developers on your team who have never used Gulp have one-more-thing to learn before they can be productive.
-
Gulp is (up to) 50% Slower than using node.js core modules/methods.
-
Each repo has to duplicate several Gulp devDependencies which have varing degrees of quality in their documentation/testing and will need to be updated soon when Gulp v.4 is released.
-
The devDependencies take up 28 Megabytes on disk For one lambda function that's insignificant, but if, like us, you have many Lambda functions (e.g: 40) you using Gulp will take up a Gigabyte of your hard drive.
Note: we still love Gulp and use it in our non-lambda projects, we just think this is a leaner way of deploying our Lambdas.
-
On Dependency - Our solution to the deployment task uses only one dependency: the
aws-sdk. -
Small Code - The entire
dpl("Deploy Lambda") module is fewer lines than our original
gulpfile.jsand uses only node.js core modules (you know and love) and your OS-nativezipcommand. -
A beginner can read and understand all the code used in
dplin a few minutes; our code has both JavaDoc and in-line comments and we are here to help if you have any questions! -
No assumptions about your code style. e.g if you need any custom processing (e.g
babelfor your ES6) simply add a task in yourscriptssection of yourpackage.jsonand run that task before deploying. -
No Global packages required or implied, just one
devDependency.
Given that AWS Lambda only supports Node.js v0.10.36 (at present) the code you deploy to Lambda needs to be ES5 Only. Since most of the cool kids are using ES6/2015 (aka modern javascript ...) the build script includes a transform step to translate ES6 into ES5 so your ES6 Code will run on Lambda.
- https://www.npmjs.com/package/deploy-aws-lambda > https://github.com/aesinv/aws-lambda-toolkit looks un-maintained/abandoned with lots of "Todo" items and no tests..
We briefly considered using node-fs-extra
to do the heavy lifting, but they are about to remove support for node.js v.0.10
which, if we want to be able to run the deploy script from a CI Environment
running node v.0.10.36 (the Lambda version of node!)
we need to DIY the file operations.
- Using NPM as a build tool: http://blog.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool/ (you don't need gulp/grunt/etc...)

