UNPKG

express-lambda

Version:

Make AWS lambda behave like an express app

83 lines (72 loc) 2.97 kB
"use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var aws = require("aws-sdk"); var async = require("async"); var awsResponse = require("./aws_response"); var log = require("./log"); var ApiGateway = function () { function ApiGateway(name) { _classCallCheck(this, ApiGateway); this.name = name; this.id = null; this.rootResourceId = null; this.accountNum = null; this._region = process.env.AWS_REGION; this._awsApiGw = new aws.APIGateway({ region: this._region }); this._iam = new aws.IAM({ region: this._region }); } _createClass(ApiGateway, [{ key: "build", value: function build(done) { async.series([this._createGateway.bind(this), this._discoverRootResource.bind(this), this._discoverAccountNumber.bind(this)], done); } }, { key: "deploy", value: function deploy(done) { log.info("Creating API Gateway deployment"); this._awsApiGw.createDeployment({ restApiId: this.id, stageName: process.env.AWS_ENVIRONMENT }, awsResponse.cb("createDeployment", done)); } }, { key: "url", value: function url() { return "https://" + this.id + ".execute-api." + this._region + ".amazonaws.com/" + process.env.AWS_ENVIRONMENT; } }, { key: "_createGateway", value: function _createGateway(done) { var _this = this; log.info("Creating API Gateway"); this._awsApiGw.createRestApi({ name: this.name }, awsResponse.cb("createRestApi", done, function (data) { _this.id = data.id; })); } }, { key: "_discoverRootResource", value: function _discoverRootResource(done) { var _this2 = this; log.info("Discovering root resource"); this._awsApiGw.getResources({ restApiId: this.id }, awsResponse.cb("getResources", done, function (data) { _this2.rootResourceId = data.items[0].id; })); } }, { key: "_discoverAccountNumber", value: function _discoverAccountNumber(done) { var _this3 = this; log.info("Discovering account number"); this._iam.getUser({}, awsResponse.cb("getUser", done, function (data) { _this3.accountNum = data.User.UserId; })); } }]); return ApiGateway; }(); module.exports = ApiGateway;