Amita Sharma
Amita Sharma

Reputation: 46

Module zip is not available on AWS lambda

Getting error zip: command not found on AWS lambda when trying to use zip command to zip a folder:

const exec = require('child_process').exec;
exec('touch /tmp/test.txt', (error, stdout, stderr) => {
      console.log(stdout);
  })
  exec('zip /tmp/test.zip /tmp/test.txt', (error, stdout, stderr) => {
      if (error) {
           console.error(`exec error: ${error}`);
           return;
         } else {
        console.log(stdout);
    }
  })
  exec('ls /tmp/', (error, stdout, stderr) => {
      console.log(stdout);
  })

Also when placing zip inside a bin folder, it is giving permission denied error. How to install zip module on AWS lambda?

Upvotes: 2

Views: 2145

Answers (1)

Todd Price
Todd Price

Reputation: 2760

The zip package is not installed on the Amazon Linux servers that your Lambda function is running on. So have two options:

1) Provide the zip application binary as a part of your function package that you upload to Lambda. The zip application will need to be compiled on Amazon Linux or statically linked.

2) Use a Node.js library that doesn't have any dependencies on binary executables.

I've personally used option #2 and can recommend the excellent "yazl" library available here:

https://github.com/thejoshwolfe/yazl

You'll end up doing something like this:

var yazl = require('yazl');

var zipfile = new yazl.ZipFile();
zipfile.addBuffer(fs.readFileSync('/tmp/file.txt'), "file.txt", {
  mtime: new Date(),
  mode: 0100664, // -rw-rw-r--
});
zipfile.outputStream.pipe(fs.createWriteStream('/tmp/test.zip')).on("close", function() {
  console.log("done zipping files");
});
zipfile.end();

Upvotes: 2

Related Questions