AWS Lambda Container Destruction Event

When to release connections and cleanup resources in lambda. In a typical Node JS application, we use hook

process.on('exit', (code) => {
    console.log(`About to exit with code: ${code}`);
});

However, this does not work on AWS Lambda. The result of connecting Mysql in sleep mode. We do not have enough resources for such active connections. None of the AWS documentation has indicated a way to achieve this.

How to get AWMS Lambda container stop event?

+4
source share
1 answer

EDIT: The short answer is that there is no such event to know when the container is stopped.

: - AWS , , , . , .

:

AWMS Lambda, - . , Node.js Node.js ( , , ):

exports.handler = (event, context, callback) => {
    // TODO implement
    callback(null, 'Hello from Lambda');
};

- . , . - :

const mysql = require('mysql');

exports.handler = (event, context, callback) => {
  let connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'me',
    password : 'secret',
    database : 'my_db'
  });

  connection.connect();

  connection.query('SELECT 1 + 1 AS solution', (error, results, fields) => {
    if (error) {
      connection.end();
      callback(error);
    }
    else {
      let retval = results[0].solution;
      connection.end();
      console.log('The solution is: ', retval);
      callback(null, retval);
    }
  });
};

. . .

, :

const mysql = require('mysql');

let connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret',
  database : 'my_db'
});

connection.connect();

exports.handler = (event, context, callback) => {
  // NOTE: should check if the connection is open first here
  connection.query('SELECT 1 + 1 AS solution', (error, results, fields) => {
    if (error) {
      callback(error);
    }
    else {
      let retval = results[0].solution;
      console.log('The solution is: ', retval);
      callback(null, retval);
    }
  });
};

: AWS Lambda , . , , , .

, , . , . . , , . , , . , , - .

, , , . , !

+5

Source: https://habr.com/ru/post/1681950/