diff --git a/lib/Stage.js b/lib/Stage.js index 1eab8f1e8..1da2584fa 100644 --- a/lib/Stage.js +++ b/lib/Stage.js @@ -50,6 +50,10 @@ class ServerlessStage extends VarContainer { this._regions[ region.getName() ] = region; } + destroy(){ + + } + removeRegion( name ){ let region = this._regions[ name ]; diff --git a/lib/actions/CodeDeployLambda.js b/lib/actions/CodeDeployLambda.js index 898e20bfa..463c58e6b 100644 --- a/lib/actions/CodeDeployLambda.js +++ b/lib/actions/CodeDeployLambda.js @@ -166,20 +166,22 @@ module.exports = function(SPlugin, serverlessPath) { SUtils.sDebug(`"${_this.evt.options.stage} - ${_this.evt.options.region} - ${_this.functionName}": Uploading lambda zip to project bucket...`); let d = new Date(), + s3Bucket = _this.meta.variables.projectBucket, + s3Region = s3Bucket.split('.')[1], key = ['serverless', _this.project.name, _this.evt.options.stage, 'lambdas', _this.functionName + '@' + d.getTime() + '.zip'].join('/'), params = { - Bucket: _this.meta.variables.projectBucket, + Bucket: s3Bucket, Key: key, ACL: 'private', ContentType: 'application/zip', Body: fs.createReadStream(_this.pathCompressed) }; - return _this.aws.request('S3', 'upload', params, _this.evt.options.stage, _this.evt.options.region) + return _this.aws.request('S3', 'upload', params, _this.evt.options.stage, s3Region) .then(function (s3Key) { // Store S3 Data - _this.s3Bucket = _this.meta.variables.projectBucket; + _this.s3Bucket = s3Bucket; _this.s3Key = s3Key; }); diff --git a/lib/actions/CodePackageLambda.js b/lib/actions/CodePackageLambda.js index 05dcb858a..96fcea607 100644 --- a/lib/actions/CodePackageLambda.js +++ b/lib/actions/CodePackageLambda.js @@ -173,6 +173,7 @@ module.exports = function(SPlugin, serverlessPath) { ); let key = ['serverless', _this.project.name, _this.evt.options.stage, _this.evt.options.region, 'envVars', '.env'].join('/'), + s3Region = _this.meta.variables.projectBucket.split('.')[1], params = { Bucket: _this.meta.variables.projectBucket, Key: key @@ -182,7 +183,7 @@ module.exports = function(SPlugin, serverlessPath) { // Get ENV file from S3 let NoSuchKey = {code: 'NoSuchKey'}; - return _this.aws.request('S3', 'getObject', params, _this.evt.options.stage, _this.evt.options.region) + return _this.aws.request('S3', 'getObject', params, _this.evt.options.stage, s3Region) .catch(NoSuchKey => ({Body: ''})) .then(function(s3ObjData) { diff --git a/lib/actions/EventDeployS3Lambda.js b/lib/actions/EventDeployS3Lambda.js index de613e32a..292455d24 100644 --- a/lib/actions/EventDeployS3Lambda.js +++ b/lib/actions/EventDeployS3Lambda.js @@ -83,6 +83,7 @@ module.exports = function(SPlugin, serverlessPath) { return _this.aws.request('Lambda', 'addPermission', params, _this.evt.options.stage, _this.evt.options.region) }) .then(function(data) { + let s3Region = _this.S.state.getMeta().variables.projectBucket.split('.')[1]; let params = { Bucket: populatedEvent.config.bucket, NotificationConfiguration: { @@ -94,7 +95,7 @@ module.exports = function(SPlugin, serverlessPath) { ] } }; - return _this.aws.request('S3', 'putBucketNotificationConfiguration', params, _this.evt.options.stage, _this.evt.options.region) + return _this.aws.request('S3', 'putBucketNotificationConfiguration', params, _this.evt.options.stage, s3Region); }) .then(function(data) { diff --git a/lib/actions/RegionCreate.js b/lib/actions/RegionCreate.js index a735fa696..fc7e07537 100644 --- a/lib/actions/RegionCreate.js +++ b/lib/actions/RegionCreate.js @@ -173,11 +173,10 @@ usage: serverless region create`, const stage = this.evt.options.stage, bucketName = this.S.state.getMeta().variables.projectBucket, - region = this.evt.options.region; - // region = bucketName.split('.')[1]; + s3Region = bucketName.split('.')[1]; return this.S.getProvider('aws') - .request('S3', 'getBucketAcl', { Bucket: bucketName }, stage, region) + .request('S3', 'getBucketAcl', { Bucket: bucketName }, stage, s3Region) .then(() => SUtils.sDebug(`Project bucket already exists: ${bucketName}`)) .catch(function(err) { @@ -193,7 +192,7 @@ usage: serverless region create`, SCli.log('Creating your project bucket on S3: ' + bucketName + '...'); return _this.S.getProvider('aws') - .request('S3', 'createBucket', {Bucket: bucketName, ACL: 'private'}, stage, region); + .request('S3', 'createBucket', {Bucket: bucketName, ACL: 'private'}, stage, s3Region); } else { @@ -209,10 +208,11 @@ usage: serverless region create`, */ _putEnvFile() { - const projectName = this.S.getProject().name, - stage = this.evt.options.stage, - region = this.evt.options.region, - key = ['serverless', projectName, stage, region, 'envVars', '.env'].join('/'); + const projectName = this.S.getProject().name, + stage = this.evt.options.stage, + region = this.evt.options.region, + projectBucket = this.S.state.getMeta().variables.projectBucket, + key = ['serverless', projectName, stage, region, 'envVars', '.env'].join('/'); // If noExeCf option, skip if (this.evt.options.noExeCf) return BbPromise.resolve(); @@ -222,8 +222,10 @@ usage: serverless region create`, SERVERLESS_DATA_MODEL_STAGE=${stage} SERVERLESS_PROJECT_NAME=${projectName}`; + let s3Region = projectBucket.split('.')[1]; + let params = { - Bucket: this.S.state.getMeta().variables.projectBucket, + Bucket: projectBucket, Key: "/" + key, ACL: 'private', ContentType: 'text/plain', @@ -231,7 +233,7 @@ SERVERLESS_PROJECT_NAME=${projectName}`; }; return this.S.getProvider('aws') - .request('S3', 'putObject', params, stage, region); + .request('S3', 'putObject', params, stage, s3Region); } /** diff --git a/lib/actions/RegionRemove.js b/lib/actions/RegionRemove.js index 631fc67f1..11068d5dd 100644 --- a/lib/actions/RegionRemove.js +++ b/lib/actions/RegionRemove.js @@ -149,17 +149,19 @@ usage: serverless region remove`, _listS3Objects() { SUtils.sDebug("List related S3 objects"); - let prefix = ['serverless', this.S.getProject().getName(), this.evt.options.stage, this.evt.options.region].join('/'), - params = { + let prefix = ['serverless', this.S.getProject().getName(), this.evt.options.stage, this.evt.options.region].join('/'), + s3Region = this.S.state.getMeta().variables.projectBucket.split('.')[1], + params = { Bucket: this.S.state.getMeta().variables.projectBucket, Prefix: prefix }; - return this.S.getProvider('aws').request('S3', 'listObjects', params, this.evt.options.stage, this.evt.options.region) + return this.S.getProvider('aws').request('S3', 'listObjects', params, this.evt.options.stage, s3Region) .then(reply => _.map(reply.Contents, (item) => ({Key: item.Key}))); } _removeS3Objects(objects) { SUtils.sDebug("Removing related S3 objects"); + let s3Region = this.S.state.getMeta().variables.projectBucket.split('.')[1]; if (objects.length) { let params = { @@ -168,7 +170,7 @@ usage: serverless region remove`, Objects: objects } }; - return this.S.getProvider('aws').request('S3', 'deleteObjects', params, this.evt.options.stage, this.evt.options.region); + return this.S.getProvider('aws').request('S3', 'deleteObjects', params, this.evt.options.stage, s3Region); } else { SUtils.sDebug("S3 objects are not found. Skipping."); return BbPromise.resolve(); diff --git a/lib/actions/ResourcesDeploy.js b/lib/actions/ResourcesDeploy.js index 36a74ea83..71064e9f3 100644 --- a/lib/actions/ResourcesDeploy.js +++ b/lib/actions/ResourcesDeploy.js @@ -384,8 +384,10 @@ usage: serverless resources deploy`, Body: JSON.stringify(cfTemplate) }; + let s3Region = bucketName.split('.')[1]; + return this.S.getProvider('aws') - .request('S3', 'putObject', params, stage, region) + .request('S3', 'putObject', params, stage, s3Region) .then(function() { const hostname = new (require('aws-sdk').S3)().endpoint.hostname; return `https://${hostname}/${bucketName}/${key}`; diff --git a/lib/actions/StageRemove.js b/lib/actions/StageRemove.js index 588320cab..864f4cd33 100644 --- a/lib/actions/StageRemove.js +++ b/lib/actions/StageRemove.js @@ -128,12 +128,11 @@ usage: serverless stage remove`, _removeMeta() { // Update Meta - this.meta = this.S.state.getMeta(); - - delete this.meta.stages[this.evt.options.stage] + let project = this.S.getProject(); + project.removeStage(this.evt.options.stage); // Save Meta before adding region - return this.meta.save(); + return project.save(); } /** diff --git a/lib/utils/index.js b/lib/utils/index.js index 141c03829..129396cf9 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -529,7 +529,8 @@ exports.getEnvFileAsMap = function(Serverless, region, stage) { deferred = Promise.resolve(fs.readFileSync(Serverless.project.getFilePath( '.env' ))); } else { let projectName = Serverless.state.meta.get().variables.project, - bucketName = Serverless.state.meta.get().variables.projectBucket; + bucketName = Serverless.state.meta.get().variables.projectBucket, + s3Region = bucketName.split('.')[1]; SCli.log(`Getting ENV file from S3 bucket: ${bucketName}`); @@ -540,7 +541,9 @@ exports.getEnvFileAsMap = function(Serverless, region, stage) { Bucket: bucketName, Key: key }; - deferred = aws.request('S3', 'getObject', params, stage, region) + + + deferred = aws.request('S3', 'getObject', params, stage, s3Region) .then(function(s3ObjData) { return (!s3ObjData.Body) ? '' : s3ObjData.Body; });