Repository: serverless/serverless (JavaScript) — Bug Fix

Issue:
Receive "Deployment bucket is not in the same region as the lambda function" when using us-east-1
<!--
1. If you have a question and not a bug/feature request please ask it at http://forum.serverless.com
2. Please check if an issue already exists so there are no duplicates
3. Check out and follow our Guidelines: https://github.com/serverless/serverless/blob/master/CONTRIBUTING.md
4. Fill out the whole template so we have a good overview on the issue
5. Do not remove any section of the template. If something is not applicable leave it empty but leave it in the Issue
6. Please follow the template, otherwise we'll have to ask you to update it
-->
# This is a Bug Report
## Description
- What went wrong?

When attempting to use a pre-existing S3 bucket in us-east-1 via the `deploymentBucket` configuration option, I receive an error response "Deployment bucket is not in the same region as the lambda function".

Debugging through the Serverless code, I observed that it is attempting to match a LocationConstraint returned from the AWS SDK to the region; however, [AWS Documentation](http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html) states: 

> When the bucket's region is US East (N. Virginia), Amazon S3 returns an empty string for the bucket's region

Due to this, it appears the check in **configureStack.js** to match the bucket's region with the specified region fails and I receive the error message.
- What did you expect should have happened?

I should have been able to use the pre-existing bucket.
- What was the config you used?

```
...
provider:
  name: aws
  runtime: nodejs4.3
  stage: dev
  region: us-east-1
  deploymentBucket: my-bucket-name
...
```
- What stacktrace or error message from your provider did you see?

Deployment bucket is not in the same region as the lambda function
## Additional Data
- **_Serverless Framework Version you're using**_: 1.0.2
- **_Operating System**_: OSX
- **_Stack Trace**_:
- **_Provider Error messages**_:

```
  Serverless Error ---------------------------------------

     Deployment bucket is not in the same region as the lambda
     function.
```

Receive "Deployment bucket is not in the same region as the lambda function" when using us-east-1
<!--
1. If you have a question and not a bug/feature request please ask it at http://forum.serverless.com
2. Please check if an issue already exists so there are no duplicates
3. Check out and follow our Guidelines: https://github.com/serverless/serverless/blob/master/CONTRIBUTING.md
4. Fill out the whole template so we have a good overview on the issue
5. Do not remove any section of the template. If something is not applicable leave it empty but leave it in the Issue
6. Please follow the template, otherwise we'll have to ask you to update it
-->
# This is a Bug Report
## Description
- What went wrong?

When attempting to use a pre-existing S3 bucket in us-east-1 via the `deploymentBucket` configuration option, I receive an error response "Deployment bucket is not in the same region as the lambda function".

Debugging through the Serverless code, I observed that it is attempting to match a LocationConstraint returned from the AWS SDK to the region; however, [AWS Documentation](http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html) states: 

> When the bucket's region is US East (N. Virginia), Amazon S3 returns an empty string for the bucket's region

Due to this, it appears the check in **configureStack.js** to match the bucket's region with the specified region fails and I receive the error message.
- What did you expect should have happened?

I should have been able to use the pre-existing bucket.
- What was the config you used?

```
...
provider:
  name: aws
  runtime: nodejs4.3
  stage: dev
  region: us-east-1
  deploymentBucket: my-bucket-name
...
```
- What stacktrace or error message from your provider did you see?

Deployment bucket is not in the same region as the lambda function
## Additional Data
- **_Serverless Framework Version you're using**_: 1.0.2
- **_Operating System**_: OSX
- **_Stack Trace**_:
- **_Provider Error messages**_:

```
  Serverless Error ---------------------------------------

     Deployment bucket is not in the same region as the lambda
     function.
```



Discussion:
Thanks for reporting and investigating @briguy202 😊 .... I've pushed a fix along with [this PR](https://github.com/serverless/serverless/pull/2385) and will be merged very soon. It's a high priority!

Cheers 👼 

Is there a workaround for this one? I was hoping that omitting `provider.region` would get around it but no dice.

@doapp-ryanp, unfortunately I don't think there is other than to patch the issue or wait on #2385 which looks to be very close to completion.  If you opt for patching, which is what we did, it's a one-liner change - see https://github.com/briguy202/serverless/commit/750c55b275ba5a022479154dfd89f26092818682.

Should be resolved with #2385 /cc @flomotlik 

I've got the same issue happening when using pre-existing deployment bucket in ```eu-west-1``` region in some of AWS accounts in my company. The reason is AWS returns ```LocationConstraint``` value as ```EU``` which is one of the values for ```eu-west-1``` according to [AWS docs](http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html):

