【问题标题】:AWS Lambda and RDS working example (need it to work with Sequelize)AWS Lambda 和 RDS 工作示例(需要它与 Sequelize 一起使用)
【发布时间】:2018-12-18 19:32:48
【问题描述】:

这是 AWS Lambda 和 MySQL 的一个工作示例,但我希望它可以与 Sequelize 一起使用。如何初始化 Sequelize 以使用 AWS Lambda?我也拥有经过身份验证的 IAM 角色。

https://dzone.com/articles/passwordless-database-authentication-for-aws-lambd

'use strict';
const mysql = require('mysql2');
const AWS = require('aws-sdk');
// TODO use the details of your database connection
const region = 'eu-west-1';
const dbPort = 3306;
const dbUsername = 'lambda'; // the name of the database user you created in step 2
const dbName = 'lambda_test'; // the name of the database your database user is granted access to
const dbEndpoint = 'lambdatest-cluster-1.cluster-c8o7oze6xoxs.eu-west-1.rds.amazonaws.com';
module.exports.handler = (event, context, cb) => {
  var signer = new AWS.RDS.Signer();
  signer.getAuthToken({ // uses the IAM role access keys to create an authentication token
    region: region,
    hostname: dbEndpoint,
    port: dbPort,
    username: dbUsername
  }, function(err, token) {
    if (err) {
      console.log(`could not get auth token: ${err}`);
      cb(err);
    } else {
      var connection = mysql.createConnection({
        host: dbEndpoint,
        port: dbPort,
        user: dbUsername,
        password: token,
        database: dbName,
        ssl: 'Amazon RDS',
        authSwitchHandler: function (data, cb) { // modifies the authentication handler
          if (data.pluginName === 'mysql_clear_password') { // authentication token is sent in clear text but connection uses SSL encryption
            cb(null, Buffer.from(token + '\0'));
          }
        }
      });
      connection.connect();
      // TODO replace with your SQL query
      connection.query('SELECT * FROM lambda_test.test', function (err, results, fields) {
        connection.end();
        if (err) {
          console.log(`could not execute query: ${err}`);
          cb(err);
        } else {
          cb(undefined, results);
        }
      });
    }
  });
};

【问题讨论】:

    标签: mysql node.js amazon-web-services aws-lambda sequelize.js


    【解决方案1】:

    我们正在使用 Sequelize 和 Lambda,但您需要预留更多资源,在我们的例子中,我们需要至少 1GB 才能使用 Sequelize 运行 lambda。没有它,只用 mysql2 就可以运行 128MB。

    但是,如果您真的想使用 Sequelize,只需将您的 createConnection 替换为您将在 sequelize doc 中找到的内容

    您可能会使用context.callbackWaitsForEmptyEventLoop=true,因为您在调用回调函数时可能会遇到一些问题而您什么也得不到,因为您的事件循环可能永远不会为空。

    【讨论】:

      【解决方案2】:

      而不是使用 mysql.createConnection() 并使用您的 RDS 签名者令牌:

      var sequelize = require('sequelize')
      const Sequelize = new sequelize(
              process.env.database_name,
              process.env.databse_user,
              token,
              {
                  dialect: 'mysql',
                  dialectOptions: {
                      ssl: 'Amazon RDS',
                      authPlugins: { // authSwitchHandler is deprecated
                          mysql_clear_password: () => () => {
                              return token
                          }
                      }
                  },
                  host: process.env.db_proxy_endpoint,
                  port: process.env.db_port,
                  pool: {
                      min: 0, //default
                      max: 5, // default
                      idle: 3600000
                  },
                  define: {
                      charset: 'utf8mb4'
                  }
              }
      // then return your models (defined in separate files usually) 
      await Sequelize.authenticate() // this just does a SELECT 1+1 as result;
      await Sequelize.sync() // DO NOT use this in production, this tries to create tables defined by your models. Consider using sequelize migrations instead of using sync()
      

      另外,最好将数据库连接参数保存在配置文件中,这样任何人都无法看到它们。 (process.env)

      【讨论】:

        猜你喜欢
        • 2016-02-09
        • 2011-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-29
        • 1970-01-01
        • 1970-01-01
        • 2015-02-27
        相关资源
        最近更新 更多