Repository: aws/aws-sam-cli

Issue:
Region from Env Vars or profile are not respected for ALL commands but package and deploy
The region option in SAM CLI was changed between 0.7.0 and 0.8.0 to add the default explicitly on the [command line option](https://github.com/awslabs/aws-sam-cli/blob/develop/samcli/cli/options.py#L44). This causes the region to always be set and not allow boto3 to do its resolving of credentials and regions, which is used to set the correct values into the docker container.

Current workaround is to explicitly set the region when invoking a function or interacting with commands that interact with AWS Services.

Fix is in #811


Discussion:

Resolving patch:
diff --git a/samcli/__init__.py b/samcli/__init__.py
--- a/samcli/__init__.py
+++ b/samcli/__init__.py
@@ -2,4 +2,4 @@
 SAM CLI version
 """
 
-__version__ = '0.8.0'
+__version__ = '0.8.1'
diff --git a/samcli/cli/options.py b/samcli/cli/options.py
--- a/samcli/cli/options.py
+++ b/samcli/cli/options.py
@@ -41,7 +41,6 @@ def callback(ctx, param, value):
     return click.option('--region',
                         expose_value=False,
                         help='Set the AWS Region of the service (e.g. us-east-1).',
-                        default='us-east-1',
                         callback=callback)(f)
 
 


Test patch:
diff --git a/tests/integration/local/invoke/test_integrations_cli.py b/tests/integration/local/invoke/test_integrations_cli.py
--- a/tests/integration/local/invoke/test_integrations_cli.py
+++ b/tests/integration/local/invoke/test_integrations_cli.py
@@ -1,6 +1,7 @@
 import json
 import shutil
 import os
+import copy
 
 from nose_parameterized import parameterized
 from subprocess import Popen, PIPE
@@ -176,6 +177,34 @@ def test_invoke_with_env_using_parameters_with_custom_region(self):
 
         self.assertEquals(environ["Region"], custom_region)
 
+    def test_invoke_with_env_with_aws_creds(self):
+        custom_region = "my-custom-region"
+        key = "key"
+        secret = "secret"
+        session = "session"
+
+        command_list = self.get_command_list("EchoEnvWithParameters",
+                                             template_path=self.template_path,
+                                             event_path=self.event_path)
+
+        env = copy.deepcopy(dict(os.environ))
+        env["AWS_DEFAULT_REGION"] = custom_region
+        env["AWS_REGION"] = custom_region
+        env["AWS_ACCESS_KEY_ID"] = key
+        env["AWS_SECRET_ACCESS_KEY"] = secret
+        env["AWS_SESSION_TOKEN"] = session
+
+        process = Popen(command_list, stdout=PIPE, env=env)
+        process.wait()
+        process_stdout = b"".join(process.stdout.readlines()).strip()
+        environ = json.loads(process_stdout.decode('utf-8'))
+
+        self.assertEquals(environ["AWS_DEFAULT_REGION"], custom_region)
+        self.assertEquals(environ["AWS_REGION"], custom_region)
+        self.assertEquals(environ["AWS_ACCESS_KEY_ID"], key)
+        self.assertEquals(environ["AWS_SECRET_ACCESS_KEY"], secret)
+        self.assertEquals(environ["AWS_SESSION_TOKEN"], session)
+
     def test_invoke_with_docker_network_of_host(self):
         command_list = self.get_command_list("HelloWorldServerlessFunction",
                                              template_path=self.template_path,