From 47ac543fd4ad12be61534698f5bcfb4d086c7c0b Mon Sep 17 00:00:00 2001 From: Tristan Hill Date: Fri, 9 Jun 2017 16:54:52 +0100 Subject: [PATCH] don't set UsePreviousValue if parameters doesn't exist (fixes #2599) --- awscli/customizations/cloudformation/deployer.py | 10 +++++++++- .../customizations/cloudformation/test_deployer.py | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/awscli/customizations/cloudformation/deployer.py b/awscli/customizations/cloudformation/deployer.py index 67ffe4520a5a..919423e32667 100644 --- a/awscli/customizations/cloudformation/deployer.py +++ b/awscli/customizations/cloudformation/deployer.py @@ -88,7 +88,6 @@ def create_changeset(self, stack_name, cfn_template, # Each changeset will get a unique name based on time changeset_name = self.changeset_prefix + str(int(time.time())) - changeset_type = "UPDATE" if not self.has_stack(stack_name): changeset_type = "CREATE" # When creating a new stack, UsePreviousValue=True is invalid. @@ -96,6 +95,15 @@ def create_changeset(self, stack_name, cfn_template, # or set a Default value in template to successfully create a stack. parameter_values = [x for x in parameter_values if not x.get("UsePreviousValue", False)] + else: + changeset_type = "UPDATE" + # UsePreviousValue not valid if parameter is new + summary = self._client.get_template_summary(StackName=stack_name) + existing_parameters = [parameter['ParameterKey'] for parameter in \ + summary['Parameters']] + parameter_values = [x for x in parameter_values + if not (x.get("UsePreviousValue", False) and \ + x["ParameterKey"] not in existing_parameters)] kwargs = { 'ChangeSetName': changeset_name, diff --git a/tests/unit/customizations/cloudformation/test_deployer.py b/tests/unit/customizations/cloudformation/test_deployer.py index 8f9a5ae81cf0..7cb5d636d522 100644 --- a/tests/unit/customizations/cloudformation/test_deployer.py +++ b/tests/unit/customizations/cloudformation/test_deployer.py @@ -135,10 +135,18 @@ def test_create_changeset_success(self): # Case 2: Stack exists. We are updating it self.deployer.has_stack.return_value = True + self.stub_client.add_response("get_template_summary", + {"Parameters": [{"ParameterKey": parameter["ParameterKey"]} + for parameter in parameters]}, + {"StackName": stack_name}) expected_params["ChangeSetType"] = "UPDATE" expected_params["Parameters"] = parameters self.stub_client.add_response("create_change_set", response, expected_params) + # template has new parameter but should not be included in + # expected_params as no previous value + parameters = list(parameters) + \ + [{"ParameterKey": "New", "UsePreviousValue": True}] with self.stub_client: result = self.deployer.create_changeset( stack_name, template, parameters, capabilities, role_arn,