多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.4 KiB

/**
* I was able to get xmysql to work on AWS lambda. This is just some docs on the process to help others.
* AWS setup notes:
* * The deploy uses serverless: https://serverless.com/framework/docs/getting-started/
* * The lambda function is accessed through API Gateway and access the RDS (mysql) database
* ** Make sure you deploy your API Gateway setup otherwise it won't be accessible.
* * The security setup:
* ** You need to put your lambda in a VPC
* ** The lambda role needs access to the VPC
* ** The RDS server has to be inside the VPC as well
6 years ago
*
* Performance:
* * Requests to the API Gateway resolve in ~ 90ms
*
* Extra packages needed:
* * serverless-http
*
* More info on deploying this to lambda:
* * https://serverless.com/blog/serverless-express-rest-api/
*/
"use strict";
6 years ago
Object.defineProperty(exports, "__esModule", { value: true });
var bodyParser = require("body-parser");
var express = require("express");
6 years ago
var serverless = require("serverless-http");
var cors = require("cors");
6 years ago
var mysql = require("mysql");
var Xapi = require("./node_modules/xmysql/lib/xapi.js");
var morgan = require("morgan");
var app = express();
6 years ago
var onXapiInitialized = new Promise(function(resolve, reject) {
try {
// /**************** START : setup express ****************/
6 years ago
app.use(morgan("tiny"));
app.use(cors());
app.use(bodyParser.json());
6 years ago
app.use(
bodyParser.urlencoded({
extended: true
})
);
// /**************** END : setup express ****************/
6 years ago
app.use(function(req, res, next) {
// You can add authentication here
6 years ago
console.log("Received request for: " + req.url, req);
next();
});
var mysqlConfig = {
host: config.mysql.host,
port: 3306,
database: config.mysql.database,
user: config.mysql.user,
password: config.mysql.password,
6 years ago
apiPrefix: "/",
ipAddress: "localhost",
portNumber: 3000,
ignoreTables: [],
storageFolder: __dirname
};
var mysqlPool = mysql.createPool(mysqlConfig);
var xapi = new Xapi(mysqlConfig, mysqlPool, app);
6 years ago
xapi.init(function(err, results) {
app.listen(3000);
resolve();
});
6 years ago
} catch (err) {
reject(err);
}
});
function handler(event, context, callback) {
6 years ago
onXapiInitialized.then(function() {
serverless(app)(event, context, callback);
});
}
exports.handler = handler;