Chris Mendez in For Developers, AWS

Packaging files for Lambda using Zip or Tar

AWS Lambda allows you to create functions on your local computer, include 3rd party libraries then upload all of that code using the AWS command line. When you use the AWS command-line, you will need package the files as a .Zip.

Below are the two most common ways of archiving files using the command line. Zip is common for Windows users but Linux tends to use Tarball.

For AWS Lambda, you will generally use Zip but I'm including both for completeness.


Zip

Archive

This example shows you how to recursively compress files into a zip folder and exclude a few files such as package.json and npm.log as well as my ReadMe.md file.

zip -r my_zip_package * -x .* -x "package.json" -x "*.log" -x *.md  
  • zip -r my_zip_package * means to create my_zip_package.zip and recursively include everything in in this folder.
  • -x "package.json" means to exclude any files with this name.
  • .* means to exclude any hidden files such as .DS_Store.
  • -x *.log means to exclude anything with a .log extension.
  • -x *.md means exclude anything with a .md extension such as ReadMe.md.

Unarchive

Example 1

Unzip a .zip file.

unzip my_zip_package.zip -d /path/to/directory  

Tar

Tar is short for Tape Archive and is unique in that you can pick a compression. For example, you can pick between gzip, bzip2 or xz.

You can always learn more by reading the manual.

man tar  

Archive

Example 1

Create a tarball using gzip compression.

tar -cpvzf my_tar_archive.tar.gz /path/to/directory  
  • -c Create an archive.
  • -v means "verbose mode" and will list every file as its being processed.
  • -z Compress the archive using gzip.
  • -p means to preserve permissions (extract all protection information)
  • -f Allows you to specify the filename of the archive.

Back up a directory and exclude any hidden files such as .DS_Store.

Example 2

This example excludes files with a specific extension or characteristic.

tar -cpvjf my_tar_archive.tar.gz /path/to/directory --exclude=*.psd --exclude=".*"  
  • -j Use bzip2 compression.
  • --exclude=*.psd Exclude any Photoshop files.
  • --exclude=".*" Exclude any hidden files.

Unarchive

Example 1

Extract an archive.

tar -xzvf my_tar_archive.tar.gz -C /path/to/directory  
  • -x Extract files.
  • -v means "verbose mode" and will list every file as its being processed.
  • -f Allows you to specify the filename of the archive.
  • -C Extract the files to a specific directory.

Next Steps

Now that we've gone over the basic commands, our next step should will be to create scripts that can help us automate the process of packaging and publishing our code to Lambda.

My article on Managing AWS Commands using JakeJS will show you how I package all of my Lambda functions for deployment into a single script.