I have a small node modules that calls a fork
syscall to fork the running process. When I try to call the aws-sdk
function Kinesis.putRecord
from the child process, the call does not happen. Calling from the parent process works fine.
To illustrate, the following two code snippets only differ on whether the call to putRecord
is performed by the parent or child processes.
In the first code snippet, the call is performed by the parent process, and the call works fine.
const aws = require('aws-sdk');
const fork = require('fork');
module.exports.handler = (event, context, callback) => {
const isChild = fork.fork();
if (!isChild) { // Parent
const kinesis = new aws.Kinesis()
const record = {
Data: "Lorem ipsum",
PartitionKey: "123456",
StreamName: process.env.STREAM_NAME,
}
kinesis.putRecord(record, (err, data) => {
if (err) {
callback(null, {message: 'Encountered error in kinesis', err});
} else {
callback(null, {message: 'Successfully ran in kinesis', data});
fork.wait();
}
})
} else { // Child
process.exit();
}
};
On the other hand, in the second code snippet, the call is performed by the child process, and the call is not performed. Specifically in this example the Lambda execution times out.
const aws = require('aws-sdk');
const fork = require('fork');
module.exports.handler = (event, context, callback) => {
const isChild = fork.fork();
if (isChild) { // Child
const kinesis = new aws.Kinesis()
const record = {
Data: "Lorem ipsum",
PartitionKey: "123456",
StreamName: process.env.STREAM_NAME,
}
kinesis.putRecord(record, (err, data) => {
if (err) {
callback(null, {message: 'Encountered error in kinesis', err});
} else {
callback(null, {message: 'Successfully ran in kinesis', data});
}
})
} else { // Parent
fork.wait();
}
};
The fork module can be found here.
I'm using node.js version 6.10.3.