DynamoDB is not immune from that problem. There is no way out: if the container is terminated the connection goes down. The key is not making Lambda stop and rm the container (in docker terms). If there are enough requests the container is reused and the connection stays up, but you must initialize it outside the function. An example with DynamoDB
const AWS = require("aws-sdk");
const docClient = new AWS.DynamoDB.DocumentClient();
module.exports.theFunction = (event, context, callback) => {
var params = {
TableName: "table",
Item: {
"attribute": "value"
}
};
docClient.put(params, function (err, data) {
...
});
callback(null, {...});
};
I didn't try but it should work with any other db.
However, is Lambda cost effective for services with the amount of requests required to have their containers almost never terminated?