> Valid Values: [ us-west-1 | us-west-2 | ca-central-1 | **EU or eu-west-1** | eu-west-2 | eu-central-1 | ap-south-1 | ap-southeast-1 | ap-southeast-2 | ap-northeast-1 | ap-northeast-2 | sa-east-1 | empty string (for the US East (N. Virginia) region) | us-east-2]

This could be one-liner fix in **configureStack.js** similar to the fix for original issue:
```javascript
if (result.LocationConstraint === 'EU') result.LocationConstraint = 'eu-west-1';
```

Should I raise a separate issue for this case or current one could be reopened?
BOOM! 💥 That is one nice catch @ssemyonov ... I feel like we're always on a mission to keep up with AWS inconsistencies 😄 ... I'm gonna PR a quick fix today.
Hi @eahefnawy are you still looking into EU - eu-west-1 issue? Do you need a hand?
Thanks for reporting and investigating @briguy202 😊 .... I've pushed a fix along with [this PR](https://github.com/serverless/serverless/pull/2385) and will be merged very soon. It's a high priority!

Cheers 👼 

Is there a workaround for this one? I was hoping that omitting `provider.region` would get around it but no dice.

@doapp-ryanp, unfortunately I don't think there is other than to patch the issue or wait on #2385 which looks to be very close to completion.  If you opt for patching, which is what we did, it's a one-liner change - see https://github.com/briguy202/serverless/commit/750c55b275ba5a022479154dfd89f26092818682.

Should be resolved with #2385 /cc @flomotlik 

I've got the same issue happening when using pre-existing deployment bucket in ```eu-west-1``` region in some of AWS accounts in my company. The reason is AWS returns ```LocationConstraint``` value as ```EU``` which is one of the values for ```eu-west-1``` according to [AWS docs](http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html):

> Valid Values: [ us-west-1 | us-west-2 | ca-central-1 | **EU or eu-west-1** | eu-west-2 | eu-central-1 | ap-south-1 | ap-southeast-1 | ap-southeast-2 | ap-northeast-1 | ap-northeast-2 | sa-east-1 | empty string (for the US East (N. Virginia) region) | us-east-2]

This could be one-liner fix in **configureStack.js** similar to the fix for original issue:
```javascript
if (result.LocationConstraint === 'EU') result.LocationConstraint = 'eu-west-1';
```

Should I raise a separate issue for this case or current one could be reopened?
BOOM! 💥 That is one nice catch @ssemyonov ... I feel like we're always on a mission to keep up with AWS inconsistencies 😄 ... I'm gonna PR a quick fix today.
Hi @eahefnawy are you still looking into EU - eu-west-1 issue? Do you need a hand?

Resolving patch:
diff --git a/lib/plugins/aws/deploy/lib/configureStack.js b/lib/plugins/aws/deploy/lib/configureStack.js
index 48d024c0f56..393c3d9e204 100644
--- a/lib/plugins/aws/deploy/lib/configureStack.js
+++ b/lib/plugins/aws/deploy/lib/configureStack.js
@@ -31,6 +31,7 @@ module.exports = {
         .then(resultParam => {
           const result = resultParam;
           if (result.LocationConstraint === '') result.LocationConstraint = 'us-east-1';
+          if (result.LocationConstraint === 'EU') result.LocationConstraint = 'eu-west-1';
           if (result.LocationConstraint !== this.options.region) {
             throw new this.serverless.classes.Error(
               'Deployment bucket is not in the same region as the lambda function'


Test patch:
diff --git a/lib/plugins/aws/deploy/lib/configureStack.test.js b/lib/plugins/aws/deploy/lib/configureStack.test.js
index be7b5e2fcbe..2a0cd50602a 100644
--- a/lib/plugins/aws/deploy/lib/configureStack.test.js
+++ b/lib/plugins/aws/deploy/lib/configureStack.test.js
@@ -110,4 +110,30 @@ describe('#configureStack', () => {
         ).to.not.exist;
       });
   });
+
+  [
+    { region: 'eu-west-1', response: 'EU' },
+    { region: 'us-east-1', response: '' },
+  ].forEach((value) => {
+    it(`should handle inconsistent getBucketLocation responses for ${value.region} region`, () => {
+      const bucketName = 'com.serverless.deploys';
+
+      awsPlugin.options.region = value.region;
+
+      sinon
+        .stub(awsPlugin.provider, 'request').returns(
+          BbPromise.resolve({ LocationConstraint: value.response })
+        );
+
+      awsPlugin.serverless.service.provider.deploymentBucket = bucketName;
+      return awsPlugin.configureStack()
+        .then(() => {
+          expect(
+            awsPlugin.serverless.service.provider.compiledCloudFormationTemplate
+              .Outputs.ServerlessDeploymentBucketName.Value
+          ).to.equal(bucketName);
+          awsPlugin.provider.request.restore();
+        });
+    });
+  });
 });