As others have pointed out, in Node.js you can use the child_process
module, which is built-in to Node.js. Here's a complete working sample:
app.js
:
'use strict'
const childproc = require('child_process')
module.exports.handler = (event, context) => {
return new Promise ((resolve, reject) => {
const commandStr = "./script.sh"
const options = {
maxBuffer: 10000000,
env: process.env
}
childproc.exec(commandStr, options, (err, stdout, stderr) => {
if (err) {
console.log("ERROR:", err)
return reject(err)
}
console.log("output:\n", stdout)
const response = {
statusCode: 200,
body: {
output: stdout
}
}
resolve(response)
})
})
}
script.sh
:
#!/bin/bash
echo $PWD
ls -l
response.body.output
is
/var/task
total 16
-rw-r--r-- 1 root root 751 Oct 26 1985 app.js
-rwxr-xr-x 1 root root 29 Oct 26 1985 script.sh
(NOTE: I ran this in an actual Lambda container, and it really does show the year as 1985).
Obviously, you can put whatever shell commands you want into script.sh
as long as it's included in the Lambda pre-built container. You can also build your own custom Lambda container if you need a command that's not in the pre-built container.