Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: helpful error message when deploy happens across regions #1562

Merged
merged 1 commit into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions samcli/commands/deploy/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ def __init__(self, stack_name, msg):
)


class DeployBucketInDifferentRegionError(UserException):
def __init__(self, msg):
self.msg = msg

message_fmt = "{msg} : deployment s3 bucket is in a different region, try sam deploy --guided"

super(DeployBucketInDifferentRegionError, self).__init__(message=message_fmt.format(msg=self.msg))


class DeployBucketRequiredError(UserException):
def __init__(self):

Expand Down
11 changes: 10 additions & 1 deletion samcli/lib/deploy/deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
import botocore

from samcli.lib.deploy.utils import DeployColor
from samcli.commands.deploy.exceptions import DeployFailedError, ChangeSetError, DeployStackOutPutFailedError
from samcli.commands.deploy.exceptions import (
DeployFailedError,
ChangeSetError,
DeployStackOutPutFailedError,
DeployBucketInDifferentRegionError,
)
from samcli.commands._utils.table_print import pprint_column_names, pprint_columns
from samcli.commands.deploy import exceptions as deploy_exceptions
from samcli.lib.package.artifact_exporter import mktempfile, parse_s3_url
Expand Down Expand Up @@ -176,6 +181,10 @@ def create_changeset(
try:
resp = self._client.create_change_set(**kwargs)
return resp, changeset_type
except botocore.exceptions.ClientError as ex:
if "The bucket you are attempting to access must be addressed using the specified endpoint" in str(ex):
raise DeployBucketInDifferentRegionError(f"Failed to create/update stack {stack_name}")

except Exception as ex:
LOG.debug("Unable to create changeset", exc_info=ex)
raise ChangeSetError(stack_name=stack_name, msg=str(ex))
Expand Down
37 changes: 36 additions & 1 deletion tests/unit/lib/deploy/test_deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

from botocore.exceptions import ClientError, WaiterError, BotoCoreError

from samcli.commands.deploy.exceptions import DeployFailedError, ChangeSetError, DeployStackOutPutFailedError
from samcli.commands.deploy.exceptions import (
DeployFailedError,
ChangeSetError,
DeployStackOutPutFailedError,
DeployBucketInDifferentRegionError,
)
from samcli.lib.deploy.deployer import Deployer
from samcli.lib.package.s3_uploader import S3Uploader
from samcli.lib.utils.time import utc_to_timestamp, to_datetime
Expand Down Expand Up @@ -158,6 +163,36 @@ def test_create_changeset_exception(self):
tags={"unit": "true"},
)

def test_create_changeset_ClientErrorException(self):
error_message = (
"An error occurred (ValidationError) when calling the CreateChangeSet "
"operation: S3 error: The bucket you are attempting to access must be "
"addressed using the specified endpoint. "
"Please send all future requests to this "
"endpoint.\nFor more information "
"check http://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html"
)
self.deployer.has_stack = MagicMock(return_value=False)
self.deployer._client.create_change_set = MagicMock(
side_effect=ClientError(
error_response={"Error": {"Message": error_message}}, operation_name="create_changeset"
)
)
with self.assertRaises(DeployBucketInDifferentRegionError):
self.deployer.create_changeset(
stack_name="test",
cfn_template=" ",
parameter_values=[
{"ParameterKey": "a", "ParameterValue": "b"},
{"ParameterKey": "c", "UsePreviousValue": True},
],
capabilities=["CAPABILITY_IAM"],
role_arn="role-arn",
notification_arns=[],
s3_uploader=S3Uploader(s3_client=self.s3_client, bucket_name="test_bucket"),
tags={"unit": "true"},
)

def test_describe_changeset_with_changes(self):
response = [
{
Expand Down