npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aws-lambda-upload

v0.2.4

Published

Package and upload an AWS lambda with its minimal dependencies

Downloads

4,165

Readme

aws-lambda-upload

Build Status npm version

Package node.js code for AWS lambda with its minimal dependencies.

This module allows you to have node.js files for AWS Lambda fuction alongside other code, and makes it easy to package a lambda function with only those dependencies that it needs. You can them update a lambda directly, or prepare the packaged code in local or S3 zip archive, including for use with CloudFormation.

Installation

npm install --save-dev aws-lambda-upload

Usage

$(npm bin)/aws-lambda-upload [options] <start-file>

Here, <start-file> is the path of the JS file to serve as the entry point into the Lambda. Note that in all cases, you'll use the basename of <start-file> as the filename to use for Lambda handler.

Update existing lambda

Use --lambda <name> flag to update a Lambda with the given name that you have previously created on AWS (e.g. using AWS Lambda console).

Available programmatically as updateLambda(startPath, lambdaName, options).

Saving a local zip file

Use --zip <path> flag to save the packaged lambda code to a zip file. It may then be used with e.g. aws lambda update-function-code command or as in a CloudFormation template with aws cloudformation package command.

Available programmatically as packageZipLocal(startPath, outputZipPath, options).

Saving a zip file to S3

Use --s3 to save the packaged lambda code to S3, and print the S3 URI to stdout.

The zip file will be saved to the bucket named by --s3-bucket flag (defaulting to "aws-lambda-upload"), and within that to folder (prefix) named by --s3-prefix flag (defaulting to empty). The basename of the file will be its MD5 checksum (which is exactly what aws cloudformation package does), which avoids duplication when uploading identical files.

Available programmatically as packageZipS3(startPath, options).

Package for CloudFormation template

Use --cfn <path> flag to interpret <start-path> as the path to a CloudFormation template (.json or .yml file), package any mentioned code to S3, replace with S3 locations, and output the adjusted template as JSON to <path> (- for stdout).

This is similar to aws cloudformation package command. It will process the following keys in the template:

  • For Resource with Type: AWS::Lambda::Function, processes Code property.
  • For Resource with Type: AWS::Serverless::Function, processes CodeUri property.

In both cases, if the relevant property is a file path, interprets it as a start JS file, packages it with packageZipS3() and replaces the property with S3 information in the format required by CloudFormation. If file path is relative, it's interpreted relative to the directory of the template.

Available programmatically as cloudformationPackage(templatePath, outputPath, options)

Collecting dependencies

If your entry file requires other files in your project, or in node_modules/, that's great. All dependencies will be collected and packaged into a temporary zip file.

Note that it does NOT package your entire directory or all of node_modules/. It uses collect-js-deps (which uses browserify) to examine the require() calls in your files, and recursively collects all dependencies. For files in node_modules/, it also includes any package.json files as they affect the import logic.

Actually, all browserify options are supported, by including them after -- on the command line (<start-path> should come before that).

Since the main file of a Lambda must be at top-level, if <start-path> is in a subdirectory (e.g. lib/my_lambda.js), a same-named top-level helper file (e.g. my_lambda.js) will be added to the zip archive for you. It's a one-liner that re-exports the entry module to let you use it as the Lambda's main file.

Supports TypeScript!

With --tsconfig <path>, you may specify a path to tsconfig.json or to the directory containing it, and typescript dependencies will be compiled to JS and included. You'll have to have tsify installed.

It is a convenience shortcut for including the tsify browserify plugin, and is equivalent to including this browserify option -- -p [ tsify -p <path> ] to collect-js-deps.

AWS permissions

To be able to update Lambda code or upload anything to S3, you need sufficient permissions. Read about configuring AWS credentials for how to set credentials that AWS SDK can use.

To use --lambda flag, the credentials you use need to at least give you the permission of lambda:UpdateFunctionCode for the resource of arn:aws:lambda:<region>:<account-id>:function:<function-name>. Read more here.

To use --s3 or --cfn flags, the credentials need to give you the permission to list and create objects in the relevant S3 bucket. E.g. the following policy works for the default bucket used by aws-lambda-upload:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:CreateBucket",
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::aws-lambda-upload"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
            ],
            "Resource": [
                "arn:aws:s3:::aws-lambda-upload/*"
            ]
        }
     ]
 }

Running tests

Before you run tests for the first time, you need to set up localstack. You can do it with

npm run setup-localstack

Note that localstack has a number of requirements.

Once set up, you can run tests with npm test as usual.