I want to execute a command on an Amazon EC2 server which takes more than 30 minutes.
I used node-ssh to execute the command on the EC2 instance, but this module waits until the execution is finished. Lambda functions run for a maximum of 15 minutes before it times-out.
My problem here is the Lambda function gets timeout before execution is finished.
In dataProcess function I have few steps:
- Establish connection - This is working
- Initiate copy: Copy file from S3 to EC2 instance
- triggerUtility: Execute files on the EC2 instance
Step 3 takes almost 30-40min, but Lambda has max 15 minutes execution time, so this results in a timeout failure.
Need solution where I can just fire and forget the command.
exports.handler = (event, context, callback) => {
if (event.Records && event.Records[0]) {
const awsBucket = event.Records[0].s3.bucket.name;
const fileKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
dataProcess();
}
}
async function dataProcess() {
await sftpConnect();
await initiateCopy();
await triggerUtility();
await terminateConnection();
}
function sftpConnect() {
console.log('sftpConnect 1');
return new Promise((resolve, reject) => {
var password = process.env.PASSWORD || CONFIG.PASSWORD;
ssh.connect({
host: process.env.HOST ,
username: process.env.USER ,
password: password,
port: process.env.PORT,
readyTimeout: 40000,
tryKeyboard: true,
onKeyboardInteractive: (name, instructions, instructionsLang, prompts, finish) => {
if (prompts.length > 0 && prompts[0].prompt.toLowerCase().includes('password')) {
finish([password]);
}
}
}).then(function() {
console.log("Connected to Source");
resolve('success');
}, function(error) {
console.log("ERROR connecting to source:" + process.env.HOST || CONFIG.HOST + " - ", error);
reject(new Error(error));
});
});
}
function initiateCopy() {
return new Promise((resolve, reject) => {
var cmd = process.env.AWS_CP_CMD || CONFIG.AWS_CP_CMD;
cmd = cmd.replace('TODAY', TODAY).replace('TODAY', TODAY);
console.log('AWS Cmd ', cmd);
ssh.execCommand(cmd).then(function(output) {
resolve('resolved 1');
}, function(error) {
reject(new Error(error));
});
});
}
function triggerUtility() {
return new Promise((resolve, reject) => {
var cmd = process.env.JAVA_UTILITY_CMD || CONFIG.JAVA_UTILITY_CMD;
ssh.execCommand(cmd).then(function(output) {
resolve('resolved 1');
}, function(error) {
reject(new Error(error));
});
});
}