阅读背景:

如何使用AWS Lambda函数(或加速初始连接)来保持sequelize连接?

来源:互联网 

I currently have an AWS Lambda function utilizing sequelize to connect/query from a MySQL database in RDS.

我目前有一个AWS Lambda函数,使用sequelize从RDS中的MySQL数据库连接/查询。

Everything works as intended, except that the first connection after a period of inactivity always takes at least 5 seconds to start up. Subsequent lambda calls are quick, so I suspect the connection is persisted and pooled.

一切都按预期运行,除了在一段时间不活动后的第一次连接总是至少需要5秒才能启动。随后的lambda调用速度很快,因此我怀疑连接是持久存在的,并且是池化的。

It's a bit annoying, and so I'm wondering if I can optimize it to either connect faster, or configure lambda so that the connection is permanent.

这有点烦人,所以我想知道我是否可以对它进行优化,以便更快地连接,或者配置lambda以便连接是永久性的。

"use strict"
var Sequelize = require('sequelize')

var database = 'participantData',
    host = 'hostname',
    username = 'username',
    password = 'blah'

var sequelize = new Sequelize(database, username, password, {
  host: host,
  dialect: 'mysql',
  logging: console.log('Query Logged')
})

var Resource = sequelize.define('resource', {
  program_name: { type: Sequelize.STRING },
  geocoded_address: { type: Sequelize.STRING }
})

exports.handler = function(event, context){
  Resource.findAll({
    //some filter
  })
    .then((res) => {
      context.succeed(JSON.stringify(res))
    })
    .catch(error => console.log(error))
}

1 个解决方案

#1


4  

No you cannot make connections in Lambda permanent because of the basic way it works. But you can optimize Lambda startup times, re-use, etc. See https://www.reddit.com/r/aws/comments/49l91l/lambda_functions_in_vpc_cold_boot_times_of_10/ and https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/

不,你不能让Lambda永久的连接,因为它的基本工作方式。但是您可以优化Lambda启动时间、重用等。参见https://www.reddit.com/r/aws/comments/49l91l/lambda_functions_in_vpc_cold_boot_times_of_10/和https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/

My guess is you just need to send a request every once in awhile so it continues to reuse the container.

我的猜测是您需要每隔一段时间发送一次请求,以便它继续重用容器。


分享到: