diff --git a/lib/commands/deploy_endpoint.js b/lib/commands/deploy_endpoint.js index 5705aca0f..c267f3b7e 100644 --- a/lib/commands/deploy_endpoint.js +++ b/lib/commands/deploy_endpoint.js @@ -174,7 +174,7 @@ CMD.prototype._promptRegions = Promise.method(function() { }); } - return JawsCli.select('Select a region inthis stage to deploy to: ', choices, false); + return JawsCli.select('Select a region in this stage to deploy to: ', choices, false); }); /** @@ -463,6 +463,7 @@ ApiDeployer.prototype._buildEndpoints = Promise.method(function() { .bind(_this) .then(_this._createEndpointMethod) .then(_this._createEndpointIntegration) + //.then(_this._updateLambdaPermission) .then(_this._createEndpointMethodResponses) .then(_this._createEndpointMethodIntegResponses) .then(function() { @@ -589,6 +590,12 @@ ApiDeployer.prototype._createEndpointMethod = Promise.method(function(endpoint) } } + //console.log( + // 'Creating method with parent ID: ' + // + endpoint.apiGateway.cloudFormation.Method + // + ' ' + // + endpoint.apiGateway.apig.resource.id); + return _this.ApiClient.showMethod( _this._restApiId, endpoint.apiGateway.apig.resource.id, @@ -654,6 +661,7 @@ ApiDeployer.prototype._createEndpointIntegration = Promise.method(function(endpo throw new JawsError('Could not find a lambda deployed in this stage/region with this function name: ' + cfLogicalResourceId); } + endpoint.apiGateway.apig.lambda = lambda; // Create integration body var integrationBody = { @@ -669,6 +677,12 @@ ApiDeployer.prototype._createEndpointIntegration = Promise.method(function(endpo + ':function:' + lambda.PhysicalResourceId + '/invocations', + + // Due to a bug in API Gateway reported here: https://github.com/awslabs/aws-apigateway-swagger-importer/issues/41 + // Specifying credentials within API Gateway causes extra latency (~500ms) + // Until API Gateway is fixed, we need to make a seperate call to Lambda to add credentials to API Gateway + // Once API Gateway is fixed, we can use this in credentials: + // _this._regionJson.iamRoleArnApiGateway credentials: _this._regionJson.iamRoleArnApiGateway, requestParameters: endpoint.apiGateway.cloudFormation.RequestParameters || {}, requestTemplates: endpoint.apiGateway.cloudFormation.RequestTemplates || {}, @@ -818,6 +832,52 @@ ApiDeployer.prototype._createEndpointMethodIntegResponses = Promise.method(funct }); }); +/** + * API Deployer: Update Lambda Permission + * - Since specifying credentials when creating the Method Integration results in ~500ms + * - of extra latency, this function updates the lambda's access policy instead + * - to grant API Gateway permission. This is how the API Gateway console does it. + * - But this is not finished and the "getPolicy" method in the SDK is broken, so this + * - is currently impossible to implement. + */ + +ApiDeployer.prototype._updateLambdaPermission = Promise.method(function(endpoint) { + + var _this = this; + var lambdas; + console.log(endpoint.apiGateway.apig.lambda.PhysicalResourceId); + + // TODO: Finish when AWS "getPolicy" bug is fixed + // Get policy for lambda + // Check to see if it already has a JAWS policy + // Replace the JAWS policy + // All done! + + return AWSUtils.lambdaListFunctions( + _this._JAWS._meta.profile, + _this._regionJson.region) + .then(function(data) { + lambdas = data.Functions; + console.log(data.Functions); + }) + .then(function() { + + console.log(endpoint.apiGateway.apig.lambda.PhysicalResourceId); + console.log(endpoint.apiGateway.apig.lambda); + return AWSUtils.lambdaGetPolicy( + _this._JAWS._meta.profile, + _this._regionJson.region, + endpoint.apiGateway.apig.lambda.PhysicalResourceId) + .then(function(data) { + + var policy = JSON.parse(data.Policy); + + console.log('lambda policy: ', policy.Statement[0]); + return endpoint; + }); + }); +}); + /** * API Deployer: Create Deployment */ diff --git a/lib/utils/aws.js b/lib/utils/aws.js index 82d993d1b..325a14111 100644 --- a/lib/utils/aws.js +++ b/lib/utils/aws.js @@ -778,3 +778,62 @@ exports.cwGetStreamEvents = function(logGroupName, logStreamName) { }); }); }; + +/** + * Lambda: List Functions + * @param awsProfile + * @param awsRegion + * @param bucketName + * @returns {*} + */ + +exports.lambdaListFunctions = function(awsProfile, awsRegion) { + this.configAWS(awsProfile, awsRegion); + + var lambda = new AWS.Lambda(); + + var params = {}; + + return new Promise(function(resolve, reject) { + lambda.listFunctions(params, function(err, data) { + + if (err) { + return reject(err); + } + + return resolve(data); + + }); + }); +}; + + +/** + * Lambda: Get Policy + * @param awsProfile + * @param awsRegion + * @param bucketName + * @returns {*} + */ + +exports.lambdaGetPolicy = function(awsProfile, awsRegion, functionName) { + this.configAWS(awsProfile, awsRegion); + + var lambda = new AWS.Lambda(); + + var params = { + FunctionName: functionName.trim() + }; + + return new Promise(function(resolve, reject) { + lambda.getPolicy(params, function(err, data) { + + if (err) { + return reject(err); + } + + return resolve(data); + + }); + }); +}; diff --git a/package.json b/package.json index 32aa1b7bc..71ca6deaf 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ }, "dependencies": { "async": "^0.9.0", - "aws-sdk": "^2.1.24", + "aws-sdk": "^2.2.4", "babelify": "^6.3.0", "bluebird": "^2.9.34", "browserify": "^11.0.1", diff --git a/tests/all.js b/tests/all.js index 50173780f..64cb5e208 100644 --- a/tests/all.js +++ b/tests/all.js @@ -15,18 +15,18 @@ describe('AllTests', function() { }); //require tests vs inline so we can run sequentially - //require('./cli/tag'); - //require('./cli/module_install'); - //require('./cli/env'); - //require('./cli/module_create'); - //require('./cli/run'); + require('./cli/tag'); + require('./cli/module_install'); + require('./cli/env'); + require('./cli/module_create'); + require('./cli/run'); /** * Tests below create AWS Resources */ //require('./cli/dash'); //require('./cli/deploy_lambda'); - require('./cli/deploy_endpoint'); + //require('./cli/deploy_endpoint'); //require('./cli/new_stage_region'); //require('./cli/new_project'); }); \ No newline at end of file