diff --git a/bin/jaws b/bin/jaws index ebbe7ab1a..0fe4f45e3 100755 --- a/bin/jaws +++ b/bin/jaws @@ -5,6 +5,18 @@ var JAWS = require('../lib/index.js'); var program = require('commander'); +function handleExit(promise) { + promise + .catch(JawsError, function(e) { + console.error(e); + process.exit(e.code); + }) + .error(function(e) { + console.error(e); + process.exit(1); + }); +} + program .version(JAWS._meta.version); @@ -12,27 +24,27 @@ program .command('new') .description('Create a new JAWS project in the current working directory') .action(function() { - JAWS.new(); + handleExit(JAWS.new()); }); program .command('install ') .description('Installs an jaws-module') .action(function(url) { - JAWS.install(url); + handleExit(JAWS.install(url)); }); program .command('tag') .description('Tag a lambda function to be deployed') .action(function() { - JAWS.tag(); + handleExit(JAWS.tag()); }); program .command('*') .action(function() { - JAWS.custom(arguments); + handleExit(JAWS.custom(arguments)); }); program.parse(process.argv); diff --git a/lib/commands/new.js b/lib/commands/new.js index 83cdc8e6b..0f07303fd 100644 --- a/lib/commands/new.js +++ b/lib/commands/new.js @@ -8,7 +8,7 @@ // Defaults var Promise = require('bluebird'), - fs = Promise.promisifyAll(require('fs')), + fs = require('fs'), os = require('os'), async = require('async'), AWS = require('aws-sdk'), @@ -17,53 +17,9 @@ var Promise = require('bluebird'), jsonfile = Promise.promisifyAll(require('jsonfile')), shortid = require('shortid'); -// AWS IAM Role True Policy -var iamRoleTrustPolicy = JSON.stringify({ - Version: '2012-10-17', - Statement: [ - { - Sid: '', - Effect: 'Allow', - Principal: { - Service: 'lambda.amazonaws.com', - }, - Action: 'sts:AssumeRole', - }, - ], -}); - -// AWS IAM Role Access Policy -var iamRoleAccessPolicy = JSON.stringify({ - Version: '2012-10-17', - Statement: [ - { - Effect: 'Allow', - Action: [ - 'cloudwatch:*', - 'cognito-identity:ListIdentityPools', - 'cognito-sync:GetCognitoEvents', - 'cognito-sync:SetCognitoEvents', - 'dynamodb:*', - 'iam:ListAttachedRolePolicies', - 'iam:ListRolePolicies', - 'iam:ListRoles', - 'iam:PassRole', - 'kinesis:DescribeStream', - 'kinesis:ListStreams', - 'kinesis:PutRecord', - 'lambda:*', - 'logs:*', - 's3:*', - 'sns:ListSubscriptions', - 'sns:ListSubscriptionsByTopic', - 'sns:ListTopics', - 'sns:Subscribe', - 'sns:Unsubscribe', - ], - Resource: '*', - }, - ], -}); +Promise.promisifyAll([ + fs, +]); /** * Internal Functions @@ -96,7 +52,7 @@ function _createSwaggerTemplate(projectTitle) { info: { version: '1.0.0', title: projectTitle, - description: 'The Swagger template for this JAWS project to use with API Gateway' + description: 'The Swagger template for this JAWS project to use with API Gateway', }, host: '', schemes: [ @@ -134,7 +90,7 @@ module.exports = function(JAWS) { JAWS.new = function() { // Epic greeting - console.log(_generateAscii()); + chalk.yellow(_generateAscii()); var iam = new AWS.IAM(); var project = {}; @@ -171,7 +127,8 @@ module.exports = function(JAWS) { { type: 'input', name: 'stage', - message: 'JAWS: Which stage would you like to create? (you can import more later). Ex: prod', + message: 'JAWS: Which stage would you like to create? (you can import more later)', + default: 'test', }, ]; @@ -230,7 +187,8 @@ module.exports = function(JAWS) { }).then(function() { // Create admin.env - var adminEnv = 'ADMIN_AWS_ACCESS_KEY_ID=' + project.awsAdminKeyId + os.EOL + 'ADMIN_AWS_SECRET_ACCESS_KEY=' + project.awsAdminSecretKey; + var adminEnv = 'ADMIN_AWS_ACCESS_KEY_ID=' + project.awsAdminKeyId + os.EOL + + 'ADMIN_AWS_SECRET_ACCESS_KEY=' + project.awsAdminSecretKey; return fs.writeFile(JAWS._meta.projectRootPath + '/admin.env', adminEnv); }) @@ -310,7 +268,10 @@ module.exports = function(JAWS) { jsonfile.writeFileSync(JAWS._meta.projectRootPath + '/jaws-cf.json', _createCFTemplate()); // Create Swagger file - jsonfile.writeFileSync(JAWS._meta.projectRootPath + '/jaws-swagger.json', _createSwaggerTemplate(project.name)); + jsonfile.writeFileSync( + JAWS._meta.projectRootPath + '/jaws-swagger.json', + _createSwaggerTemplate(project.name) + ); // End console.log('JAWS: Your project "' + diff --git a/lib/templates/jaws-cf.json b/lib/templates/jaws-cf.json new file mode 100644 index 000000000..2f1bcd206 --- /dev/null +++ b/lib/templates/jaws-cf.json @@ -0,0 +1,201 @@ + +{ + "AWSTemplateFormatVersion": "2010-09-09", + "Description": "JAWS", + "Parameters": { + "aaaStage": { + "Type": "String", + "Default": "test" + }, + "aaaDataModelPrefix": { + "Type": "String", + "Default": "test", + "AllowedValues": [ + "test", + "prod" + ] + }, + "aaaProjectName": { + "Type": "String", + "Default": "jaws", + "AllowedValues": [ + "jaws" + ] + }, + "HostedZoneName": { + "Type": "String", + "Default": "myapp.com" + } + }, + "Resources": { + "LambdaRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + }, + "Action": [ + "sts:AssumeRole" + ] + } + ] + }, + "Path": "/" + } + }, + "Profile": { + "Type": "AWS::IAM::InstanceProfile", + "Properties": { + "Path": "/", + "Roles": [ + { + "Ref": "LambdaRole" + } + ] + } + }, + "Group": { + "Type": "AWS::IAM::Group", + "Properties": { + "Path": "/dataModel/" + } + }, + "DataModelPolicy": { + "Type": "AWS::IAM::Policy", + "Properties": { + "PolicyName": { + "Fn::Join": [ + "-", + [ + { + "Ref": "aaaStage" + }, + { + "Ref": "aaaProjectName" + } + ] + ] + }, + "PolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "logs:CreateLogGroup", + "logs:CreateLogStream", + "logs:PutLogEvents" + ], + "Resource": "arn:aws:logs:*:*:*" + }, + { + "Action": [ + "s3:Get*", + "s3:List*", + "s3:Put*" + ], + "Resource": [ + { + "Fn::Join": [ + "", + [ + "arn:aws:s3:::", + { + "Ref": "aaaProjectName" + }, + "-images.", + { + "Ref": "HostedZoneName" + }, + "/", + { + "Ref": "aaaDataModelPrefix" + }, + "*" + ] + ] + } + ], + "Effect": "Allow" + }, + { + "Effect": "Allow", + "Action": [ + "dynamodb:BatchGetItem", + "dynamodb:BatchWriteItem", + "dynamodb:DeleteItem", + "dynamodb:DescribeTable", + "dynamodb:Get*", + "dynamodb:List*", + "dynamodb:PutItem", + "dynamodb:Query", + "dynamodb:Scan", + "dynamodb:UpdateItem", + "dynamodb:UpdateTable" + ], + "Resource": [ + { + "Fn::Join": [ + "", + [ + "arn:aws:dynamodb:us-east-1:", + { + "Ref": "AWS::AccountId" + }, + ":table/", + { + "Ref": "aaaDataModelPrefix" + }, + "-", + { + "Ref": "aaaProjectName" + }, + "-users*" + ] + ] + }, + { + "Fn::Join": [ + "", + [ + "arn:aws:dynamodb:us-east-1:", + { + "Ref": "AWS::AccountId" + }, + ":table/", + { + "Ref": "aaaDataModelPrefix" + }, + "-", + { + "Ref": "aaaProjectName" + }, + "-images*" + ] + ] + } + ] + } + ] + }, + "Roles": [ + { + "Ref": "LambdaRole" + } + ], + "Groups": [ + { + "Ref": "Group" + } + ] + } + } + } +} \ No newline at end of file