From 1c2e550284aefe7798226194e3ad3cb2b5af95ee Mon Sep 17 00:00:00 2001 From: Keeton Hodgson Date: Mon, 27 Apr 2020 10:48:33 -0700 Subject: [PATCH 01/16] fix code deploy conditions bug --- .../preferences/deployment_preference.py | 25 ++++++++++++++++--- .../deployment_preference_collection.py | 17 +++++++++++-- samtranslator/model/sam_resources.py | 10 ++++++-- .../preferences/test_deployment_preference.py | 13 ++++++---- tests/translator/test_function_resources.py | 10 +++++--- 5 files changed, 59 insertions(+), 16 deletions(-) diff --git a/samtranslator/model/preferences/deployment_preference.py b/samtranslator/model/preferences/deployment_preference.py index 0b2026d5d..cca77feea 100644 --- a/samtranslator/model/preferences/deployment_preference.py +++ b/samtranslator/model/preferences/deployment_preference.py @@ -25,7 +25,16 @@ """ DeploymentPreferenceTuple = namedtuple( "DeploymentPreferenceTuple", - ["deployment_type", "pre_traffic_hook", "post_traffic_hook", "alarms", "enabled", "role", "trigger_configurations"], + [ + "deployment_type", + "pre_traffic_hook", + "post_traffic_hook", + "alarms", + "enabled", + "role", + "trigger_configurations", + "condition", + ], ) @@ -37,17 +46,18 @@ class DeploymentPreference(DeploymentPreferenceTuple): """ @classmethod - def from_dict(cls, logical_id, deployment_preference_dict): + def from_dict(cls, logical_id, deployment_preference_dict, condition=None): """ :param logical_id: the logical_id of the resource that owns this deployment preference :param deployment_preference_dict: the dict object taken from the SAM template + :param condition: condition on this deployment preference :return: """ enabled = deployment_preference_dict.get("Enabled", True) enabled = False if enabled in ["false", "False"] else enabled if not enabled: - return DeploymentPreference(None, None, None, None, False, None, None) + return DeploymentPreference(None, None, None, None, False, None, None, None) if "Type" not in deployment_preference_dict: raise InvalidResourceException(logical_id, "'DeploymentPreference' is missing required Property 'Type'") @@ -65,5 +75,12 @@ def from_dict(cls, logical_id, deployment_preference_dict): role = deployment_preference_dict.get("Role", None) trigger_configurations = deployment_preference_dict.get("TriggerConfigurations", None) return DeploymentPreference( - deployment_type, pre_traffic_hook, post_traffic_hook, alarms, enabled, role, trigger_configurations + deployment_type, + pre_traffic_hook, + post_traffic_hook, + alarms, + enabled, + role, + trigger_configurations, + condition, ) diff --git a/samtranslator/model/preferences/deployment_preference_collection.py b/samtranslator/model/preferences/deployment_preference_collection.py index 5f1c13029..6e5b82dd6 100644 --- a/samtranslator/model/preferences/deployment_preference_collection.py +++ b/samtranslator/model/preferences/deployment_preference_collection.py @@ -48,13 +48,14 @@ def __init__(self): self.codedeploy_application = self._codedeploy_application() self.codedeploy_iam_role = self._codedeploy_iam_role() - def add(self, logical_id, deployment_preference_dict): + def add(self, logical_id, deployment_preference_dict, condition=None): """ Add this deployment preference to the collection :raise ValueError if an existing logical id already exists in the _resource_preferences :param logical_id: logical id of the resource where this deployment preference applies :param deployment_preference_dict: the input SAM template deployment preference mapping + :param condition: the condition (if it exists) on the serverless function """ if logical_id in self._resource_preferences: raise ValueError( @@ -63,7 +64,9 @@ def add(self, logical_id, deployment_preference_dict): ) ) - self._resource_preferences[logical_id] = DeploymentPreference.from_dict(logical_id, deployment_preference_dict) + self._resource_preferences[logical_id] = DeploymentPreference.from_dict( + logical_id, deployment_preference_dict, condition + ) def get(self, logical_id): """ @@ -85,6 +88,13 @@ def can_skip_service_role(self): """ return all(preference.role or not preference.enabled for preference in self._resource_preferences.values()) + def needs_resource_condition(self): + """ + If all preferences have a condition, all code deploy resources need to be conditionally created + :return: True, if a condition needs to be created + """ + return all(preference.condition for preference in self._resource_preferences.values()) + def enabled_logical_ids(self): """ :return: only the logical id's for the deployment preferences in this collection which are enabled @@ -156,6 +166,9 @@ def deployment_group(self, function_logical_id): if deployment_preference.trigger_configurations: deployment_group.TriggerConfigurations = deployment_preference.trigger_configurations + if deployment_preference.condition: + deployment_group.set_resource_attribute("Condition", deployment_preference.condition) + return deployment_group def _convert_alarms(self, preference_alarms): diff --git a/samtranslator/model/sam_resources.py b/samtranslator/model/sam_resources.py index c614c56d3..e39d2c719 100644 --- a/samtranslator/model/sam_resources.py +++ b/samtranslator/model/sam_resources.py @@ -185,6 +185,7 @@ def to_cloudformation(self, **kwargs): lambda_alias, intrinsics_resolver, mappings_resolver, + self.get_passthrough_resource_attributes(), ) event_invoke_policies = [] if self.EventInvokeConfig: @@ -827,7 +828,7 @@ def _construct_alias(self, name, function, version): return alias def _validate_deployment_preference_and_add_update_policy( - self, deployment_preference_collection, lambda_alias, intrinsics_resolver, mappings_resolver + self, deployment_preference_collection, lambda_alias, intrinsics_resolver, mappings_resolver, condition ): if "Enabled" in self.DeploymentPreference: # resolve intrinsics and mappings for Type @@ -843,10 +844,15 @@ def _validate_deployment_preference_and_add_update_policy( preference_type = mappings_resolver.resolve_parameter_refs(preference_type) self.DeploymentPreference["Type"] = preference_type + if condition: + condition = condition.get("Condition") + if deployment_preference_collection is None: raise ValueError("deployment_preference_collection required for parsing the deployment preference") - deployment_preference_collection.add(self.logical_id, self.DeploymentPreference) + deployment_preference_collection.add( + self.logical_id, self.DeploymentPreference, condition, + ) if deployment_preference_collection.get(self.logical_id).enabled: if self.AutoPublishAlias is None: diff --git a/tests/translator/model/preferences/test_deployment_preference.py b/tests/translator/model/preferences/test_deployment_preference.py index e6fbc9d8e..8eab88baa 100644 --- a/tests/translator/model/preferences/test_deployment_preference.py +++ b/tests/translator/model/preferences/test_deployment_preference.py @@ -16,6 +16,7 @@ def setUp(self): "TriggerTargetArn": {"Ref": "MySNSTopic"}, "TriggerName": "TestTrigger", } + self.condition = "condition" self.expected_deployment_preference = DeploymentPreference( self.deployment_type, self.pre_traffic_hook, @@ -24,6 +25,7 @@ def setUp(self): True, self.role, self.trigger_configurations, + self.condition, ) def test_from_dict_with_intrinsic_function_type(self): @@ -37,6 +39,7 @@ def test_from_dict_with_intrinsic_function_type(self): True, self.role, self.trigger_configurations, + self.condition, ) deployment_preference_yaml_dict = dict() @@ -49,7 +52,7 @@ def test_from_dict_with_intrinsic_function_type(self): deployment_preference_yaml_dict["Role"] = self.role deployment_preference_yaml_dict["TriggerConfigurations"] = self.trigger_configurations deployment_preference_from_yaml_dict = DeploymentPreference.from_dict( - "logical_id", deployment_preference_yaml_dict + "logical_id", deployment_preference_yaml_dict, self.condition ) self.assertEqual(expected_deployment_preference, deployment_preference_from_yaml_dict) @@ -65,13 +68,13 @@ def test_from_dict(self): deployment_preference_yaml_dict["Role"] = self.role deployment_preference_yaml_dict["TriggerConfigurations"] = self.trigger_configurations deployment_preference_from_yaml_dict = DeploymentPreference.from_dict( - "logical_id", deployment_preference_yaml_dict + "logical_id", deployment_preference_yaml_dict, self.condition ) self.assertEqual(self.expected_deployment_preference, deployment_preference_from_yaml_dict) def test_from_dict_with_disabled_preference_does_not_require_other_parameters(self): - expected_deployment_preference = DeploymentPreference(None, None, None, None, False, None, None) + expected_deployment_preference = DeploymentPreference(None, None, None, None, False, None, None, None) deployment_preference_yaml_dict = dict() deployment_preference_yaml_dict["Enabled"] = False @@ -82,7 +85,7 @@ def test_from_dict_with_disabled_preference_does_not_require_other_parameters(se self.assertEqual(expected_deployment_preference, deployment_preference_from_yaml_dict) def test_from_dict_with_string_disabled_preference_does_not_require_other_parameters(self): - expected_deployment_preference = DeploymentPreference(None, None, None, None, False, None, None) + expected_deployment_preference = DeploymentPreference(None, None, None, None, False, None, None, None) deployment_preference_yaml_dict = dict() deployment_preference_yaml_dict["Enabled"] = "False" @@ -93,7 +96,7 @@ def test_from_dict_with_string_disabled_preference_does_not_require_other_parame self.assertEqual(expected_deployment_preference, deployment_preference_from_yaml_dict) def test_from_dict_with_lowercase_string_disabled_preference_does_not_require_other_parameters(self): - expected_deployment_preference = DeploymentPreference(None, None, None, None, False, None, None) + expected_deployment_preference = DeploymentPreference(None, None, None, None, False, None, None, None) deployment_preference_yaml_dict = dict() deployment_preference_yaml_dict["Enabled"] = "false" diff --git a/tests/translator/test_function_resources.py b/tests/translator/test_function_resources.py index 279668155..f2675ef7b 100644 --- a/tests/translator/test_function_resources.py +++ b/tests/translator/test_function_resources.py @@ -144,7 +144,9 @@ def test_sam_function_with_deployment_preference(self, get_resolved_alias_name_m resources = sam_func.to_cloudformation(**kwargs) deployment_preference_collection.update_policy.assert_called_once_with(self.sam_func.logical_id) - deployment_preference_collection.add.assert_called_once_with(self.sam_func.logical_id, deploy_preference_dict) + deployment_preference_collection.add.assert_called_once_with( + self.sam_func.logical_id, deploy_preference_dict, None + ) aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type] @@ -222,7 +224,7 @@ def test_sam_function_with_disabled_deployment_preference_does_not_add_update_po resources = sam_func.to_cloudformation(**kwargs) - preference_collection.add.assert_called_once_with(sam_func.logical_id, deploy_preference_dict) + preference_collection.add.assert_called_once_with(sam_func.logical_id, deploy_preference_dict, None) preference_collection.get.assert_called_once_with(sam_func.logical_id) self.intrinsics_resolver_mock.resolve_parameter_refs.assert_called_with(enabled) aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type] @@ -318,7 +320,9 @@ def test_sam_function_with_deployment_preference_intrinsic_ref_enabled_boolean_p resources = sam.to_cloudformation(**kwargs) deployment_preference_collection.update_policy.assert_called_once_with(self.sam_func.logical_id) - deployment_preference_collection.add.assert_called_once_with(self.sam_func.logical_id, deploy_preference_dict) + deployment_preference_collection.add.assert_called_once_with( + self.sam_func.logical_id, deploy_preference_dict, None + ) self.intrinsics_resolver_mock.resolve_parameter_refs.assert_any_call(enabled) aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type] From 7aacc65e79b82ed8b691e01ebde64d85b7c68524 Mon Sep 17 00:00:00 2001 From: Keeton Hodgson Date: Mon, 27 Apr 2020 22:17:22 -0700 Subject: [PATCH 02/16] Cover all condition cases, update tests --- .../deployment_preference_collection.py | 55 +- samtranslator/translator/translator.py | 8 +- ...ction_with_deployment_no_service_role.yaml | 7 + ..._with_deployment_preference_condition.yaml | 30 + ...ence_multiple_combinations_conditions.yaml | 86 +++ .../test_deployment_preference_collection.py | 5 +- ...ction_with_deployment_no_service_role.json | 67 +- ..._with_deployment_preference_condition.json | 209 +++++++ ...ence_multiple_combinations_conditions.json | 588 ++++++++++++++++++ ...ction_with_deployment_no_service_role.json | 67 +- ..._with_deployment_preference_condition.json | 209 +++++++ ...ence_multiple_combinations_conditions.json | 588 ++++++++++++++++++ ...ction_with_deployment_no_service_role.json | 67 +- ..._with_deployment_preference_condition.json | 209 +++++++ ...ence_multiple_combinations_conditions.json | 588 ++++++++++++++++++ tests/translator/test_translator.py | 2 + 16 files changed, 2693 insertions(+), 92 deletions(-) create mode 100644 tests/translator/input/function_with_deployment_preference_condition.yaml create mode 100644 tests/translator/input/function_with_deployment_preference_multiple_combinations_conditions.yaml create mode 100644 tests/translator/output/aws-cn/function_with_deployment_preference_condition.json create mode 100644 tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions.json create mode 100644 tests/translator/output/aws-us-gov/function_with_deployment_preference_condition.json create mode 100644 tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions.json create mode 100644 tests/translator/output/function_with_deployment_preference_condition.json create mode 100644 tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions.json diff --git a/samtranslator/model/preferences/deployment_preference_collection.py b/samtranslator/model/preferences/deployment_preference_collection.py index 6e5b82dd6..98ab34032 100644 --- a/samtranslator/model/preferences/deployment_preference_collection.py +++ b/samtranslator/model/preferences/deployment_preference_collection.py @@ -9,6 +9,9 @@ is_intrinsic_if, is_intrinsic_no_value, validate_intrinsic_if_items, + make_combined_condition, + ref, + fnGetAtt ) from samtranslator.model.update_policy import UpdatePolicy from samtranslator.translator.arn_generator import ArnGenerator @@ -27,6 +30,7 @@ "Linear10PercentEvery10Minutes", "AllAtOnce", ] +CODE_DEPLOY_CONDITION_NAME = "ServerlessCodeDeployCondition" class DeploymentPreferenceCollection(object): @@ -41,12 +45,10 @@ class DeploymentPreferenceCollection(object): def __init__(self): """ - This collection stores an intenral dict of the deployment preferences for each function's - deployment preference in the SAM Template. + This collection stores an internal dict of the deployment preferences for each function's + deployment preference in the SAM Template. """ self._resource_preferences = {} - self.codedeploy_application = self._codedeploy_application() - self.codedeploy_iam_role = self._codedeploy_iam_role() def add(self, logical_id, deployment_preference_dict, condition=None): """ @@ -93,7 +95,28 @@ def needs_resource_condition(self): If all preferences have a condition, all code deploy resources need to be conditionally created :return: True, if a condition needs to be created """ - return all(preference.condition for preference in self._resource_preferences.values()) + # If there are any enabled deployment preferences without conditions, return false + return self._resource_preferences and not any( + not preference.condition and preference.enabled for preference in self._resource_preferences.values() + ) + + def get_all_deployment_conditions(self): + """ + Returns a list of all conditions associated with the deployment preference resources + :return: List of condition names + """ + conditions_set = set([preference.condition for preference in self._resource_preferences.values()]) + if None in conditions_set: + # None can exist if there are disabled deployment preference(s) + conditions_set.remove(None) + return list(conditions_set) + + def create_aggregate_deployment_condition(self): + """ + Creates an aggregate deployment condition if necessary + :return: None if <2 conditions are found, otherwise a dictionary of new conditions to add to template + """ + return make_combined_condition(self.get_all_deployment_conditions(), CODE_DEPLOY_CONDITION_NAME) def enabled_logical_ids(self): """ @@ -101,12 +124,18 @@ def enabled_logical_ids(self): """ return [logical_id for logical_id, preference in self._resource_preferences.items() if preference.enabled] - def _codedeploy_application(self): + def get_codedeploy_application(self): codedeploy_application_resource = CodeDeployApplication(CODEDEPLOY_APPLICATION_LOGICAL_ID) codedeploy_application_resource.ComputePlatform = "Lambda" + if self.needs_resource_condition(): + conditions = self.get_all_deployment_conditions() + condition_name = CODE_DEPLOY_CONDITION_NAME + if len(conditions) <= 1: + condition_name = conditions.pop() + codedeploy_application_resource.set_resource_attribute("Condition", condition_name) return codedeploy_application_resource - def _codedeploy_iam_role(self): + def get_codedeploy_iam_role(self): iam_role = IAMRole(CODE_DEPLOY_SERVICE_ROLE_LOGICAL_ID) iam_role.AssumeRolePolicyDocument = { "Version": "2012-10-17", @@ -130,6 +159,12 @@ def _codedeploy_iam_role(self): ArnGenerator.generate_aws_managed_policy_arn("service-role/AWSCodeDeployRoleForLambda") ] + if self.needs_resource_condition(): + conditions = self.get_all_deployment_conditions() + condition_name = CODE_DEPLOY_CONDITION_NAME + if len(conditions) <= 1: + condition_name = conditions.pop() + iam_role.set_resource_attribute("Condition", condition_name) return iam_role def deployment_group(self, function_logical_id): @@ -147,7 +182,7 @@ def deployment_group(self, function_logical_id): except ValueError as e: raise InvalidResourceException(function_logical_id, str(e)) - deployment_group.ApplicationName = self.codedeploy_application.get_runtime_attr("name") + deployment_group.ApplicationName = ref(CODEDEPLOY_APPLICATION_LOGICAL_ID) deployment_group.AutoRollbackConfiguration = { "Enabled": True, "Events": ["DEPLOYMENT_FAILURE", "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST"], @@ -159,7 +194,7 @@ def deployment_group(self, function_logical_id): deployment_group.DeploymentStyle = {"DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL"} - deployment_group.ServiceRoleArn = self.codedeploy_iam_role.get_runtime_attr("arn") + deployment_group.ServiceRoleArn = fnGetAtt(CODE_DEPLOY_SERVICE_ROLE_LOGICAL_ID, "Arn") if deployment_preference.role: deployment_group.ServiceRoleArn = deployment_preference.role @@ -253,7 +288,7 @@ def update_policy(self, function_logical_id): deployment_preference = self.get(function_logical_id) return UpdatePolicy( - self.codedeploy_application.get_runtime_attr("name"), + ref(CODEDEPLOY_APPLICATION_LOGICAL_ID), self.deployment_group(function_logical_id).get_runtime_attr("name"), deployment_preference.pre_traffic_hook, deployment_preference.post_traffic_hook, diff --git a/samtranslator/translator/translator.py b/samtranslator/translator/translator.py index 2824dd8d6..bf1ca9cfb 100644 --- a/samtranslator/translator/translator.py +++ b/samtranslator/translator/translator.py @@ -167,10 +167,14 @@ def translate(self, sam_template, parameter_values, feature_toggle=None, passthr document_errors.append(e) if deployment_preference_collection.any_enabled(): - template["Resources"].update(deployment_preference_collection.codedeploy_application.to_dict()) + template["Resources"].update(deployment_preference_collection.get_codedeploy_application().to_dict()) + if deployment_preference_collection.needs_resource_condition(): + new_conditions = deployment_preference_collection.create_aggregate_deployment_condition() + if new_conditions: + template.get("Conditions").update(new_conditions) if not deployment_preference_collection.can_skip_service_role(): - template["Resources"].update(deployment_preference_collection.codedeploy_iam_role.to_dict()) + template["Resources"].update(deployment_preference_collection.get_codedeploy_iam_role().to_dict()) for logical_id in deployment_preference_collection.enabled_logical_ids(): try: diff --git a/tests/translator/input/function_with_deployment_no_service_role.yaml b/tests/translator/input/function_with_deployment_no_service_role.yaml index ae40a6a00..ef92307c8 100644 --- a/tests/translator/input/function_with_deployment_no_service_role.yaml +++ b/tests/translator/input/function_with_deployment_no_service_role.yaml @@ -1,3 +1,9 @@ +Conditions: + Condition1: + Fn::Equals: + - true + - true + Globals: Function: AutoPublishAlias: live @@ -15,6 +21,7 @@ Resources: Runtime: python2.7 OtherFunction: + Condition: Condition1 Type: 'AWS::Serverless::Function' Properties: CodeUri: s3://sam-demo-bucket/hello.zip diff --git a/tests/translator/input/function_with_deployment_preference_condition.yaml b/tests/translator/input/function_with_deployment_preference_condition.yaml new file mode 100644 index 000000000..cbcd16ccc --- /dev/null +++ b/tests/translator/input/function_with_deployment_preference_condition.yaml @@ -0,0 +1,30 @@ +Parameters: + FnName: + Type: String + ProvisionedConcurrency: + Type: String + Default: 10 + EnableAliasProvisionedConcurrency: + Type: String + AllowedValues: + - true + - false + Default: true +Conditions: + AliasProvisionedConcurrencyEnabled: !Equals [!Ref EnableAliasProvisionedConcurrency, true] + FunctionCondition: !Equals [true, true] +Resources: + MinimalFunction: + Type: 'AWS::Serverless::Function' + Condition: FunctionCondition + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + AutoPublishAlias: live + DeploymentPreference: + Type: Linear10PercentEvery3Minutes + ProvisionedConcurrencyConfig: !If + - AliasProvisionedConcurrencyEnabled + - ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency + - !Ref 'AWS::NoValue' diff --git a/tests/translator/input/function_with_deployment_preference_multiple_combinations_conditions.yaml b/tests/translator/input/function_with_deployment_preference_multiple_combinations_conditions.yaml new file mode 100644 index 000000000..819e3e1e1 --- /dev/null +++ b/tests/translator/input/function_with_deployment_preference_multiple_combinations_conditions.yaml @@ -0,0 +1,86 @@ +Conditions: + Condition1: + Fn::Equals: + - true + - true + Condition2: + Fn::Equals: + - true + - false + Condition3: + Fn::Equals: + - true + - false + +Parameters: + MyFalseParameter: + Type: String + Default: False + +Resources: + MinimalFunction: + Condition: Condition1 + Type: 'AWS::Serverless::Function' + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + AutoPublishAlias: live + DeploymentPreference: + Type: Linear10PercentEvery2Minutes + + MinimalFunctionWithMinimalDeploymentPreference: + Type: 'AWS::Serverless::Function' + Condition: Condition2 + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + AutoPublishAlias: livewithdeployment + DeploymentPreference: + Type: Canary10Percent5Minutes + + MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms: + Type: 'AWS::Serverless::Function' + Condition: Condition2 + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + AutoPublishAlias: livewithdeploymentwithhooksandalarms + DeploymentPreference: + Type: Linear10PercentEvery2Minutes + Hooks: + PreTraffic: !Ref MySanityTestFunction + PostTraffic: !Ref MyValidationTestFunction + Alarms: + - !Ref MyCloudWatchAlarm + + MySanityTestFunction: + Type: 'AWS::Serverless::Function' + Condition: Condition3 + Properties: + Handler: hello.handler + Runtime: python2.7 + CodeUri: s3://my-bucket/mySanityTestFunction.zip + DeploymentPreference: + Enabled: False + + MyValidationTestFunction: + Type: 'AWS::Serverless::Function' + Properties: + Handler: hello.handler + Runtime: python2.7 + CodeUri: s3://my-bucket/myValidationTestFunction.zip + DeploymentPreference: + Enabled: !Ref MyFalseParameter + + MyCloudWatchAlarm: + Type: AWS::CloudWatch::Alarm + Properties: + ComparisonOperator: GreaterThanThreshold + EvaluationPeriods: 1 + MetricName: MyMetric + Namespace: AWS/EC2 + Period: 300 + Threshold: 10 diff --git a/tests/translator/model/preferences/test_deployment_preference_collection.py b/tests/translator/model/preferences/test_deployment_preference_collection.py index 6ca0d2c59..2ee6bf81d 100644 --- a/tests/translator/model/preferences/test_deployment_preference_collection.py +++ b/tests/translator/model/preferences/test_deployment_preference_collection.py @@ -18,6 +18,7 @@ def setup_method(self, method): self.post_traffic_host_global = "post_traffic_function_ref" self.pre_traffic_hook_global = "pre_traffic_function_ref" self.function_logical_id = "FunctionLogicalId" + self.condition = "CodeDeployCondition" @patch("boto3.session.Session.region_name", "ap-southeast-1") def test_when_no_global_dict_each_local_deployment_preference_requires_parameters(self): @@ -37,7 +38,7 @@ def test_codedeploy_application(self): expected_codedeploy_application_resource.ComputePlatform = "Lambda" self.assertEqual( - DeploymentPreferenceCollection().codedeploy_application.to_dict(), + DeploymentPreferenceCollection().get_codedeploy_application().to_dict(), expected_codedeploy_application_resource.to_dict(), ) @@ -59,7 +60,7 @@ def test_codedeploy_iam_role(self): ] self.assertEqual( - DeploymentPreferenceCollection().codedeploy_iam_role.to_dict(), expected_codedeploy_iam_role.to_dict() + DeploymentPreferenceCollection().get_codedeploy_iam_role().to_dict(), expected_codedeploy_iam_role.to_dict() ) @patch("boto3.session.Session.region_name", "ap-southeast-1") diff --git a/tests/translator/output/aws-cn/function_with_deployment_no_service_role.json b/tests/translator/output/aws-cn/function_with_deployment_no_service_role.json index 65d6a4483..3ca072428 100644 --- a/tests/translator/output/aws-cn/function_with_deployment_no_service_role.json +++ b/tests/translator/output/aws-cn/function_with_deployment_no_service_role.json @@ -1,4 +1,12 @@ { + "Conditions": { + "Condition1": { + "Fn::Equals": [ + true, + true + ] + } + }, "Resources": { "OtherFunctionDeploymentGroup": { "Type": "AWS::CodeDeploy::DeploymentGroup", @@ -29,16 +37,17 @@ "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } - } + }, + "Condition": "Condition1" }, "MinimalFunction": { "Type": "AWS::Lambda::Function", "Properties": { + "Handler": "hello.handler", "Code": { "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" }, - "Handler": "hello.handler", "Role": { "Fn::GetAtt": [ "MinimalFunctionRole", @@ -57,15 +66,6 @@ "MinimalFunctionRole": { "Type": "AWS::IAM::Role", "Properties": { - "ManagedPolicyArns": [ - "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], - "Tags": [ - { - "Value": "SAM", - "Key": "lambda:createdBy" - } - ], "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ @@ -81,30 +81,32 @@ } } ] - } + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] } }, "OtherFunctionVersion640128d35d": { + "UpdateReplacePolicy": "Delete", "DeletionPolicy": "Retain", "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "OtherFunction" } - } + }, + "Condition": "Condition1" }, "OtherFunctionRole": { "Type": "AWS::IAM::Role", "Properties": { - "ManagedPolicyArns": [ - "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], - "Tags": [ - { - "Value": "SAM", - "Key": "lambda:createdBy" - } - ], "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ @@ -120,8 +122,18 @@ } } ] - } - } + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition1" }, "DeploymentRole": { "Type": "AWS::IAM::Role", @@ -185,6 +197,7 @@ } }, "MinimalFunctionVersion640128d35d": { + "UpdateReplacePolicy": "Delete", "DeletionPolicy": "Retain", "Type": "AWS::Lambda::Version", "Properties": { @@ -196,11 +209,11 @@ "OtherFunction": { "Type": "AWS::Lambda::Function", "Properties": { + "Handler": "hello.handler", "Code": { "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" }, - "Handler": "hello.handler", "Role": { "Fn::GetAtt": [ "OtherFunctionRole", @@ -214,7 +227,8 @@ "Key": "lambda:createdBy" } ] - } + }, + "Condition": "Condition1" }, "MinimalFunctionAliaslive": { "Type": "AWS::Lambda::Alias", @@ -253,6 +267,7 @@ } } }, + "Condition": "Condition1", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ diff --git a/tests/translator/output/aws-cn/function_with_deployment_preference_condition.json b/tests/translator/output/aws-cn/function_with_deployment_preference_condition.json new file mode 100644 index 000000000..e91204c8a --- /dev/null +++ b/tests/translator/output/aws-cn/function_with_deployment_preference_condition.json @@ -0,0 +1,209 @@ +{ + "Conditions": { + "AliasProvisionedConcurrencyEnabled": { + "Fn::Equals": [ + { + "Ref": "EnableAliasProvisionedConcurrency" + }, + true + ] + }, + "FunctionCondition": { + "Fn::Equals": [ + true, + true + ] + } + }, + "Parameters": { + "EnableAliasProvisionedConcurrency": { + "Default": true, + "Type": "String", + "AllowedValues": [ + true, + false + ] + }, + "FnName": { + "Type": "String" + }, + "ProvisionedConcurrency": { + "Default": 10, + "Type": "String" + } + }, + "Resources": { + "MinimalFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + }, + "Condition": "FunctionCondition" + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "FunctionCondition" + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "FunctionCondition" + }, + "CodeDeployServiceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + } + }, + "Condition": "FunctionCondition" + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + }, + "Condition": "FunctionCondition" + }, + "MinimalFunctionVersion640128d35d": { + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunction" + } + }, + "Condition": "FunctionCondition" + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Condition": "FunctionCondition", + "Properties": { + "Name": "live", + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + }, + "FunctionName": { + "Ref": "MinimalFunction" + } + } + } + } +} \ No newline at end of file diff --git a/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions.json b/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions.json new file mode 100644 index 000000000..e3cd22cab --- /dev/null +++ b/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions.json @@ -0,0 +1,588 @@ +{ + "Conditions": { + "ServerlessCodeDeployCondition": { + "Fn::Or": [ + { + "Condition": "Condition2" + }, + { + "Condition": "Condition1" + } + ] + }, + "Condition3": { + "Fn::Equals": [ + true, + false + ] + }, + "Condition2": { + "Fn::Equals": [ + true, + false + ] + }, + "Condition1": { + "Fn::Equals": [ + true, + true + ] + } + }, + "Parameters": { + "MyFalseParameter": { + "Default": false, + "Type": "String" + } + }, + "Resources": { + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition2" + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition1" + }, + "MyCloudWatchAlarm": { + "Type": "AWS::CloudWatch::Alarm", + "Properties": { + "EvaluationPeriods": 1, + "Namespace": "AWS/EC2", + "Period": 300, + "ComparisonOperator": "GreaterThanThreshold", + "Threshold": 10, + "MetricName": "MyMetric" + } + }, + "MinimalFunctionWithMinimalDeploymentPreference": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionWithMinimalDeploymentPreferenceRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition2" + }, + "MinimalFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery2Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + }, + "Condition": "Condition1" + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d": { + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" + } + }, + "Condition": "Condition2" + }, + "CodeDeployServiceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + } + }, + "Condition": "ServerlessCodeDeployCondition" + }, + "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Canary10Percent5Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + }, + "Condition": "Condition2" + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsAliaslivewithdeploymentwithhooksandalarms": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "BeforeAllowTrafficHook": { + "Ref": "MySanityTestFunction" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup" + }, + "AfterAllowTrafficHook": { + "Ref": "MyValidationTestFunction" + } + } + }, + "Condition": "Condition2", + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" + }, + "Name": "livewithdeploymentwithhooksandalarms" + } + }, + "MyValidationTestFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + }, + "MinimalFunctionWithMinimalDeploymentPreferenceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition2" + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition1" + }, + "MySanityTestFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition3" + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery2Minutes" + } + ] + }, + "AlarmConfiguration": { + "Alarms": [ + { + "Name": { + "Ref": "MyCloudWatchAlarm" + } + } + ], + "Enabled": true + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + }, + "Condition": "Condition2" + }, + "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d": { + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreference" + } + }, + "Condition": "Condition2" + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition2" + }, + "MinimalFunctionWithMinimalDeploymentPreferenceAliaslivewithdeployment": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup" + } + } + }, + "Condition": "Condition2", + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreference" + }, + "Name": "livewithdeployment" + } + }, + "MySanityTestFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "my-bucket", + "S3Key": "mySanityTestFunction.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MySanityTestFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition3" + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + }, + "Condition": "ServerlessCodeDeployCondition" + }, + "MinimalFunctionVersion640128d35d": { + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunction" + } + }, + "Condition": "Condition1" + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Condition": "Condition1", + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "MinimalFunction" + }, + "Name": "live" + } + }, + "MyValidationTestFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "my-bucket", + "S3Key": "myValidationTestFunction.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MyValidationTestFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + } + } +} \ No newline at end of file diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_no_service_role.json b/tests/translator/output/aws-us-gov/function_with_deployment_no_service_role.json index ca6cf4a1e..1e17ca77e 100644 --- a/tests/translator/output/aws-us-gov/function_with_deployment_no_service_role.json +++ b/tests/translator/output/aws-us-gov/function_with_deployment_no_service_role.json @@ -1,4 +1,12 @@ { + "Conditions": { + "Condition1": { + "Fn::Equals": [ + true, + true + ] + } + }, "Resources": { "OtherFunctionDeploymentGroup": { "Type": "AWS::CodeDeploy::DeploymentGroup", @@ -29,16 +37,17 @@ "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } - } + }, + "Condition": "Condition1" }, "MinimalFunction": { "Type": "AWS::Lambda::Function", "Properties": { + "Handler": "hello.handler", "Code": { "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" }, - "Handler": "hello.handler", "Role": { "Fn::GetAtt": [ "MinimalFunctionRole", @@ -57,15 +66,6 @@ "MinimalFunctionRole": { "Type": "AWS::IAM::Role", "Properties": { - "ManagedPolicyArns": [ - "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], - "Tags": [ - { - "Value": "SAM", - "Key": "lambda:createdBy" - } - ], "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ @@ -81,30 +81,32 @@ } } ] - } + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] } }, "OtherFunctionVersion640128d35d": { + "UpdateReplacePolicy": "Delete", "DeletionPolicy": "Retain", "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "OtherFunction" } - } + }, + "Condition": "Condition1" }, "OtherFunctionRole": { "Type": "AWS::IAM::Role", "Properties": { - "ManagedPolicyArns": [ - "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], - "Tags": [ - { - "Value": "SAM", - "Key": "lambda:createdBy" - } - ], "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ @@ -120,8 +122,18 @@ } } ] - } - } + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition1" }, "DeploymentRole": { "Type": "AWS::IAM::Role", @@ -185,6 +197,7 @@ } }, "MinimalFunctionVersion640128d35d": { + "UpdateReplacePolicy": "Delete", "DeletionPolicy": "Retain", "Type": "AWS::Lambda::Version", "Properties": { @@ -196,11 +209,11 @@ "OtherFunction": { "Type": "AWS::Lambda::Function", "Properties": { + "Handler": "hello.handler", "Code": { "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" }, - "Handler": "hello.handler", "Role": { "Fn::GetAtt": [ "OtherFunctionRole", @@ -214,7 +227,8 @@ "Key": "lambda:createdBy" } ] - } + }, + "Condition": "Condition1" }, "MinimalFunctionAliaslive": { "Type": "AWS::Lambda::Alias", @@ -253,6 +267,7 @@ } } }, + "Condition": "Condition1", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_preference_condition.json b/tests/translator/output/aws-us-gov/function_with_deployment_preference_condition.json new file mode 100644 index 000000000..0a8936b3e --- /dev/null +++ b/tests/translator/output/aws-us-gov/function_with_deployment_preference_condition.json @@ -0,0 +1,209 @@ +{ + "Conditions": { + "AliasProvisionedConcurrencyEnabled": { + "Fn::Equals": [ + { + "Ref": "EnableAliasProvisionedConcurrency" + }, + true + ] + }, + "FunctionCondition": { + "Fn::Equals": [ + true, + true + ] + } + }, + "Parameters": { + "EnableAliasProvisionedConcurrency": { + "Default": true, + "Type": "String", + "AllowedValues": [ + true, + false + ] + }, + "FnName": { + "Type": "String" + }, + "ProvisionedConcurrency": { + "Default": 10, + "Type": "String" + } + }, + "Resources": { + "MinimalFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + }, + "Condition": "FunctionCondition" + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "FunctionCondition" + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "FunctionCondition" + }, + "CodeDeployServiceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + } + }, + "Condition": "FunctionCondition" + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + }, + "Condition": "FunctionCondition" + }, + "MinimalFunctionVersion640128d35d": { + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunction" + } + }, + "Condition": "FunctionCondition" + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Condition": "FunctionCondition", + "Properties": { + "Name": "live", + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + }, + "FunctionName": { + "Ref": "MinimalFunction" + } + } + } + } +} \ No newline at end of file diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions.json b/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions.json new file mode 100644 index 000000000..4c82df4aa --- /dev/null +++ b/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions.json @@ -0,0 +1,588 @@ +{ + "Conditions": { + "ServerlessCodeDeployCondition": { + "Fn::Or": [ + { + "Condition": "Condition2" + }, + { + "Condition": "Condition1" + } + ] + }, + "Condition3": { + "Fn::Equals": [ + true, + false + ] + }, + "Condition2": { + "Fn::Equals": [ + true, + false + ] + }, + "Condition1": { + "Fn::Equals": [ + true, + true + ] + } + }, + "Parameters": { + "MyFalseParameter": { + "Default": false, + "Type": "String" + } + }, + "Resources": { + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition2" + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition1" + }, + "MyCloudWatchAlarm": { + "Type": "AWS::CloudWatch::Alarm", + "Properties": { + "EvaluationPeriods": 1, + "Namespace": "AWS/EC2", + "Period": 300, + "ComparisonOperator": "GreaterThanThreshold", + "Threshold": 10, + "MetricName": "MyMetric" + } + }, + "MinimalFunctionWithMinimalDeploymentPreference": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionWithMinimalDeploymentPreferenceRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition2" + }, + "MinimalFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery2Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + }, + "Condition": "Condition1" + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d": { + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" + } + }, + "Condition": "Condition2" + }, + "CodeDeployServiceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + } + }, + "Condition": "ServerlessCodeDeployCondition" + }, + "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Canary10Percent5Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + }, + "Condition": "Condition2" + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsAliaslivewithdeploymentwithhooksandalarms": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "BeforeAllowTrafficHook": { + "Ref": "MySanityTestFunction" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup" + }, + "AfterAllowTrafficHook": { + "Ref": "MyValidationTestFunction" + } + } + }, + "Condition": "Condition2", + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" + }, + "Name": "livewithdeploymentwithhooksandalarms" + } + }, + "MyValidationTestFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + }, + "MinimalFunctionWithMinimalDeploymentPreferenceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition2" + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition1" + }, + "MySanityTestFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition3" + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery2Minutes" + } + ] + }, + "AlarmConfiguration": { + "Alarms": [ + { + "Name": { + "Ref": "MyCloudWatchAlarm" + } + } + ], + "Enabled": true + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + }, + "Condition": "Condition2" + }, + "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d": { + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreference" + } + }, + "Condition": "Condition2" + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition2" + }, + "MinimalFunctionWithMinimalDeploymentPreferenceAliaslivewithdeployment": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup" + } + } + }, + "Condition": "Condition2", + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreference" + }, + "Name": "livewithdeployment" + } + }, + "MySanityTestFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "my-bucket", + "S3Key": "mySanityTestFunction.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MySanityTestFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition3" + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + }, + "Condition": "ServerlessCodeDeployCondition" + }, + "MinimalFunctionVersion640128d35d": { + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunction" + } + }, + "Condition": "Condition1" + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Condition": "Condition1", + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "MinimalFunction" + }, + "Name": "live" + } + }, + "MyValidationTestFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "my-bucket", + "S3Key": "myValidationTestFunction.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MyValidationTestFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + } + } +} \ No newline at end of file diff --git a/tests/translator/output/function_with_deployment_no_service_role.json b/tests/translator/output/function_with_deployment_no_service_role.json index ef9ed4aa6..5040a0ed4 100644 --- a/tests/translator/output/function_with_deployment_no_service_role.json +++ b/tests/translator/output/function_with_deployment_no_service_role.json @@ -1,4 +1,12 @@ { + "Conditions": { + "Condition1": { + "Fn::Equals": [ + true, + true + ] + } + }, "Resources": { "OtherFunctionDeploymentGroup": { "Type": "AWS::CodeDeploy::DeploymentGroup", @@ -29,16 +37,17 @@ "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } - } + }, + "Condition": "Condition1" }, "MinimalFunction": { "Type": "AWS::Lambda::Function", "Properties": { + "Handler": "hello.handler", "Code": { "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" }, - "Handler": "hello.handler", "Role": { "Fn::GetAtt": [ "MinimalFunctionRole", @@ -57,15 +66,6 @@ "MinimalFunctionRole": { "Type": "AWS::IAM::Role", "Properties": { - "ManagedPolicyArns": [ - "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], - "Tags": [ - { - "Value": "SAM", - "Key": "lambda:createdBy" - } - ], "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ @@ -81,30 +81,32 @@ } } ] - } + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] } }, "OtherFunctionVersion640128d35d": { + "UpdateReplacePolicy": "Delete", "DeletionPolicy": "Retain", "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "OtherFunction" } - } + }, + "Condition": "Condition1" }, "OtherFunctionRole": { "Type": "AWS::IAM::Role", "Properties": { - "ManagedPolicyArns": [ - "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], - "Tags": [ - { - "Value": "SAM", - "Key": "lambda:createdBy" - } - ], "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ @@ -120,8 +122,18 @@ } } ] - } - } + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition1" }, "DeploymentRole": { "Type": "AWS::IAM::Role", @@ -185,6 +197,7 @@ } }, "MinimalFunctionVersion640128d35d": { + "UpdateReplacePolicy": "Delete", "DeletionPolicy": "Retain", "Type": "AWS::Lambda::Version", "Properties": { @@ -196,11 +209,11 @@ "OtherFunction": { "Type": "AWS::Lambda::Function", "Properties": { + "Handler": "hello.handler", "Code": { "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" }, - "Handler": "hello.handler", "Role": { "Fn::GetAtt": [ "OtherFunctionRole", @@ -214,7 +227,8 @@ "Key": "lambda:createdBy" } ] - } + }, + "Condition": "Condition1" }, "MinimalFunctionAliaslive": { "Type": "AWS::Lambda::Alias", @@ -253,6 +267,7 @@ } } }, + "Condition": "Condition1", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ diff --git a/tests/translator/output/function_with_deployment_preference_condition.json b/tests/translator/output/function_with_deployment_preference_condition.json new file mode 100644 index 000000000..5c2b65f26 --- /dev/null +++ b/tests/translator/output/function_with_deployment_preference_condition.json @@ -0,0 +1,209 @@ +{ + "Conditions": { + "AliasProvisionedConcurrencyEnabled": { + "Fn::Equals": [ + { + "Ref": "EnableAliasProvisionedConcurrency" + }, + true + ] + }, + "FunctionCondition": { + "Fn::Equals": [ + true, + true + ] + } + }, + "Parameters": { + "EnableAliasProvisionedConcurrency": { + "Default": true, + "Type": "String", + "AllowedValues": [ + true, + false + ] + }, + "FnName": { + "Type": "String" + }, + "ProvisionedConcurrency": { + "Default": 10, + "Type": "String" + } + }, + "Resources": { + "MinimalFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + }, + "Condition": "FunctionCondition" + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "FunctionCondition" + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "FunctionCondition" + }, + "CodeDeployServiceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + } + }, + "Condition": "FunctionCondition" + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + }, + "Condition": "FunctionCondition" + }, + "MinimalFunctionVersion640128d35d": { + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunction" + } + }, + "Condition": "FunctionCondition" + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Condition": "FunctionCondition", + "Properties": { + "Name": "live", + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + }, + "FunctionName": { + "Ref": "MinimalFunction" + } + } + } + } +} \ No newline at end of file diff --git a/tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions.json b/tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions.json new file mode 100644 index 000000000..a115ee311 --- /dev/null +++ b/tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions.json @@ -0,0 +1,588 @@ +{ + "Conditions": { + "ServerlessCodeDeployCondition": { + "Fn::Or": [ + { + "Condition": "Condition2" + }, + { + "Condition": "Condition1" + } + ] + }, + "Condition3": { + "Fn::Equals": [ + true, + false + ] + }, + "Condition2": { + "Fn::Equals": [ + true, + false + ] + }, + "Condition1": { + "Fn::Equals": [ + true, + true + ] + } + }, + "Parameters": { + "MyFalseParameter": { + "Default": false, + "Type": "String" + } + }, + "Resources": { + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition2" + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition1" + }, + "MyCloudWatchAlarm": { + "Type": "AWS::CloudWatch::Alarm", + "Properties": { + "EvaluationPeriods": 1, + "Namespace": "AWS/EC2", + "Period": 300, + "ComparisonOperator": "GreaterThanThreshold", + "Threshold": 10, + "MetricName": "MyMetric" + } + }, + "MinimalFunctionWithMinimalDeploymentPreference": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionWithMinimalDeploymentPreferenceRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition2" + }, + "MinimalFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery2Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + }, + "Condition": "Condition1" + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d": { + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" + } + }, + "Condition": "Condition2" + }, + "CodeDeployServiceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + } + }, + "Condition": "ServerlessCodeDeployCondition" + }, + "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Canary10Percent5Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + }, + "Condition": "Condition2" + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsAliaslivewithdeploymentwithhooksandalarms": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "BeforeAllowTrafficHook": { + "Ref": "MySanityTestFunction" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup" + }, + "AfterAllowTrafficHook": { + "Ref": "MyValidationTestFunction" + } + } + }, + "Condition": "Condition2", + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" + }, + "Name": "livewithdeploymentwithhooksandalarms" + } + }, + "MyValidationTestFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + }, + "MinimalFunctionWithMinimalDeploymentPreferenceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition2" + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition1" + }, + "MySanityTestFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition3" + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery2Minutes" + } + ] + }, + "AlarmConfiguration": { + "Alarms": [ + { + "Name": { + "Ref": "MyCloudWatchAlarm" + } + } + ], + "Enabled": true + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + }, + "Condition": "Condition2" + }, + "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d": { + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreference" + } + }, + "Condition": "Condition2" + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition2" + }, + "MinimalFunctionWithMinimalDeploymentPreferenceAliaslivewithdeployment": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup" + } + } + }, + "Condition": "Condition2", + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreference" + }, + "Name": "livewithdeployment" + } + }, + "MySanityTestFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "my-bucket", + "S3Key": "mySanityTestFunction.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MySanityTestFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition3" + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + }, + "Condition": "ServerlessCodeDeployCondition" + }, + "MinimalFunctionVersion640128d35d": { + "UpdateReplacePolicy": "Delete", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunction" + } + }, + "Condition": "Condition1" + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Condition": "Condition1", + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "MinimalFunction" + }, + "Name": "live" + } + }, + "MyValidationTestFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "my-bucket", + "S3Key": "myValidationTestFunction.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MyValidationTestFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + } + } +} \ No newline at end of file diff --git a/tests/translator/test_translator.py b/tests/translator/test_translator.py index 80bdd456f..48955690e 100644 --- a/tests/translator/test_translator.py +++ b/tests/translator/test_translator.py @@ -366,10 +366,12 @@ class TestTranslatorEndToEnd(AbstractTestTranslator): "function_with_disabled_deployment_preference", "function_with_disabled_traffic_hook", "function_with_deployment_preference", + "function_with_deployment_preference_condition", "function_with_deployment_preference_all_parameters", "function_with_deployment_preference_from_parameters", "function_with_deployment_preference_multiple_combinations", "function_with_deployment_preference_alarms_intrinsic_if", + "function_with_deployment_preference_multiple_combinations_conditions", "function_with_alias_and_event_sources", "function_with_resource_refs", "function_with_deployment_and_custom_role", From 2ec132fa61dc78365114d9446e74a62d84eb3d56 Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Fri, 22 Apr 2022 15:40:32 -0700 Subject: [PATCH 03/16] black reformat --- .../model/preferences/deployment_preference_collection.py | 2 +- samtranslator/model/sam_resources.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/samtranslator/model/preferences/deployment_preference_collection.py b/samtranslator/model/preferences/deployment_preference_collection.py index 98ab34032..bd5be65f9 100644 --- a/samtranslator/model/preferences/deployment_preference_collection.py +++ b/samtranslator/model/preferences/deployment_preference_collection.py @@ -11,7 +11,7 @@ validate_intrinsic_if_items, make_combined_condition, ref, - fnGetAtt + fnGetAtt, ) from samtranslator.model.update_policy import UpdatePolicy from samtranslator.translator.arn_generator import ArnGenerator diff --git a/samtranslator/model/sam_resources.py b/samtranslator/model/sam_resources.py index e39d2c719..f37df2bfe 100644 --- a/samtranslator/model/sam_resources.py +++ b/samtranslator/model/sam_resources.py @@ -851,7 +851,9 @@ def _validate_deployment_preference_and_add_update_policy( raise ValueError("deployment_preference_collection required for parsing the deployment preference") deployment_preference_collection.add( - self.logical_id, self.DeploymentPreference, condition, + self.logical_id, + self.DeploymentPreference, + condition, ) if deployment_preference_collection.get(self.logical_id).enabled: From b2736f4f705e4b3834e6c4da11fb3ec30579b212 Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Wed, 4 May 2022 09:42:03 -0700 Subject: [PATCH 04/16] Update tests to use .get_codedeploy_iam_role() instead of .codedeploy_iam_role --- .../preferences/test_deployment_preference_collection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/model/preferences/test_deployment_preference_collection.py b/tests/unit/model/preferences/test_deployment_preference_collection.py index 0ab04c949..163660aed 100644 --- a/tests/unit/model/preferences/test_deployment_preference_collection.py +++ b/tests/unit/model/preferences/test_deployment_preference_collection.py @@ -20,7 +20,7 @@ def test_codedeploy_iam_role_contains_AWSCodeDeployRoleForLambdaLimited_managedp ) as get_partition_name_patch: get_partition_name_patch.return_value = partition - iam_role = DeploymentPreferenceCollection().codedeploy_iam_role + iam_role = DeploymentPreferenceCollection().get_codedeploy_iam_role() self.assertIn( "arn:{}:iam::aws:policy/service-role/AWSCodeDeployRoleForLambdaLimited".format(partition), @@ -41,7 +41,7 @@ def test_codedeploy_iam_role_contains_AWSCodeDeployRoleForLambda_managedpolicy(s ) as get_partition_name_patch: get_partition_name_patch.return_value = partition - iam_role = DeploymentPreferenceCollection().codedeploy_iam_role + iam_role = DeploymentPreferenceCollection().get_codedeploy_iam_role() self.assertIn( "arn:{}:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda".format(partition), From 38fcc4844a5f51a0c9633127b209cde6d941be9b Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Wed, 4 May 2022 14:44:54 -0700 Subject: [PATCH 05/16] Fixing unit tests after merging Condition fix commit --- ...ction_with_deployment_no_service_role.json | 184 ++++---- ..._with_deployment_preference_condition.json | 137 +++--- ...ence_multiple_combinations_conditions.json | 401 +++++++++--------- ...ction_with_deployment_no_service_role.json | 184 ++++---- ..._with_deployment_preference_condition.json | 137 +++--- ...ence_multiple_combinations_conditions.json | 401 +++++++++--------- ...ction_with_deployment_no_service_role.json | 184 ++++---- ..._with_deployment_preference_condition.json | 137 +++--- ...ence_multiple_combinations_conditions.json | 401 +++++++++--------- 9 files changed, 1074 insertions(+), 1092 deletions(-) diff --git a/tests/translator/output/aws-cn/function_with_deployment_no_service_role.json b/tests/translator/output/aws-cn/function_with_deployment_no_service_role.json index 3ca072428..801fd6cb3 100644 --- a/tests/translator/output/aws-cn/function_with_deployment_no_service_role.json +++ b/tests/translator/output/aws-cn/function_with_deployment_no_service_role.json @@ -2,78 +2,78 @@ "Conditions": { "Condition1": { "Fn::Equals": [ - true, + true, true ] } - }, + }, "Resources": { "OtherFunctionDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "ServiceRoleArn": { "Ref": "DeploymentRole" - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "AllAtOnce" } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } - }, + }, "Condition": "Condition1" - }, + }, "MinimalFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MinimalFunctionRole", + "MinimalFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] } - }, + }, "MinimalFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -81,40 +81,39 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] } - }, + }, "OtherFunctionVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "OtherFunction" } - }, + }, "Condition": "Condition1" - }, + }, "OtherFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -122,33 +121,33 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition1" - }, + }, "DeploymentRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole" - ], + ], "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "codedeploy.amazonaws.com" @@ -158,128 +157,127 @@ ] } } - }, + }, "MinimalFunctionDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "ServiceRoleArn": { "Ref": "DeploymentRole" - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "AllAtOnce" } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } } - }, + }, "ServerlessDeploymentApplication": { - "Type": "AWS::CodeDeploy::Application", + "Type": "AWS::CodeDeploy::Application", "Properties": { "ComputePlatform": "Lambda" } - }, + }, "MinimalFunctionVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "MinimalFunction" } } - }, + }, "OtherFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "OtherFunctionRole", + "OtherFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition1" - }, + }, "MinimalFunctionAliaslive": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "MinimalFunctionDeploymentGroup" } } - }, + }, "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "MinimalFunctionVersion640128d35d", + "MinimalFunctionVersion640128d35d", "Version" ] - }, + }, "FunctionName": { "Ref": "MinimalFunction" - }, + }, "Name": "live" } - }, + }, "OtherFunctionAliaslive": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "OtherFunctionDeploymentGroup" } } - }, - "Condition": "Condition1", + }, + "Condition": "Condition1", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "OtherFunctionVersion640128d35d", + "OtherFunctionVersion640128d35d", "Version" ] - }, + }, "FunctionName": { "Ref": "OtherFunction" - }, + }, "Name": "live" } } } -} \ No newline at end of file +} diff --git a/tests/translator/output/aws-cn/function_with_deployment_preference_condition.json b/tests/translator/output/aws-cn/function_with_deployment_preference_condition.json index e91204c8a..1e82b6612 100644 --- a/tests/translator/output/aws-cn/function_with_deployment_preference_condition.json +++ b/tests/translator/output/aws-cn/function_with_deployment_preference_condition.json @@ -4,81 +4,81 @@ "Fn::Equals": [ { "Ref": "EnableAliasProvisionedConcurrency" - }, + }, true ] - }, + }, "FunctionCondition": { "Fn::Equals": [ - true, + true, true ] } - }, + }, "Parameters": { "EnableAliasProvisionedConcurrency": { - "Default": true, - "Type": "String", + "Default": true, + "Type": "String", "AllowedValues": [ - true, + true, false ] - }, + }, "FnName": { "Type": "String" - }, + }, "ProvisionedConcurrency": { - "Default": 10, + "Default": 10, "Type": "String" } - }, + }, "Resources": { "MinimalFunctionDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "ServiceRoleArn": { "Fn::GetAtt": [ - "CodeDeployServiceRole", + "CodeDeployServiceRole", "Arn" ] - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Linear10PercentEvery3Minutes" } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } - }, + }, "Condition": "FunctionCondition" - }, + }, "MinimalFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -86,57 +86,57 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "FunctionCondition" - }, + }, "MinimalFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MinimalFunctionRole", + "MinimalFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "FunctionCondition" - }, + }, "CodeDeployServiceRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ "arn:aws-cn:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" - ], + ], "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "codedeploy.amazonaws.com" @@ -145,65 +145,64 @@ } ] } - }, + }, "Condition": "FunctionCondition" - }, + }, "ServerlessDeploymentApplication": { - "Type": "AWS::CodeDeploy::Application", + "Type": "AWS::CodeDeploy::Application", "Properties": { "ComputePlatform": "Lambda" - }, + }, "Condition": "FunctionCondition" - }, + }, "MinimalFunctionVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "MinimalFunction" } - }, + }, "Condition": "FunctionCondition" - }, + }, "MinimalFunctionAliaslive": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "MinimalFunctionDeploymentGroup" } } - }, - "Condition": "FunctionCondition", + }, + "Condition": "FunctionCondition", "Properties": { - "Name": "live", + "Name": "live", "FunctionVersion": { "Fn::GetAtt": [ - "MinimalFunctionVersion640128d35d", + "MinimalFunctionVersion640128d35d", "Version" ] - }, + }, "ProvisionedConcurrencyConfig": { "Fn::If": [ - "AliasProvisionedConcurrencyEnabled", + "AliasProvisionedConcurrencyEnabled", { "ProvisionedConcurrentExecutions": { "Ref": "ProvisionedConcurrency" } - }, + }, { "Ref": "AWS::NoValue" } ] - }, + }, "FunctionName": { "Ref": "MinimalFunction" } } } } -} \ No newline at end of file +} diff --git a/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions.json b/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions.json index e3cd22cab..2999c3fd1 100644 --- a/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions.json +++ b/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions.json @@ -4,181 +4,180 @@ "Fn::Or": [ { "Condition": "Condition2" - }, + }, { "Condition": "Condition1" } ] - }, + }, "Condition3": { "Fn::Equals": [ - true, + true, false ] - }, + }, "Condition2": { "Fn::Equals": [ - true, + true, false ] - }, + }, "Condition1": { "Fn::Equals": [ - true, + true, true ] } - }, + }, "Parameters": { "MyFalseParameter": { - "Default": false, + "Default": false, "Type": "String" } - }, + }, "Resources": { "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole", + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MinimalFunctionRole", + "MinimalFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition1" - }, + }, "MyCloudWatchAlarm": { - "Type": "AWS::CloudWatch::Alarm", + "Type": "AWS::CloudWatch::Alarm", "Properties": { - "EvaluationPeriods": 1, - "Namespace": "AWS/EC2", - "Period": 300, - "ComparisonOperator": "GreaterThanThreshold", - "Threshold": 10, + "EvaluationPeriods": 1, + "Namespace": "AWS/EC2", + "Period": 300, + "ComparisonOperator": "GreaterThanThreshold", + "Threshold": 10, "MetricName": "MyMetric" } - }, + }, "MinimalFunctionWithMinimalDeploymentPreference": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MinimalFunctionWithMinimalDeploymentPreferenceRole", + "MinimalFunctionWithMinimalDeploymentPreferenceRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "ServiceRoleArn": { "Fn::GetAtt": [ - "CodeDeployServiceRole", + "CodeDeployServiceRole", "Arn" ] - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Linear10PercentEvery2Minutes" } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } - }, + }, "Condition": "Condition1" - }, + }, "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" } - }, + }, "Condition": "Condition2" - }, + }, "CodeDeployServiceRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ "arn:aws-cn:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" - ], + ], "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "codedeploy.amazonaws.com" @@ -187,87 +186,87 @@ } ] } - }, + }, "Condition": "ServerlessCodeDeployCondition" - }, + }, "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "ServiceRoleArn": { "Fn::GetAtt": [ - "CodeDeployServiceRole", + "CodeDeployServiceRole", "Arn" ] - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Canary10Percent5Minutes" } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsAliaslivewithdeploymentwithhooksandalarms": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "BeforeAllowTrafficHook": { "Ref": "MySanityTestFunction" - }, + }, "DeploymentGroupName": { "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup" - }, + }, "AfterAllowTrafficHook": { "Ref": "MyValidationTestFunction" } } - }, - "Condition": "Condition2", + }, + "Condition": "Condition2", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d", + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d", "Version" ] - }, + }, "FunctionName": { "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" - }, + }, "Name": "livewithdeploymentwithhooksandalarms" } - }, + }, "MyValidationTestFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -275,29 +274,29 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] } - }, + }, "MinimalFunctionWithMinimalDeploymentPreferenceRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -305,30 +304,30 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -336,30 +335,30 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition1" - }, + }, "MySanityTestFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -367,41 +366,41 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition3" - }, + }, "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Linear10PercentEvery2Minutes" } ] - }, + }, "AlarmConfiguration": { "Alarms": [ { @@ -409,44 +408,43 @@ "Ref": "MyCloudWatchAlarm" } } - ], + ], "Enabled": true - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" - }, + }, "ServiceRoleArn": { "Fn::GetAtt": [ - "CodeDeployServiceRole", + "CodeDeployServiceRole", "Arn" ] } - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "MinimalFunctionWithMinimalDeploymentPreference" } - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -454,135 +452,134 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionWithMinimalDeploymentPreferenceAliaslivewithdeployment": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup" } } - }, - "Condition": "Condition2", + }, + "Condition": "Condition2", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d", + "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d", "Version" ] - }, + }, "FunctionName": { "Ref": "MinimalFunctionWithMinimalDeploymentPreference" - }, + }, "Name": "livewithdeployment" } - }, + }, "MySanityTestFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "my-bucket", + "S3Bucket": "my-bucket", "S3Key": "mySanityTestFunction.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MySanityTestFunctionRole", + "MySanityTestFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition3" - }, + }, "ServerlessDeploymentApplication": { - "Type": "AWS::CodeDeploy::Application", + "Type": "AWS::CodeDeploy::Application", "Properties": { "ComputePlatform": "Lambda" - }, + }, "Condition": "ServerlessCodeDeployCondition" - }, + }, "MinimalFunctionVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "MinimalFunction" } - }, + }, "Condition": "Condition1" - }, + }, "MinimalFunctionAliaslive": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "MinimalFunctionDeploymentGroup" } } - }, - "Condition": "Condition1", + }, + "Condition": "Condition1", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "MinimalFunctionVersion640128d35d", + "MinimalFunctionVersion640128d35d", "Version" ] - }, + }, "FunctionName": { "Ref": "MinimalFunction" - }, + }, "Name": "live" } - }, + }, "MyValidationTestFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "my-bucket", + "S3Bucket": "my-bucket", "S3Key": "myValidationTestFunction.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MyValidationTestFunctionRole", + "MyValidationTestFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] } } } -} \ No newline at end of file +} diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_no_service_role.json b/tests/translator/output/aws-us-gov/function_with_deployment_no_service_role.json index 1e17ca77e..8300aab2f 100644 --- a/tests/translator/output/aws-us-gov/function_with_deployment_no_service_role.json +++ b/tests/translator/output/aws-us-gov/function_with_deployment_no_service_role.json @@ -2,78 +2,78 @@ "Conditions": { "Condition1": { "Fn::Equals": [ - true, + true, true ] } - }, + }, "Resources": { "OtherFunctionDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "ServiceRoleArn": { "Ref": "DeploymentRole" - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "AllAtOnce" } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } - }, + }, "Condition": "Condition1" - }, + }, "MinimalFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MinimalFunctionRole", + "MinimalFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] } - }, + }, "MinimalFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -81,40 +81,39 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] } - }, + }, "OtherFunctionVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "OtherFunction" } - }, + }, "Condition": "Condition1" - }, + }, "OtherFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -122,33 +121,33 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition1" - }, + }, "DeploymentRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole" - ], + ], "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "codedeploy.amazonaws.com" @@ -158,128 +157,127 @@ ] } } - }, + }, "MinimalFunctionDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "ServiceRoleArn": { "Ref": "DeploymentRole" - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "AllAtOnce" } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } } - }, + }, "ServerlessDeploymentApplication": { - "Type": "AWS::CodeDeploy::Application", + "Type": "AWS::CodeDeploy::Application", "Properties": { "ComputePlatform": "Lambda" } - }, + }, "MinimalFunctionVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "MinimalFunction" } } - }, + }, "OtherFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "OtherFunctionRole", + "OtherFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition1" - }, + }, "MinimalFunctionAliaslive": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "MinimalFunctionDeploymentGroup" } } - }, + }, "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "MinimalFunctionVersion640128d35d", + "MinimalFunctionVersion640128d35d", "Version" ] - }, + }, "FunctionName": { "Ref": "MinimalFunction" - }, + }, "Name": "live" } - }, + }, "OtherFunctionAliaslive": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "OtherFunctionDeploymentGroup" } } - }, - "Condition": "Condition1", + }, + "Condition": "Condition1", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "OtherFunctionVersion640128d35d", + "OtherFunctionVersion640128d35d", "Version" ] - }, + }, "FunctionName": { "Ref": "OtherFunction" - }, + }, "Name": "live" } } } -} \ No newline at end of file +} diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_preference_condition.json b/tests/translator/output/aws-us-gov/function_with_deployment_preference_condition.json index 0a8936b3e..385a1f49f 100644 --- a/tests/translator/output/aws-us-gov/function_with_deployment_preference_condition.json +++ b/tests/translator/output/aws-us-gov/function_with_deployment_preference_condition.json @@ -4,81 +4,81 @@ "Fn::Equals": [ { "Ref": "EnableAliasProvisionedConcurrency" - }, + }, true ] - }, + }, "FunctionCondition": { "Fn::Equals": [ - true, + true, true ] } - }, + }, "Parameters": { "EnableAliasProvisionedConcurrency": { - "Default": true, - "Type": "String", + "Default": true, + "Type": "String", "AllowedValues": [ - true, + true, false ] - }, + }, "FnName": { "Type": "String" - }, + }, "ProvisionedConcurrency": { - "Default": 10, + "Default": 10, "Type": "String" } - }, + }, "Resources": { "MinimalFunctionDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "ServiceRoleArn": { "Fn::GetAtt": [ - "CodeDeployServiceRole", + "CodeDeployServiceRole", "Arn" ] - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Linear10PercentEvery3Minutes" } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } - }, + }, "Condition": "FunctionCondition" - }, + }, "MinimalFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -86,57 +86,57 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "FunctionCondition" - }, + }, "MinimalFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MinimalFunctionRole", + "MinimalFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "FunctionCondition" - }, + }, "CodeDeployServiceRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ "arn:aws-us-gov:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" - ], + ], "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "codedeploy.amazonaws.com" @@ -145,65 +145,64 @@ } ] } - }, + }, "Condition": "FunctionCondition" - }, + }, "ServerlessDeploymentApplication": { - "Type": "AWS::CodeDeploy::Application", + "Type": "AWS::CodeDeploy::Application", "Properties": { "ComputePlatform": "Lambda" - }, + }, "Condition": "FunctionCondition" - }, + }, "MinimalFunctionVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "MinimalFunction" } - }, + }, "Condition": "FunctionCondition" - }, + }, "MinimalFunctionAliaslive": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "MinimalFunctionDeploymentGroup" } } - }, - "Condition": "FunctionCondition", + }, + "Condition": "FunctionCondition", "Properties": { - "Name": "live", + "Name": "live", "FunctionVersion": { "Fn::GetAtt": [ - "MinimalFunctionVersion640128d35d", + "MinimalFunctionVersion640128d35d", "Version" ] - }, + }, "ProvisionedConcurrencyConfig": { "Fn::If": [ - "AliasProvisionedConcurrencyEnabled", + "AliasProvisionedConcurrencyEnabled", { "ProvisionedConcurrentExecutions": { "Ref": "ProvisionedConcurrency" } - }, + }, { "Ref": "AWS::NoValue" } ] - }, + }, "FunctionName": { "Ref": "MinimalFunction" } } } } -} \ No newline at end of file +} diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions.json b/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions.json index 4c82df4aa..d534f9d6f 100644 --- a/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions.json +++ b/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions.json @@ -4,181 +4,180 @@ "Fn::Or": [ { "Condition": "Condition2" - }, + }, { "Condition": "Condition1" } ] - }, + }, "Condition3": { "Fn::Equals": [ - true, + true, false ] - }, + }, "Condition2": { "Fn::Equals": [ - true, + true, false ] - }, + }, "Condition1": { "Fn::Equals": [ - true, + true, true ] } - }, + }, "Parameters": { "MyFalseParameter": { - "Default": false, + "Default": false, "Type": "String" } - }, + }, "Resources": { "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole", + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MinimalFunctionRole", + "MinimalFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition1" - }, + }, "MyCloudWatchAlarm": { - "Type": "AWS::CloudWatch::Alarm", + "Type": "AWS::CloudWatch::Alarm", "Properties": { - "EvaluationPeriods": 1, - "Namespace": "AWS/EC2", - "Period": 300, - "ComparisonOperator": "GreaterThanThreshold", - "Threshold": 10, + "EvaluationPeriods": 1, + "Namespace": "AWS/EC2", + "Period": 300, + "ComparisonOperator": "GreaterThanThreshold", + "Threshold": 10, "MetricName": "MyMetric" } - }, + }, "MinimalFunctionWithMinimalDeploymentPreference": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MinimalFunctionWithMinimalDeploymentPreferenceRole", + "MinimalFunctionWithMinimalDeploymentPreferenceRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "ServiceRoleArn": { "Fn::GetAtt": [ - "CodeDeployServiceRole", + "CodeDeployServiceRole", "Arn" ] - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Linear10PercentEvery2Minutes" } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } - }, + }, "Condition": "Condition1" - }, + }, "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" } - }, + }, "Condition": "Condition2" - }, + }, "CodeDeployServiceRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ "arn:aws-us-gov:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" - ], + ], "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "codedeploy.amazonaws.com" @@ -187,87 +186,87 @@ } ] } - }, + }, "Condition": "ServerlessCodeDeployCondition" - }, + }, "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "ServiceRoleArn": { "Fn::GetAtt": [ - "CodeDeployServiceRole", + "CodeDeployServiceRole", "Arn" ] - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Canary10Percent5Minutes" } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsAliaslivewithdeploymentwithhooksandalarms": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "BeforeAllowTrafficHook": { "Ref": "MySanityTestFunction" - }, + }, "DeploymentGroupName": { "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup" - }, + }, "AfterAllowTrafficHook": { "Ref": "MyValidationTestFunction" } } - }, - "Condition": "Condition2", + }, + "Condition": "Condition2", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d", + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d", "Version" ] - }, + }, "FunctionName": { "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" - }, + }, "Name": "livewithdeploymentwithhooksandalarms" } - }, + }, "MyValidationTestFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -275,29 +274,29 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] } - }, + }, "MinimalFunctionWithMinimalDeploymentPreferenceRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -305,30 +304,30 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -336,30 +335,30 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition1" - }, + }, "MySanityTestFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -367,41 +366,41 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition3" - }, + }, "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Linear10PercentEvery2Minutes" } ] - }, + }, "AlarmConfiguration": { "Alarms": [ { @@ -409,44 +408,43 @@ "Ref": "MyCloudWatchAlarm" } } - ], + ], "Enabled": true - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" - }, + }, "ServiceRoleArn": { "Fn::GetAtt": [ - "CodeDeployServiceRole", + "CodeDeployServiceRole", "Arn" ] } - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "MinimalFunctionWithMinimalDeploymentPreference" } - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -454,135 +452,134 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionWithMinimalDeploymentPreferenceAliaslivewithdeployment": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup" } } - }, - "Condition": "Condition2", + }, + "Condition": "Condition2", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d", + "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d", "Version" ] - }, + }, "FunctionName": { "Ref": "MinimalFunctionWithMinimalDeploymentPreference" - }, + }, "Name": "livewithdeployment" } - }, + }, "MySanityTestFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "my-bucket", + "S3Bucket": "my-bucket", "S3Key": "mySanityTestFunction.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MySanityTestFunctionRole", + "MySanityTestFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition3" - }, + }, "ServerlessDeploymentApplication": { - "Type": "AWS::CodeDeploy::Application", + "Type": "AWS::CodeDeploy::Application", "Properties": { "ComputePlatform": "Lambda" - }, + }, "Condition": "ServerlessCodeDeployCondition" - }, + }, "MinimalFunctionVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "MinimalFunction" } - }, + }, "Condition": "Condition1" - }, + }, "MinimalFunctionAliaslive": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "MinimalFunctionDeploymentGroup" } } - }, - "Condition": "Condition1", + }, + "Condition": "Condition1", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "MinimalFunctionVersion640128d35d", + "MinimalFunctionVersion640128d35d", "Version" ] - }, + }, "FunctionName": { "Ref": "MinimalFunction" - }, + }, "Name": "live" } - }, + }, "MyValidationTestFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "my-bucket", + "S3Bucket": "my-bucket", "S3Key": "myValidationTestFunction.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MyValidationTestFunctionRole", + "MyValidationTestFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] } } } -} \ No newline at end of file +} diff --git a/tests/translator/output/function_with_deployment_no_service_role.json b/tests/translator/output/function_with_deployment_no_service_role.json index 5040a0ed4..1a6c407f6 100644 --- a/tests/translator/output/function_with_deployment_no_service_role.json +++ b/tests/translator/output/function_with_deployment_no_service_role.json @@ -2,78 +2,78 @@ "Conditions": { "Condition1": { "Fn::Equals": [ - true, + true, true ] } - }, + }, "Resources": { "OtherFunctionDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "ServiceRoleArn": { "Ref": "DeploymentRole" - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "AllAtOnce" } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } - }, + }, "Condition": "Condition1" - }, + }, "MinimalFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MinimalFunctionRole", + "MinimalFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] } - }, + }, "MinimalFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -81,40 +81,39 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] } - }, + }, "OtherFunctionVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "OtherFunction" } - }, + }, "Condition": "Condition1" - }, + }, "OtherFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -122,33 +121,33 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition1" - }, + }, "DeploymentRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole" - ], + ], "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "codedeploy.amazonaws.com" @@ -158,128 +157,127 @@ ] } } - }, + }, "MinimalFunctionDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "ServiceRoleArn": { "Ref": "DeploymentRole" - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "AllAtOnce" } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } } - }, + }, "ServerlessDeploymentApplication": { - "Type": "AWS::CodeDeploy::Application", + "Type": "AWS::CodeDeploy::Application", "Properties": { "ComputePlatform": "Lambda" } - }, + }, "MinimalFunctionVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "MinimalFunction" } } - }, + }, "OtherFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "OtherFunctionRole", + "OtherFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition1" - }, + }, "MinimalFunctionAliaslive": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "MinimalFunctionDeploymentGroup" } } - }, + }, "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "MinimalFunctionVersion640128d35d", + "MinimalFunctionVersion640128d35d", "Version" ] - }, + }, "FunctionName": { "Ref": "MinimalFunction" - }, + }, "Name": "live" } - }, + }, "OtherFunctionAliaslive": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "OtherFunctionDeploymentGroup" } } - }, - "Condition": "Condition1", + }, + "Condition": "Condition1", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "OtherFunctionVersion640128d35d", + "OtherFunctionVersion640128d35d", "Version" ] - }, + }, "FunctionName": { "Ref": "OtherFunction" - }, + }, "Name": "live" } } } -} \ No newline at end of file +} diff --git a/tests/translator/output/function_with_deployment_preference_condition.json b/tests/translator/output/function_with_deployment_preference_condition.json index 5c2b65f26..07936044d 100644 --- a/tests/translator/output/function_with_deployment_preference_condition.json +++ b/tests/translator/output/function_with_deployment_preference_condition.json @@ -4,81 +4,81 @@ "Fn::Equals": [ { "Ref": "EnableAliasProvisionedConcurrency" - }, + }, true ] - }, + }, "FunctionCondition": { "Fn::Equals": [ - true, + true, true ] } - }, + }, "Parameters": { "EnableAliasProvisionedConcurrency": { - "Default": true, - "Type": "String", + "Default": true, + "Type": "String", "AllowedValues": [ - true, + true, false ] - }, + }, "FnName": { "Type": "String" - }, + }, "ProvisionedConcurrency": { - "Default": 10, + "Default": 10, "Type": "String" } - }, + }, "Resources": { "MinimalFunctionDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "ServiceRoleArn": { "Fn::GetAtt": [ - "CodeDeployServiceRole", + "CodeDeployServiceRole", "Arn" ] - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Linear10PercentEvery3Minutes" } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } - }, + }, "Condition": "FunctionCondition" - }, + }, "MinimalFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -86,57 +86,57 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "FunctionCondition" - }, + }, "MinimalFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MinimalFunctionRole", + "MinimalFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "FunctionCondition" - }, + }, "CodeDeployServiceRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" - ], + ], "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "codedeploy.amazonaws.com" @@ -145,65 +145,64 @@ } ] } - }, + }, "Condition": "FunctionCondition" - }, + }, "ServerlessDeploymentApplication": { - "Type": "AWS::CodeDeploy::Application", + "Type": "AWS::CodeDeploy::Application", "Properties": { "ComputePlatform": "Lambda" - }, + }, "Condition": "FunctionCondition" - }, + }, "MinimalFunctionVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "MinimalFunction" } - }, + }, "Condition": "FunctionCondition" - }, + }, "MinimalFunctionAliaslive": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "MinimalFunctionDeploymentGroup" } } - }, - "Condition": "FunctionCondition", + }, + "Condition": "FunctionCondition", "Properties": { - "Name": "live", + "Name": "live", "FunctionVersion": { "Fn::GetAtt": [ - "MinimalFunctionVersion640128d35d", + "MinimalFunctionVersion640128d35d", "Version" ] - }, + }, "ProvisionedConcurrencyConfig": { "Fn::If": [ - "AliasProvisionedConcurrencyEnabled", + "AliasProvisionedConcurrencyEnabled", { "ProvisionedConcurrentExecutions": { "Ref": "ProvisionedConcurrency" } - }, + }, { "Ref": "AWS::NoValue" } ] - }, + }, "FunctionName": { "Ref": "MinimalFunction" } } } } -} \ No newline at end of file +} diff --git a/tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions.json b/tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions.json index a115ee311..f908e21be 100644 --- a/tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions.json +++ b/tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions.json @@ -4,181 +4,180 @@ "Fn::Or": [ { "Condition": "Condition2" - }, + }, { "Condition": "Condition1" } ] - }, + }, "Condition3": { "Fn::Equals": [ - true, + true, false ] - }, + }, "Condition2": { "Fn::Equals": [ - true, + true, false ] - }, + }, "Condition1": { "Fn::Equals": [ - true, + true, true ] } - }, + }, "Parameters": { "MyFalseParameter": { - "Default": false, + "Default": false, "Type": "String" } - }, + }, "Resources": { "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole", + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MinimalFunctionRole", + "MinimalFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition1" - }, + }, "MyCloudWatchAlarm": { - "Type": "AWS::CloudWatch::Alarm", + "Type": "AWS::CloudWatch::Alarm", "Properties": { - "EvaluationPeriods": 1, - "Namespace": "AWS/EC2", - "Period": 300, - "ComparisonOperator": "GreaterThanThreshold", - "Threshold": 10, + "EvaluationPeriods": 1, + "Namespace": "AWS/EC2", + "Period": 300, + "ComparisonOperator": "GreaterThanThreshold", + "Threshold": 10, "MetricName": "MyMetric" } - }, + }, "MinimalFunctionWithMinimalDeploymentPreference": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "sam-demo-bucket", + "S3Bucket": "sam-demo-bucket", "S3Key": "hello.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MinimalFunctionWithMinimalDeploymentPreferenceRole", + "MinimalFunctionWithMinimalDeploymentPreferenceRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "ServiceRoleArn": { "Fn::GetAtt": [ - "CodeDeployServiceRole", + "CodeDeployServiceRole", "Arn" ] - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Linear10PercentEvery2Minutes" } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } - }, + }, "Condition": "Condition1" - }, + }, "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" } - }, + }, "Condition": "Condition2" - }, + }, "CodeDeployServiceRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" - ], + ], "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "codedeploy.amazonaws.com" @@ -187,87 +186,87 @@ } ] } - }, + }, "Condition": "ServerlessCodeDeployCondition" - }, + }, "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "ServiceRoleArn": { "Fn::GetAtt": [ - "CodeDeployServiceRole", + "CodeDeployServiceRole", "Arn" ] - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Canary10Percent5Minutes" } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsAliaslivewithdeploymentwithhooksandalarms": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "BeforeAllowTrafficHook": { "Ref": "MySanityTestFunction" - }, + }, "DeploymentGroupName": { "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup" - }, + }, "AfterAllowTrafficHook": { "Ref": "MyValidationTestFunction" } } - }, - "Condition": "Condition2", + }, + "Condition": "Condition2", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d", + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d", "Version" ] - }, + }, "FunctionName": { "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" - }, + }, "Name": "livewithdeploymentwithhooksandalarms" } - }, + }, "MyValidationTestFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -275,29 +274,29 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] } - }, + }, "MinimalFunctionWithMinimalDeploymentPreferenceRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -305,30 +304,30 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -336,30 +335,30 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition1" - }, + }, "MySanityTestFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -367,41 +366,41 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition3" - }, + }, "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup": { - "Type": "AWS::CodeDeploy::DeploymentGroup", + "Type": "AWS::CodeDeploy::DeploymentGroup", "Properties": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "AutoRollbackConfiguration": { - "Enabled": true, + "Enabled": true, "Events": [ - "DEPLOYMENT_FAILURE", - "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", "DEPLOYMENT_STOP_ON_REQUEST" ] - }, + }, "DeploymentConfigName": { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Linear10PercentEvery2Minutes" } ] - }, + }, "AlarmConfiguration": { "Alarms": [ { @@ -409,44 +408,43 @@ "Ref": "MyCloudWatchAlarm" } } - ], + ], "Enabled": true - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" - }, + }, "ServiceRoleArn": { "Fn::GetAtt": [ - "CodeDeployServiceRole", + "CodeDeployServiceRole", "Arn" ] } - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "MinimalFunctionWithMinimalDeploymentPreference" } - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -454,135 +452,134 @@ } } ] - }, + }, "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition2" - }, + }, "MinimalFunctionWithMinimalDeploymentPreferenceAliaslivewithdeployment": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup" } } - }, - "Condition": "Condition2", + }, + "Condition": "Condition2", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d", + "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d", "Version" ] - }, + }, "FunctionName": { "Ref": "MinimalFunctionWithMinimalDeploymentPreference" - }, + }, "Name": "livewithdeployment" } - }, + }, "MySanityTestFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "my-bucket", + "S3Bucket": "my-bucket", "S3Key": "mySanityTestFunction.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MySanityTestFunctionRole", + "MySanityTestFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] - }, + }, "Condition": "Condition3" - }, + }, "ServerlessDeploymentApplication": { - "Type": "AWS::CodeDeploy::Application", + "Type": "AWS::CodeDeploy::Application", "Properties": { "ComputePlatform": "Lambda" - }, + }, "Condition": "ServerlessCodeDeployCondition" - }, + }, "MinimalFunctionVersion640128d35d": { - "UpdateReplacePolicy": "Delete", - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { "FunctionName": { "Ref": "MinimalFunction" } - }, + }, "Condition": "Condition1" - }, + }, "MinimalFunctionAliaslive": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "MinimalFunctionDeploymentGroup" } } - }, - "Condition": "Condition1", + }, + "Condition": "Condition1", "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "MinimalFunctionVersion640128d35d", + "MinimalFunctionVersion640128d35d", "Version" ] - }, + }, "FunctionName": { "Ref": "MinimalFunction" - }, + }, "Name": "live" } - }, + }, "MyValidationTestFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "hello.handler", + "Handler": "hello.handler", "Code": { - "S3Bucket": "my-bucket", + "S3Bucket": "my-bucket", "S3Key": "myValidationTestFunction.zip" - }, + }, "Role": { "Fn::GetAtt": [ - "MyValidationTestFunctionRole", + "MyValidationTestFunctionRole", "Arn" ] - }, - "Runtime": "python2.7", + }, + "Runtime": "python2.7", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] } } } -} \ No newline at end of file +} From 2a56189f5541abbc26de4f43a9a765700c3290bc Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Wed, 4 May 2022 21:12:55 -0700 Subject: [PATCH 06/16] Use feature toggle to gate deployment preference condition fix --- .../deployment_preference_collection.py | 7 +++++-- samtranslator/translator/translator.py | 4 +++- tests/translator/test_function_resources.py | 6 +++--- tests/translator/test_translator.py | 19 ++++++++++++++++--- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/samtranslator/model/preferences/deployment_preference_collection.py b/samtranslator/model/preferences/deployment_preference_collection.py index bd5be65f9..6477daf37 100644 --- a/samtranslator/model/preferences/deployment_preference_collection.py +++ b/samtranslator/model/preferences/deployment_preference_collection.py @@ -43,12 +43,13 @@ class DeploymentPreferenceCollection(object): resources. """ - def __init__(self): + def __init__(self, use_condition_fix=False): """ This collection stores an internal dict of the deployment preferences for each function's deployment preference in the SAM Template. """ self._resource_preferences = {} + self._use_condition_fix = use_condition_fix def add(self, logical_id, deployment_preference_dict, condition=None): """ @@ -67,7 +68,9 @@ def add(self, logical_id, deployment_preference_dict, condition=None): ) self._resource_preferences[logical_id] = DeploymentPreference.from_dict( - logical_id, deployment_preference_dict, condition + logical_id, + deployment_preference_dict, + condition=condition if self._use_condition_fix else None, ) def get(self, logical_id): diff --git a/samtranslator/translator/translator.py b/samtranslator/translator/translator.py index bf1ca9cfb..df2c6f65a 100644 --- a/samtranslator/translator/translator.py +++ b/samtranslator/translator/translator.py @@ -119,7 +119,9 @@ def translate(self, sam_template, parameter_values, feature_toggle=None, passthr mappings_resolver = IntrinsicsResolver( template.get("Mappings", {}), {FindInMapAction.intrinsic_name: FindInMapAction()} ) - deployment_preference_collection = DeploymentPreferenceCollection() + deployment_preference_collection = DeploymentPreferenceCollection( + use_condition_fix=self.feature_toggle.is_enabled("deployment_preference_condition_fix") + ) supported_resource_refs = SupportedResourceReferences() shared_api_usage_plan = SharedApiUsagePlan() document_errors = [] diff --git a/tests/translator/test_function_resources.py b/tests/translator/test_function_resources.py index f2675ef7b..19ee42419 100644 --- a/tests/translator/test_function_resources.py +++ b/tests/translator/test_function_resources.py @@ -145,7 +145,7 @@ def test_sam_function_with_deployment_preference(self, get_resolved_alias_name_m deployment_preference_collection.update_policy.assert_called_once_with(self.sam_func.logical_id) deployment_preference_collection.add.assert_called_once_with( - self.sam_func.logical_id, deploy_preference_dict, None + self.sam_func.logical_id, deploy_preference_dict, {} ) aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type] @@ -224,7 +224,7 @@ def test_sam_function_with_disabled_deployment_preference_does_not_add_update_po resources = sam_func.to_cloudformation(**kwargs) - preference_collection.add.assert_called_once_with(sam_func.logical_id, deploy_preference_dict, None) + preference_collection.add.assert_called_once_with(sam_func.logical_id, deploy_preference_dict, {}) preference_collection.get.assert_called_once_with(sam_func.logical_id) self.intrinsics_resolver_mock.resolve_parameter_refs.assert_called_with(enabled) aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type] @@ -321,7 +321,7 @@ def test_sam_function_with_deployment_preference_intrinsic_ref_enabled_boolean_p deployment_preference_collection.update_policy.assert_called_once_with(self.sam_func.logical_id) deployment_preference_collection.add.assert_called_once_with( - self.sam_func.logical_id, deploy_preference_dict, None + self.sam_func.logical_id, deploy_preference_dict, {} ) self.intrinsics_resolver_mock.resolve_parameter_refs.assert_any_call(enabled) diff --git a/tests/translator/test_translator.py b/tests/translator/test_translator.py index 48955690e..a2d2331a1 100644 --- a/tests/translator/test_translator.py +++ b/tests/translator/test_translator.py @@ -151,7 +151,7 @@ def _read_expected_output(self, testcase, partition): expected_filepath = os.path.join(OUTPUT_FOLDER, partition_folder, testcase + ".json") return json.load(open(expected_filepath, "r")) - def _compare_transform(self, manifest, expected, partition, region): + def _compare_transform(self, manifest, expected, partition, region, feature_toggle=None): with patch("boto3.session.Session.region_name", region): parameter_values = get_template_parameter_values() mock_policy_loader = MagicMock() @@ -172,7 +172,7 @@ def _compare_transform(self, manifest, expected, partition, region): "AWSXRayDaemonWriteAccess" ] = "arn:{}:iam::aws:policy/AWSXRayDaemonWriteAccess".format(partition) - output_fragment = transform(manifest, parameter_values, mock_policy_loader) + output_fragment = transform(manifest, parameter_values, mock_policy_loader, feature_toggle=feature_toggle) print(json.dumps(output_fragment, indent=2)) @@ -367,6 +367,7 @@ class TestTranslatorEndToEnd(AbstractTestTranslator): "function_with_disabled_traffic_hook", "function_with_deployment_preference", "function_with_deployment_preference_condition", + "function_with_deployment_preference_condition_without_condition_fix", "function_with_deployment_preference_all_parameters", "function_with_deployment_preference_from_parameters", "function_with_deployment_preference_multiple_combinations", @@ -499,7 +500,19 @@ def test_transform_success(self, testcase, partition_with_region): manifest = self._read_input(testcase) expected = self._read_expected_output(testcase, partition) - self._compare_transform(manifest, expected, partition, region) + def is_enabled(feature_name): + if feature_name == "deployment_preference_condition_fix" and testcase in [ + "function_with_deployment_no_service_role", + "function_with_deployment_preference_condition", + "function_with_deployment_preference_multiple_combinations_conditions", + ]: + return True + return False + + feature_toggle_mock = Mock() + feature_toggle_mock.is_enabled.side_effect = is_enabled + + self._compare_transform(manifest, expected, partition, region, feature_toggle_mock) @parameterized.expand( itertools.product( From f14ef77e9c3dc9e2fad2bc787881ff4041045167 Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Wed, 4 May 2022 23:12:11 -0700 Subject: [PATCH 07/16] Add tests --- ...rence_condition_without_condition_fix.yaml | 30 +++ ...rence_condition_without_condition_fix.json | 205 ++++++++++++++++++ ...rence_condition_without_condition_fix.json | 205 ++++++++++++++++++ ...rence_condition_without_condition_fix.json | 205 ++++++++++++++++++ 4 files changed, 645 insertions(+) create mode 100644 tests/translator/input/function_with_deployment_preference_condition_without_condition_fix.yaml create mode 100644 tests/translator/output/aws-cn/function_with_deployment_preference_condition_without_condition_fix.json create mode 100644 tests/translator/output/aws-us-gov/function_with_deployment_preference_condition_without_condition_fix.json create mode 100644 tests/translator/output/function_with_deployment_preference_condition_without_condition_fix.json diff --git a/tests/translator/input/function_with_deployment_preference_condition_without_condition_fix.yaml b/tests/translator/input/function_with_deployment_preference_condition_without_condition_fix.yaml new file mode 100644 index 000000000..06e788bfe --- /dev/null +++ b/tests/translator/input/function_with_deployment_preference_condition_without_condition_fix.yaml @@ -0,0 +1,30 @@ +Parameters: + FnName: + Type: String + ProvisionedConcurrency: + Type: String + Default: 10 + EnableAliasProvisionedConcurrency: + Type: String + AllowedValues: + - true + - false + Default: true +Conditions: + AliasProvisionedConcurrencyEnabled: !Equals [!Ref EnableAliasProvisionedConcurrency, true] + FunctionCondition: !Equals [true, true] +Resources: + MinimalFunction: + Type: 'AWS::Serverless::Function' + Condition: FunctionCondition + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + AutoPublishAlias: live + DeploymentPreference: + Type: Linear10PercentEvery3Minutes + ProvisionedConcurrencyConfig: !If + - AliasProvisionedConcurrencyEnabled + - ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency + - !Ref 'AWS::NoValue' \ No newline at end of file diff --git a/tests/translator/output/aws-cn/function_with_deployment_preference_condition_without_condition_fix.json b/tests/translator/output/aws-cn/function_with_deployment_preference_condition_without_condition_fix.json new file mode 100644 index 000000000..5660e1f9f --- /dev/null +++ b/tests/translator/output/aws-cn/function_with_deployment_preference_condition_without_condition_fix.json @@ -0,0 +1,205 @@ +{ + "Conditions": { + "AliasProvisionedConcurrencyEnabled": { + "Fn::Equals": [ + { + "Ref": "EnableAliasProvisionedConcurrency" + }, + true + ] + }, + "FunctionCondition": { + "Fn::Equals": [ + true, + true + ] + } + }, + "Parameters": { + "EnableAliasProvisionedConcurrency": { + "Default": true, + "Type": "String", + "AllowedValues": [ + true, + false + ] + }, + "FnName": { + "Type": "String" + }, + "ProvisionedConcurrency": { + "Default": 10, + "Type": "String" + } + }, + "Resources": { + "MinimalFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + } + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "FunctionCondition" + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "FunctionCondition" + }, + "CodeDeployServiceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + } + } + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, + "MinimalFunctionVersion640128d35d": { + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunction" + } + }, + "Condition": "FunctionCondition" + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Condition": "FunctionCondition", + "Properties": { + "Name": "live", + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + }, + "FunctionName": { + "Ref": "MinimalFunction" + } + } + } + } + } \ No newline at end of file diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_preference_condition_without_condition_fix.json b/tests/translator/output/aws-us-gov/function_with_deployment_preference_condition_without_condition_fix.json new file mode 100644 index 000000000..d69bc3b59 --- /dev/null +++ b/tests/translator/output/aws-us-gov/function_with_deployment_preference_condition_without_condition_fix.json @@ -0,0 +1,205 @@ +{ + "Conditions": { + "AliasProvisionedConcurrencyEnabled": { + "Fn::Equals": [ + { + "Ref": "EnableAliasProvisionedConcurrency" + }, + true + ] + }, + "FunctionCondition": { + "Fn::Equals": [ + true, + true + ] + } + }, + "Parameters": { + "EnableAliasProvisionedConcurrency": { + "Default": true, + "Type": "String", + "AllowedValues": [ + true, + false + ] + }, + "FnName": { + "Type": "String" + }, + "ProvisionedConcurrency": { + "Default": 10, + "Type": "String" + } + }, + "Resources": { + "MinimalFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + } + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "FunctionCondition" + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "FunctionCondition" + }, + "CodeDeployServiceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + } + } + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, + "MinimalFunctionVersion640128d35d": { + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunction" + } + }, + "Condition": "FunctionCondition" + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Condition": "FunctionCondition", + "Properties": { + "Name": "live", + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + }, + "FunctionName": { + "Ref": "MinimalFunction" + } + } + } + } + } \ No newline at end of file diff --git a/tests/translator/output/function_with_deployment_preference_condition_without_condition_fix.json b/tests/translator/output/function_with_deployment_preference_condition_without_condition_fix.json new file mode 100644 index 000000000..643c86b29 --- /dev/null +++ b/tests/translator/output/function_with_deployment_preference_condition_without_condition_fix.json @@ -0,0 +1,205 @@ +{ + "Conditions": { + "AliasProvisionedConcurrencyEnabled": { + "Fn::Equals": [ + { + "Ref": "EnableAliasProvisionedConcurrency" + }, + true + ] + }, + "FunctionCondition": { + "Fn::Equals": [ + true, + true + ] + } + }, + "Parameters": { + "EnableAliasProvisionedConcurrency": { + "Default": true, + "Type": "String", + "AllowedValues": [ + true, + false + ] + }, + "FnName": { + "Type": "String" + }, + "ProvisionedConcurrency": { + "Default": 10, + "Type": "String" + } + }, + "Resources": { + "MinimalFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + } + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "FunctionCondition" + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "FunctionCondition" + }, + "CodeDeployServiceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + } + } + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, + "MinimalFunctionVersion640128d35d": { + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunction" + } + }, + "Condition": "FunctionCondition" + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Condition": "FunctionCondition", + "Properties": { + "Name": "live", + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + }, + "FunctionName": { + "Ref": "MinimalFunction" + } + } + } + } + } \ No newline at end of file From cdcde399b7cea75c2bc6df7337bb75bc3fbae664 Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Fri, 6 May 2022 16:33:38 -0700 Subject: [PATCH 08/16] Revert using feature toggle --- .../deployment_preference_collection.py | 7 ++----- samtranslator/translator/translator.py | 4 +--- tests/translator/test_function_resources.py | 6 +++--- tests/translator/test_translator.py | 19 +++---------------- 4 files changed, 9 insertions(+), 27 deletions(-) diff --git a/samtranslator/model/preferences/deployment_preference_collection.py b/samtranslator/model/preferences/deployment_preference_collection.py index 6477daf37..bd5be65f9 100644 --- a/samtranslator/model/preferences/deployment_preference_collection.py +++ b/samtranslator/model/preferences/deployment_preference_collection.py @@ -43,13 +43,12 @@ class DeploymentPreferenceCollection(object): resources. """ - def __init__(self, use_condition_fix=False): + def __init__(self): """ This collection stores an internal dict of the deployment preferences for each function's deployment preference in the SAM Template. """ self._resource_preferences = {} - self._use_condition_fix = use_condition_fix def add(self, logical_id, deployment_preference_dict, condition=None): """ @@ -68,9 +67,7 @@ def add(self, logical_id, deployment_preference_dict, condition=None): ) self._resource_preferences[logical_id] = DeploymentPreference.from_dict( - logical_id, - deployment_preference_dict, - condition=condition if self._use_condition_fix else None, + logical_id, deployment_preference_dict, condition ) def get(self, logical_id): diff --git a/samtranslator/translator/translator.py b/samtranslator/translator/translator.py index df2c6f65a..bf1ca9cfb 100644 --- a/samtranslator/translator/translator.py +++ b/samtranslator/translator/translator.py @@ -119,9 +119,7 @@ def translate(self, sam_template, parameter_values, feature_toggle=None, passthr mappings_resolver = IntrinsicsResolver( template.get("Mappings", {}), {FindInMapAction.intrinsic_name: FindInMapAction()} ) - deployment_preference_collection = DeploymentPreferenceCollection( - use_condition_fix=self.feature_toggle.is_enabled("deployment_preference_condition_fix") - ) + deployment_preference_collection = DeploymentPreferenceCollection() supported_resource_refs = SupportedResourceReferences() shared_api_usage_plan = SharedApiUsagePlan() document_errors = [] diff --git a/tests/translator/test_function_resources.py b/tests/translator/test_function_resources.py index 19ee42419..f2675ef7b 100644 --- a/tests/translator/test_function_resources.py +++ b/tests/translator/test_function_resources.py @@ -145,7 +145,7 @@ def test_sam_function_with_deployment_preference(self, get_resolved_alias_name_m deployment_preference_collection.update_policy.assert_called_once_with(self.sam_func.logical_id) deployment_preference_collection.add.assert_called_once_with( - self.sam_func.logical_id, deploy_preference_dict, {} + self.sam_func.logical_id, deploy_preference_dict, None ) aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type] @@ -224,7 +224,7 @@ def test_sam_function_with_disabled_deployment_preference_does_not_add_update_po resources = sam_func.to_cloudformation(**kwargs) - preference_collection.add.assert_called_once_with(sam_func.logical_id, deploy_preference_dict, {}) + preference_collection.add.assert_called_once_with(sam_func.logical_id, deploy_preference_dict, None) preference_collection.get.assert_called_once_with(sam_func.logical_id) self.intrinsics_resolver_mock.resolve_parameter_refs.assert_called_with(enabled) aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type] @@ -321,7 +321,7 @@ def test_sam_function_with_deployment_preference_intrinsic_ref_enabled_boolean_p deployment_preference_collection.update_policy.assert_called_once_with(self.sam_func.logical_id) deployment_preference_collection.add.assert_called_once_with( - self.sam_func.logical_id, deploy_preference_dict, {} + self.sam_func.logical_id, deploy_preference_dict, None ) self.intrinsics_resolver_mock.resolve_parameter_refs.assert_any_call(enabled) diff --git a/tests/translator/test_translator.py b/tests/translator/test_translator.py index a2d2331a1..48955690e 100644 --- a/tests/translator/test_translator.py +++ b/tests/translator/test_translator.py @@ -151,7 +151,7 @@ def _read_expected_output(self, testcase, partition): expected_filepath = os.path.join(OUTPUT_FOLDER, partition_folder, testcase + ".json") return json.load(open(expected_filepath, "r")) - def _compare_transform(self, manifest, expected, partition, region, feature_toggle=None): + def _compare_transform(self, manifest, expected, partition, region): with patch("boto3.session.Session.region_name", region): parameter_values = get_template_parameter_values() mock_policy_loader = MagicMock() @@ -172,7 +172,7 @@ def _compare_transform(self, manifest, expected, partition, region, feature_togg "AWSXRayDaemonWriteAccess" ] = "arn:{}:iam::aws:policy/AWSXRayDaemonWriteAccess".format(partition) - output_fragment = transform(manifest, parameter_values, mock_policy_loader, feature_toggle=feature_toggle) + output_fragment = transform(manifest, parameter_values, mock_policy_loader) print(json.dumps(output_fragment, indent=2)) @@ -367,7 +367,6 @@ class TestTranslatorEndToEnd(AbstractTestTranslator): "function_with_disabled_traffic_hook", "function_with_deployment_preference", "function_with_deployment_preference_condition", - "function_with_deployment_preference_condition_without_condition_fix", "function_with_deployment_preference_all_parameters", "function_with_deployment_preference_from_parameters", "function_with_deployment_preference_multiple_combinations", @@ -500,19 +499,7 @@ def test_transform_success(self, testcase, partition_with_region): manifest = self._read_input(testcase) expected = self._read_expected_output(testcase, partition) - def is_enabled(feature_name): - if feature_name == "deployment_preference_condition_fix" and testcase in [ - "function_with_deployment_no_service_role", - "function_with_deployment_preference_condition", - "function_with_deployment_preference_multiple_combinations_conditions", - ]: - return True - return False - - feature_toggle_mock = Mock() - feature_toggle_mock.is_enabled.side_effect = is_enabled - - self._compare_transform(manifest, expected, partition, region, feature_toggle_mock) + self._compare_transform(manifest, expected, partition, region) @parameterized.expand( itertools.product( From 7b672e720170c2323bcc1c28671461c8b7a5c448 Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Sat, 7 May 2022 10:17:35 -0700 Subject: [PATCH 09/16] Add property to opt-in deployment preference condition passthrough --- samtranslator/model/sam_resources.py | 16 +- samtranslator/translator/translator.py | 1 + ...ment_no_service_role_with_passthrough.yaml | 48 ++ ..._no_service_role_without_passthrough.yaml} | 0 ...reference_condition_with_passthrough.yaml} | 1 + ...erence_condition_without_passthrough.yaml} | 0 ...binations_conditions_with_passthrough.yaml | 91 +++ ...tions_conditions_without_passthrough.yaml} | 12 +- ...ent_no_service_role_with_passthrough.json} | 0 ...t_no_service_role_without_passthrough.json | 282 +++++++++ ...reference_condition_with_passthrough.json} | 0 ...erence_condition_without_passthrough.json} | 0 ...inations_conditions_with_passthrough.json} | 0 ...ations_conditions_without_passthrough.json | 570 ++++++++++++++++++ ...ent_no_service_role_with_passthrough.json} | 0 ...t_no_service_role_without_passthrough.json | 282 +++++++++ ...reference_condition_with_passthrough.json} | 0 ...erence_condition_without_passthrough.json} | 0 ...inations_conditions_with_passthrough.json} | 0 ...ations_conditions_without_passthrough.json | 570 ++++++++++++++++++ ...ent_no_service_role_with_passthrough.json} | 0 ...t_no_service_role_without_passthrough.json | 282 +++++++++ ...reference_condition_with_passthrough.json} | 0 ...erence_condition_without_passthrough.json} | 0 ...inations_conditions_with_passthrough.json} | 0 ...ations_conditions_without_passthrough.json | 570 ++++++++++++++++++ tests/translator/test_translator.py | 9 +- tests/validator/test_validator.py | 2 +- 28 files changed, 2723 insertions(+), 13 deletions(-) create mode 100644 tests/translator/input/function_with_deployment_no_service_role_with_passthrough.yaml rename tests/translator/input/{function_with_deployment_no_service_role.yaml => function_with_deployment_no_service_role_without_passthrough.yaml} (100%) rename tests/translator/input/{function_with_deployment_preference_condition.yaml => function_with_deployment_preference_condition_with_passthrough.yaml} (96%) rename tests/translator/input/{function_with_deployment_preference_condition_without_condition_fix.yaml => function_with_deployment_preference_condition_without_passthrough.yaml} (100%) create mode 100644 tests/translator/input/function_with_deployment_preference_multiple_combinations_conditions_with_passthrough.yaml rename tests/translator/input/{function_with_deployment_preference_multiple_combinations_conditions.yaml => function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.yaml} (92%) rename tests/translator/output/aws-cn/{function_with_deployment_no_service_role.json => function_with_deployment_no_service_role_with_passthrough.json} (100%) create mode 100644 tests/translator/output/aws-cn/function_with_deployment_no_service_role_without_passthrough.json rename tests/translator/output/aws-cn/{function_with_deployment_preference_condition.json => function_with_deployment_preference_condition_with_passthrough.json} (100%) rename tests/translator/output/aws-cn/{function_with_deployment_preference_condition_without_condition_fix.json => function_with_deployment_preference_condition_without_passthrough.json} (100%) rename tests/translator/output/aws-cn/{function_with_deployment_preference_multiple_combinations_conditions.json => function_with_deployment_preference_multiple_combinations_conditions_with_passthrough.json} (100%) create mode 100644 tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.json rename tests/translator/output/aws-us-gov/{function_with_deployment_no_service_role.json => function_with_deployment_no_service_role_with_passthrough.json} (100%) create mode 100644 tests/translator/output/aws-us-gov/function_with_deployment_no_service_role_without_passthrough.json rename tests/translator/output/aws-us-gov/{function_with_deployment_preference_condition.json => function_with_deployment_preference_condition_with_passthrough.json} (100%) rename tests/translator/output/aws-us-gov/{function_with_deployment_preference_condition_without_condition_fix.json => function_with_deployment_preference_condition_without_passthrough.json} (100%) rename tests/translator/output/aws-us-gov/{function_with_deployment_preference_multiple_combinations_conditions.json => function_with_deployment_preference_multiple_combinations_conditions_with_passthrough.json} (100%) create mode 100644 tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.json rename tests/translator/output/{function_with_deployment_no_service_role.json => function_with_deployment_no_service_role_with_passthrough.json} (100%) create mode 100644 tests/translator/output/function_with_deployment_no_service_role_without_passthrough.json rename tests/translator/output/{function_with_deployment_preference_condition.json => function_with_deployment_preference_condition_with_passthrough.json} (100%) rename tests/translator/output/{function_with_deployment_preference_condition_without_condition_fix.json => function_with_deployment_preference_condition_without_passthrough.json} (100%) rename tests/translator/output/{function_with_deployment_preference_multiple_combinations_conditions.json => function_with_deployment_preference_multiple_combinations_conditions_with_passthrough.json} (100%) create mode 100644 tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.json diff --git a/samtranslator/model/sam_resources.py b/samtranslator/model/sam_resources.py index f37df2bfe..d2bc35f88 100644 --- a/samtranslator/model/sam_resources.py +++ b/samtranslator/model/sam_resources.py @@ -139,6 +139,7 @@ def to_cloudformation(self, **kwargs): intrinsics_resolver = kwargs["intrinsics_resolver"] mappings_resolver = kwargs.get("mappings_resolver", None) conditions = kwargs.get("conditions", {}) + feature_toggle = kwargs.get("feature_toggle") if self.DeadLetterQueue: self._validate_dlq() @@ -186,6 +187,7 @@ def to_cloudformation(self, **kwargs): intrinsics_resolver, mappings_resolver, self.get_passthrough_resource_attributes(), + feature_toggle, ) event_invoke_policies = [] if self.EventInvokeConfig: @@ -828,7 +830,13 @@ def _construct_alias(self, name, function, version): return alias def _validate_deployment_preference_and_add_update_policy( - self, deployment_preference_collection, lambda_alias, intrinsics_resolver, mappings_resolver, condition + self, + deployment_preference_collection, + lambda_alias, + intrinsics_resolver, + mappings_resolver, + passthrough_resource_attributes, + feature_toggle=None, ): if "Enabled" in self.DeploymentPreference: # resolve intrinsics and mappings for Type @@ -844,8 +852,10 @@ def _validate_deployment_preference_and_add_update_policy( preference_type = mappings_resolver.resolve_parameter_refs(preference_type) self.DeploymentPreference["Type"] = preference_type - if condition: - condition = condition.get("Condition") + should_passthrough_condition = self.DeploymentPreference.get("PassthroughCondition", False) or ( + feature_toggle and feature_toggle.is_enabled("deployment_preference_condition_fix") + ) + condition = passthrough_resource_attributes.get("Condition") if should_passthrough_condition else None if deployment_preference_collection is None: raise ValueError("deployment_preference_collection required for parsing the deployment preference") diff --git a/samtranslator/translator/translator.py b/samtranslator/translator/translator.py index bf1ca9cfb..9c99d86a7 100644 --- a/samtranslator/translator/translator.py +++ b/samtranslator/translator/translator.py @@ -142,6 +142,7 @@ def translate(self, sam_template, parameter_values, feature_toggle=None, passthr ) kwargs["redeploy_restapi_parameters"] = self.redeploy_restapi_parameters kwargs["shared_api_usage_plan"] = shared_api_usage_plan + kwargs["feature_toggle"] = self.feature_toggle translated = macro.to_cloudformation(**kwargs) supported_resource_refs = macro.get_resource_references(translated, supported_resource_refs) diff --git a/tests/translator/input/function_with_deployment_no_service_role_with_passthrough.yaml b/tests/translator/input/function_with_deployment_no_service_role_with_passthrough.yaml new file mode 100644 index 000000000..6a033caee --- /dev/null +++ b/tests/translator/input/function_with_deployment_no_service_role_with_passthrough.yaml @@ -0,0 +1,48 @@ +Conditions: + Condition1: + Fn::Equals: + - true + - true + +Globals: + Function: + AutoPublishAlias: live + DeploymentPreference: + Type: AllAtOnce + Role: !Ref DeploymentRole + PassthroughCondition: true + +Resources: + + MinimalFunction: + Type: 'AWS::Serverless::Function' + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + + OtherFunction: + Condition: Condition1 + Type: 'AWS::Serverless::Function' + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + + DeploymentRole: + Type: AWS::IAM::Role + Properties: + ManagedPolicyArns: + - arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole + AssumeRolePolicyDocument: + Version: '2012-10-17' + Statement: + - Action: + - sts:AssumeRole + Effect: Allow + Principal: + Service: + - codedeploy.amazonaws.com + + + diff --git a/tests/translator/input/function_with_deployment_no_service_role.yaml b/tests/translator/input/function_with_deployment_no_service_role_without_passthrough.yaml similarity index 100% rename from tests/translator/input/function_with_deployment_no_service_role.yaml rename to tests/translator/input/function_with_deployment_no_service_role_without_passthrough.yaml diff --git a/tests/translator/input/function_with_deployment_preference_condition.yaml b/tests/translator/input/function_with_deployment_preference_condition_with_passthrough.yaml similarity index 96% rename from tests/translator/input/function_with_deployment_preference_condition.yaml rename to tests/translator/input/function_with_deployment_preference_condition_with_passthrough.yaml index cbcd16ccc..b112e4d30 100644 --- a/tests/translator/input/function_with_deployment_preference_condition.yaml +++ b/tests/translator/input/function_with_deployment_preference_condition_with_passthrough.yaml @@ -24,6 +24,7 @@ Resources: AutoPublishAlias: live DeploymentPreference: Type: Linear10PercentEvery3Minutes + PassthroughCondition: true ProvisionedConcurrencyConfig: !If - AliasProvisionedConcurrencyEnabled - ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency diff --git a/tests/translator/input/function_with_deployment_preference_condition_without_condition_fix.yaml b/tests/translator/input/function_with_deployment_preference_condition_without_passthrough.yaml similarity index 100% rename from tests/translator/input/function_with_deployment_preference_condition_without_condition_fix.yaml rename to tests/translator/input/function_with_deployment_preference_condition_without_passthrough.yaml diff --git a/tests/translator/input/function_with_deployment_preference_multiple_combinations_conditions_with_passthrough.yaml b/tests/translator/input/function_with_deployment_preference_multiple_combinations_conditions_with_passthrough.yaml new file mode 100644 index 000000000..78445ec10 --- /dev/null +++ b/tests/translator/input/function_with_deployment_preference_multiple_combinations_conditions_with_passthrough.yaml @@ -0,0 +1,91 @@ +Conditions: + Condition1: + Fn::Equals: + - true + - true + Condition2: + Fn::Equals: + - true + - false + Condition3: + Fn::Equals: + - true + - false + +Parameters: + MyFalseParameter: + Type: String + Default: False + +Resources: + MinimalFunction: + Condition: Condition1 + Type: 'AWS::Serverless::Function' + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + AutoPublishAlias: live + DeploymentPreference: + Type: Linear10PercentEvery2Minutes + PassthroughCondition: true + + MinimalFunctionWithMinimalDeploymentPreference: + Type: 'AWS::Serverless::Function' + Condition: Condition2 + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + AutoPublishAlias: livewithdeployment + DeploymentPreference: + Type: Canary10Percent5Minutes + PassthroughCondition: true + + MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms: + Type: 'AWS::Serverless::Function' + Condition: Condition2 + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + AutoPublishAlias: livewithdeploymentwithhooksandalarms + DeploymentPreference: + Type: Linear10PercentEvery2Minutes + Hooks: + PreTraffic: !Ref MySanityTestFunction + PostTraffic: !Ref MyValidationTestFunction + Alarms: + - !Ref MyCloudWatchAlarm + PassthroughCondition: true + + MySanityTestFunction: + Type: 'AWS::Serverless::Function' + Condition: Condition3 + Properties: + Handler: hello.handler + Runtime: python2.7 + CodeUri: s3://my-bucket/mySanityTestFunction.zip + DeploymentPreference: + Enabled: False + PassthroughCondition: true + + MyValidationTestFunction: + Type: 'AWS::Serverless::Function' + Properties: + Handler: hello.handler + Runtime: python2.7 + CodeUri: s3://my-bucket/myValidationTestFunction.zip + DeploymentPreference: + Enabled: !Ref MyFalseParameter + PassthroughCondition: true + + MyCloudWatchAlarm: + Type: AWS::CloudWatch::Alarm + Properties: + ComparisonOperator: GreaterThanThreshold + EvaluationPeriods: 1 + MetricName: MyMetric + Namespace: AWS/EC2 + Period: 300 + Threshold: 10 diff --git a/tests/translator/input/function_with_deployment_preference_multiple_combinations_conditions.yaml b/tests/translator/input/function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.yaml similarity index 92% rename from tests/translator/input/function_with_deployment_preference_multiple_combinations_conditions.yaml rename to tests/translator/input/function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.yaml index 819e3e1e1..f9e994109 100644 --- a/tests/translator/input/function_with_deployment_preference_multiple_combinations_conditions.yaml +++ b/tests/translator/input/function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.yaml @@ -78,9 +78,9 @@ Resources: MyCloudWatchAlarm: Type: AWS::CloudWatch::Alarm Properties: - ComparisonOperator: GreaterThanThreshold - EvaluationPeriods: 1 - MetricName: MyMetric - Namespace: AWS/EC2 - Period: 300 - Threshold: 10 + ComparisonOperator: GreaterThanThreshold + EvaluationPeriods: 1 + MetricName: MyMetric + Namespace: AWS/EC2 + Period: 300 + Threshold: 10 diff --git a/tests/translator/output/aws-cn/function_with_deployment_no_service_role.json b/tests/translator/output/aws-cn/function_with_deployment_no_service_role_with_passthrough.json similarity index 100% rename from tests/translator/output/aws-cn/function_with_deployment_no_service_role.json rename to tests/translator/output/aws-cn/function_with_deployment_no_service_role_with_passthrough.json diff --git a/tests/translator/output/aws-cn/function_with_deployment_no_service_role_without_passthrough.json b/tests/translator/output/aws-cn/function_with_deployment_no_service_role_without_passthrough.json new file mode 100644 index 000000000..704201e99 --- /dev/null +++ b/tests/translator/output/aws-cn/function_with_deployment_no_service_role_without_passthrough.json @@ -0,0 +1,282 @@ +{ + "Conditions": { + "Condition1": { + "Fn::Equals": [ + true, + true + ] + } + }, + "Resources": { + "OtherFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Ref": "DeploymentRole" + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "AllAtOnce" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + } + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + }, + "OtherFunctionVersion640128d35d": { + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "OtherFunction" + } + }, + "Condition": "Condition1" + }, + "OtherFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition1" + }, + "DeploymentRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + } + } + }, + "MinimalFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Ref": "DeploymentRole" + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "AllAtOnce" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + } + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, + "MinimalFunctionVersion640128d35d": { + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunction" + } + } + }, + "OtherFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "OtherFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition1" + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "MinimalFunction" + }, + "Name": "live" + } + }, + "OtherFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "OtherFunctionDeploymentGroup" + } + } + }, + "Condition": "Condition1", + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "OtherFunctionVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "OtherFunction" + }, + "Name": "live" + } + } + } +} diff --git a/tests/translator/output/aws-cn/function_with_deployment_preference_condition.json b/tests/translator/output/aws-cn/function_with_deployment_preference_condition_with_passthrough.json similarity index 100% rename from tests/translator/output/aws-cn/function_with_deployment_preference_condition.json rename to tests/translator/output/aws-cn/function_with_deployment_preference_condition_with_passthrough.json diff --git a/tests/translator/output/aws-cn/function_with_deployment_preference_condition_without_condition_fix.json b/tests/translator/output/aws-cn/function_with_deployment_preference_condition_without_passthrough.json similarity index 100% rename from tests/translator/output/aws-cn/function_with_deployment_preference_condition_without_condition_fix.json rename to tests/translator/output/aws-cn/function_with_deployment_preference_condition_without_passthrough.json diff --git a/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions.json b/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions_with_passthrough.json similarity index 100% rename from tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions.json rename to tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions_with_passthrough.json diff --git a/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.json b/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.json new file mode 100644 index 000000000..6e2b17324 --- /dev/null +++ b/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.json @@ -0,0 +1,570 @@ +{ + "Conditions": { + "Condition1": { + "Fn::Equals": [ + true, + true + ] + }, + "Condition2": { + "Fn::Equals": [ + true, + false + ] + }, + "Condition3": { + "Fn::Equals": [ + true, + false + ] + } + }, + "Parameters": { + "MyFalseParameter": { + "Type": "String", + "Default": false + } + }, + "Resources": { + "MyCloudWatchAlarm": { + "Type": "AWS::CloudWatch::Alarm", + "Properties": { + "ComparisonOperator": "GreaterThanThreshold", + "EvaluationPeriods": 1, + "MetricName": "MyMetric", + "Namespace": "AWS/EC2", + "Period": 300, + "Threshold": 10 + } + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Condition": "Condition1", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MinimalFunctionVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "Condition1", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunction" + } + } + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "Condition": "Condition1", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Properties": { + "Name": "live", + "FunctionName": { + "Ref": "MinimalFunction" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + } + } + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Condition": "Condition1", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MinimalFunctionWithMinimalDeploymentPreference": { + "Type": "AWS::Lambda::Function", + "Condition": "Condition2", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionWithMinimalDeploymentPreferenceRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "Condition2", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreference" + } + } + }, + "MinimalFunctionWithMinimalDeploymentPreferenceAliaslivewithdeployment": { + "Type": "AWS::Lambda::Alias", + "Condition": "Condition2", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup" + } + } + }, + "Properties": { + "Name": "livewithdeployment", + "FunctionName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreference" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d", + "Version" + ] + } + } + }, + "MinimalFunctionWithMinimalDeploymentPreferenceRole": { + "Type": "AWS::IAM::Role", + "Condition": "Condition2", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms": { + "Type": "AWS::Lambda::Function", + "Condition": "Condition2", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "Condition2", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" + } + } + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsAliaslivewithdeploymentwithhooksandalarms": { + "Type": "AWS::Lambda::Alias", + "Condition": "Condition2", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup" + }, + "BeforeAllowTrafficHook": { + "Ref": "MySanityTestFunction" + }, + "AfterAllowTrafficHook": { + "Ref": "MyValidationTestFunction" + } + } + }, + "Properties": { + "Name": "livewithdeploymentwithhooksandalarms", + "FunctionName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d", + "Version" + ] + } + } + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole": { + "Type": "AWS::IAM::Role", + "Condition": "Condition2", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MySanityTestFunction": { + "Type": "AWS::Lambda::Function", + "Condition": "Condition3", + "Properties": { + "Code": { + "S3Bucket": "my-bucket", + "S3Key": "mySanityTestFunction.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MySanityTestFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MySanityTestFunctionRole": { + "Type": "AWS::IAM::Role", + "Condition": "Condition3", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MyValidationTestFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "S3Bucket": "my-bucket", + "S3Key": "myValidationTestFunction.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MyValidationTestFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MyValidationTestFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, + "CodeDeployServiceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + ] + } + }, + "MinimalFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery2Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + }, + "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Canary10Percent5Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "AlarmConfiguration": { + "Enabled": true, + "Alarms": [ + { + "Name": { + "Ref": "MyCloudWatchAlarm" + } + } + ] + }, + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery2Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + } + } +} diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_no_service_role.json b/tests/translator/output/aws-us-gov/function_with_deployment_no_service_role_with_passthrough.json similarity index 100% rename from tests/translator/output/aws-us-gov/function_with_deployment_no_service_role.json rename to tests/translator/output/aws-us-gov/function_with_deployment_no_service_role_with_passthrough.json diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_no_service_role_without_passthrough.json b/tests/translator/output/aws-us-gov/function_with_deployment_no_service_role_without_passthrough.json new file mode 100644 index 000000000..ad07002d0 --- /dev/null +++ b/tests/translator/output/aws-us-gov/function_with_deployment_no_service_role_without_passthrough.json @@ -0,0 +1,282 @@ +{ + "Conditions": { + "Condition1": { + "Fn::Equals": [ + true, + true + ] + } + }, + "Resources": { + "OtherFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Ref": "DeploymentRole" + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "AllAtOnce" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + } + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + }, + "OtherFunctionVersion640128d35d": { + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "OtherFunction" + } + }, + "Condition": "Condition1" + }, + "OtherFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition1" + }, + "DeploymentRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + } + } + }, + "MinimalFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Ref": "DeploymentRole" + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "AllAtOnce" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + } + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, + "MinimalFunctionVersion640128d35d": { + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunction" + } + } + }, + "OtherFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "OtherFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition1" + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "MinimalFunction" + }, + "Name": "live" + } + }, + "OtherFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "OtherFunctionDeploymentGroup" + } + } + }, + "Condition": "Condition1", + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "OtherFunctionVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "OtherFunction" + }, + "Name": "live" + } + } + } +} diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_preference_condition.json b/tests/translator/output/aws-us-gov/function_with_deployment_preference_condition_with_passthrough.json similarity index 100% rename from tests/translator/output/aws-us-gov/function_with_deployment_preference_condition.json rename to tests/translator/output/aws-us-gov/function_with_deployment_preference_condition_with_passthrough.json diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_preference_condition_without_condition_fix.json b/tests/translator/output/aws-us-gov/function_with_deployment_preference_condition_without_passthrough.json similarity index 100% rename from tests/translator/output/aws-us-gov/function_with_deployment_preference_condition_without_condition_fix.json rename to tests/translator/output/aws-us-gov/function_with_deployment_preference_condition_without_passthrough.json diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions.json b/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions_with_passthrough.json similarity index 100% rename from tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions.json rename to tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions_with_passthrough.json diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.json b/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.json new file mode 100644 index 000000000..aaf023072 --- /dev/null +++ b/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.json @@ -0,0 +1,570 @@ +{ + "Conditions": { + "Condition1": { + "Fn::Equals": [ + true, + true + ] + }, + "Condition2": { + "Fn::Equals": [ + true, + false + ] + }, + "Condition3": { + "Fn::Equals": [ + true, + false + ] + } + }, + "Parameters": { + "MyFalseParameter": { + "Type": "String", + "Default": false + } + }, + "Resources": { + "MyCloudWatchAlarm": { + "Type": "AWS::CloudWatch::Alarm", + "Properties": { + "ComparisonOperator": "GreaterThanThreshold", + "EvaluationPeriods": 1, + "MetricName": "MyMetric", + "Namespace": "AWS/EC2", + "Period": 300, + "Threshold": 10 + } + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Condition": "Condition1", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MinimalFunctionVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "Condition1", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunction" + } + } + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "Condition": "Condition1", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Properties": { + "Name": "live", + "FunctionName": { + "Ref": "MinimalFunction" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + } + } + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Condition": "Condition1", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MinimalFunctionWithMinimalDeploymentPreference": { + "Type": "AWS::Lambda::Function", + "Condition": "Condition2", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionWithMinimalDeploymentPreferenceRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "Condition2", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreference" + } + } + }, + "MinimalFunctionWithMinimalDeploymentPreferenceAliaslivewithdeployment": { + "Type": "AWS::Lambda::Alias", + "Condition": "Condition2", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup" + } + } + }, + "Properties": { + "Name": "livewithdeployment", + "FunctionName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreference" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d", + "Version" + ] + } + } + }, + "MinimalFunctionWithMinimalDeploymentPreferenceRole": { + "Type": "AWS::IAM::Role", + "Condition": "Condition2", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms": { + "Type": "AWS::Lambda::Function", + "Condition": "Condition2", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "Condition2", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" + } + } + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsAliaslivewithdeploymentwithhooksandalarms": { + "Type": "AWS::Lambda::Alias", + "Condition": "Condition2", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup" + }, + "BeforeAllowTrafficHook": { + "Ref": "MySanityTestFunction" + }, + "AfterAllowTrafficHook": { + "Ref": "MyValidationTestFunction" + } + } + }, + "Properties": { + "Name": "livewithdeploymentwithhooksandalarms", + "FunctionName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d", + "Version" + ] + } + } + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole": { + "Type": "AWS::IAM::Role", + "Condition": "Condition2", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MySanityTestFunction": { + "Type": "AWS::Lambda::Function", + "Condition": "Condition3", + "Properties": { + "Code": { + "S3Bucket": "my-bucket", + "S3Key": "mySanityTestFunction.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MySanityTestFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MySanityTestFunctionRole": { + "Type": "AWS::IAM::Role", + "Condition": "Condition3", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MyValidationTestFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "S3Bucket": "my-bucket", + "S3Key": "myValidationTestFunction.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MyValidationTestFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MyValidationTestFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, + "CodeDeployServiceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + ] + } + }, + "MinimalFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery2Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + }, + "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Canary10Percent5Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "AlarmConfiguration": { + "Enabled": true, + "Alarms": [ + { + "Name": { + "Ref": "MyCloudWatchAlarm" + } + } + ] + }, + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery2Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + } + } +} \ No newline at end of file diff --git a/tests/translator/output/function_with_deployment_no_service_role.json b/tests/translator/output/function_with_deployment_no_service_role_with_passthrough.json similarity index 100% rename from tests/translator/output/function_with_deployment_no_service_role.json rename to tests/translator/output/function_with_deployment_no_service_role_with_passthrough.json diff --git a/tests/translator/output/function_with_deployment_no_service_role_without_passthrough.json b/tests/translator/output/function_with_deployment_no_service_role_without_passthrough.json new file mode 100644 index 000000000..e3b229376 --- /dev/null +++ b/tests/translator/output/function_with_deployment_no_service_role_without_passthrough.json @@ -0,0 +1,282 @@ +{ + "Conditions": { + "Condition1": { + "Fn::Equals": [ + true, + true + ] + } + }, + "Resources": { + "OtherFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Ref": "DeploymentRole" + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "AllAtOnce" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + } + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + }, + "OtherFunctionVersion640128d35d": { + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "OtherFunction" + } + }, + "Condition": "Condition1" + }, + "OtherFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition1" + }, + "DeploymentRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + } + } + }, + "MinimalFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "ServiceRoleArn": { + "Ref": "DeploymentRole" + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "AllAtOnce" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + } + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, + "MinimalFunctionVersion640128d35d": { + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunction" + } + } + }, + "OtherFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "hello.handler", + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Role": { + "Fn::GetAtt": [ + "OtherFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + }, + "Condition": "Condition1" + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "MinimalFunction" + }, + "Name": "live" + } + }, + "OtherFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "OtherFunctionDeploymentGroup" + } + } + }, + "Condition": "Condition1", + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "OtherFunctionVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "OtherFunction" + }, + "Name": "live" + } + } + } +} diff --git a/tests/translator/output/function_with_deployment_preference_condition.json b/tests/translator/output/function_with_deployment_preference_condition_with_passthrough.json similarity index 100% rename from tests/translator/output/function_with_deployment_preference_condition.json rename to tests/translator/output/function_with_deployment_preference_condition_with_passthrough.json diff --git a/tests/translator/output/function_with_deployment_preference_condition_without_condition_fix.json b/tests/translator/output/function_with_deployment_preference_condition_without_passthrough.json similarity index 100% rename from tests/translator/output/function_with_deployment_preference_condition_without_condition_fix.json rename to tests/translator/output/function_with_deployment_preference_condition_without_passthrough.json diff --git a/tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions.json b/tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions_with_passthrough.json similarity index 100% rename from tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions.json rename to tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions_with_passthrough.json diff --git a/tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.json b/tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.json new file mode 100644 index 000000000..0519f0b44 --- /dev/null +++ b/tests/translator/output/function_with_deployment_preference_multiple_combinations_conditions_without_passthrough.json @@ -0,0 +1,570 @@ +{ + "Conditions": { + "Condition1": { + "Fn::Equals": [ + true, + true + ] + }, + "Condition2": { + "Fn::Equals": [ + true, + false + ] + }, + "Condition3": { + "Fn::Equals": [ + true, + false + ] + } + }, + "Parameters": { + "MyFalseParameter": { + "Type": "String", + "Default": false + } + }, + "Resources": { + "MyCloudWatchAlarm": { + "Type": "AWS::CloudWatch::Alarm", + "Properties": { + "ComparisonOperator": "GreaterThanThreshold", + "EvaluationPeriods": 1, + "MetricName": "MyMetric", + "Namespace": "AWS/EC2", + "Period": 300, + "Threshold": 10 + } + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Condition": "Condition1", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MinimalFunctionVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "Condition1", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunction" + } + } + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "Condition": "Condition1", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Properties": { + "Name": "live", + "FunctionName": { + "Ref": "MinimalFunction" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + } + } + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Condition": "Condition1", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MinimalFunctionWithMinimalDeploymentPreference": { + "Type": "AWS::Lambda::Function", + "Condition": "Condition2", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionWithMinimalDeploymentPreferenceRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "Condition2", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreference" + } + } + }, + "MinimalFunctionWithMinimalDeploymentPreferenceAliaslivewithdeployment": { + "Type": "AWS::Lambda::Alias", + "Condition": "Condition2", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup" + } + } + }, + "Properties": { + "Name": "livewithdeployment", + "FunctionName": { + "Ref": "MinimalFunctionWithMinimalDeploymentPreference" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionWithMinimalDeploymentPreferenceVersion640128d35d", + "Version" + ] + } + } + }, + "MinimalFunctionWithMinimalDeploymentPreferenceRole": { + "Type": "AWS::IAM::Role", + "Condition": "Condition2", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms": { + "Type": "AWS::Lambda::Function", + "Condition": "Condition2", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "Condition2", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" + } + } + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsAliaslivewithdeploymentwithhooksandalarms": { + "Type": "AWS::Lambda::Alias", + "Condition": "Condition2", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup" + }, + "BeforeAllowTrafficHook": { + "Ref": "MySanityTestFunction" + }, + "AfterAllowTrafficHook": { + "Ref": "MyValidationTestFunction" + } + } + }, + "Properties": { + "Name": "livewithdeploymentwithhooksandalarms", + "FunctionName": { + "Ref": "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarms" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsVersion640128d35d", + "Version" + ] + } + } + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsRole": { + "Type": "AWS::IAM::Role", + "Condition": "Condition2", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MySanityTestFunction": { + "Type": "AWS::Lambda::Function", + "Condition": "Condition3", + "Properties": { + "Code": { + "S3Bucket": "my-bucket", + "S3Key": "mySanityTestFunction.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MySanityTestFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MySanityTestFunctionRole": { + "Type": "AWS::IAM::Role", + "Condition": "Condition3", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MyValidationTestFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "S3Bucket": "my-bucket", + "S3Key": "myValidationTestFunction.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MyValidationTestFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "MyValidationTestFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, + "CodeDeployServiceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + ] + } + }, + "MinimalFunctionDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery2Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + }, + "MinimalFunctionWithMinimalDeploymentPreferenceDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Canary10Percent5Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + }, + "MinimalFunctionWithDeploymentPreferenceWithHooksAndAlarmsDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "AlarmConfiguration": { + "Enabled": true, + "Alarms": [ + { + "Name": { + "Ref": "MyCloudWatchAlarm" + } + } + ] + }, + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery2Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + } + } +} diff --git a/tests/translator/test_translator.py b/tests/translator/test_translator.py index 48955690e..81ec500e3 100644 --- a/tests/translator/test_translator.py +++ b/tests/translator/test_translator.py @@ -366,16 +366,19 @@ class TestTranslatorEndToEnd(AbstractTestTranslator): "function_with_disabled_deployment_preference", "function_with_disabled_traffic_hook", "function_with_deployment_preference", - "function_with_deployment_preference_condition", + "function_with_deployment_preference_condition_with_passthrough", + "function_with_deployment_preference_condition_without_passthrough", "function_with_deployment_preference_all_parameters", "function_with_deployment_preference_from_parameters", "function_with_deployment_preference_multiple_combinations", "function_with_deployment_preference_alarms_intrinsic_if", - "function_with_deployment_preference_multiple_combinations_conditions", + "function_with_deployment_preference_multiple_combinations_conditions_with_passthrough", + "function_with_deployment_preference_multiple_combinations_conditions_without_passthrough", "function_with_alias_and_event_sources", "function_with_resource_refs", "function_with_deployment_and_custom_role", - "function_with_deployment_no_service_role", + "function_with_deployment_no_service_role_with_passthrough", + "function_with_deployment_no_service_role_without_passthrough", "function_with_global_layers", "function_with_layers", "function_with_many_layers", diff --git a/tests/validator/test_validator.py b/tests/validator/test_validator.py index 6ac288d48..e35524df1 100644 --- a/tests/validator/test_validator.py +++ b/tests/validator/test_validator.py @@ -121,7 +121,7 @@ class TestValidatorTranslatorTemplates(TestValidatorBase): "function_with_alias_and_event_sources", "function_with_alias_intrinsics", "function_with_deployment_and_custom_role", - "function_with_deployment_no_service_role", + "function_with_deployment_no_service_role_with_passthrough", "function_with_deployment_preference", "function_with_deployment_preference_all_parameters", "function_with_deployment_preference_from_parameters", From 8585bf8c7703e9de54f347d66fc67491101d0f5b Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Sat, 7 May 2022 16:02:57 -0700 Subject: [PATCH 10/16] Add tests for PassthroughCondition --- tests/translator/test_function_resources.py | 97 +++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/tests/translator/test_function_resources.py b/tests/translator/test_function_resources.py index f2675ef7b..3009c33cf 100644 --- a/tests/translator/test_function_resources.py +++ b/tests/translator/test_function_resources.py @@ -399,6 +399,103 @@ def test_sam_function_with_deployment_preference_intrinsic_findinmap_enabled_dic sam_func.to_cloudformation(**kwargs) self.assertTrue(sam_func.DeploymentPreference["Enabled"]) + @patch("boto3.session.Session.region_name", "ap-southeast-1") + @patch.object(SamFunction, "_get_resolved_alias_name") + def test_sam_function_with_deployment_preference_passthrough_condition_through_property( + self, get_resolved_alias_name_mock + ): + deploy_preference_dict = {"Type": "LINEAR", "PassthroughCondition": True} + alias_name = "AliasName" + func = { + "Type": "AWS::Serverless::Function", + "Condition": "Condition1", + "Properties": { + "CodeUri": self.code_uri, + "Runtime": "nodejs12.x", + "Handler": "index.handler", + "AutoPublishAlias": alias_name, + "DeploymentPreference": deploy_preference_dict, + }, + } + + sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) + + kwargs = dict() + kwargs["managed_policy_map"] = {"a": "b"} + kwargs["event_resources"] = [] + kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock + kwargs["mappings_resolver"] = self.mappings_resolver_mock + deployment_preference_collection = self._make_deployment_preference_collection() + kwargs["deployment_preference_collection"] = deployment_preference_collection + get_resolved_alias_name_mock.return_value = alias_name + + self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = { + "S3Bucket": "bucket", + "S3Key": "key", + "S3ObjectVersion": "version", + } + resources = sam_func.to_cloudformation(**kwargs) + + deployment_preference_collection.update_policy.assert_called_once_with(self.sam_func.logical_id) + deployment_preference_collection.add.assert_called_once_with( + self.sam_func.logical_id, deploy_preference_dict, "Condition1" + ) + + aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type] + + self.assertTrue("UpdatePolicy" in list(aliases[0].values())[0]) + self.assertEqual(list(aliases[0].values())[0]["UpdatePolicy"], self.update_policy().to_dict()) + + @patch("boto3.session.Session.region_name", "ap-southeast-1") + @patch.object(SamFunction, "_get_resolved_alias_name") + def test_sam_function_with_deployment_preference_passthrough_condition_through_feature_flag( + self, get_resolved_alias_name_mock + ): + deploy_preference_dict = {"Type": "LINEAR", "PassthroughCondition": True} + alias_name = "AliasName" + func = { + "Type": "AWS::Serverless::Function", + "Condition": "Condition1", + "Properties": { + "CodeUri": self.code_uri, + "Runtime": "nodejs12.x", + "Handler": "index.handler", + "AutoPublishAlias": alias_name, + "DeploymentPreference": deploy_preference_dict, + }, + } + + sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) + + kwargs = dict() + kwargs["managed_policy_map"] = {"a": "b"} + kwargs["event_resources"] = [] + kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock + kwargs["mappings_resolver"] = self.mappings_resolver_mock + deployment_preference_collection = self._make_deployment_preference_collection() + kwargs["deployment_preference_collection"] = deployment_preference_collection + get_resolved_alias_name_mock.return_value = alias_name + feature_toggle_mock = Mock() + feature_toggle_mock.is_enabled.side_effect = lambda x: x == "deployment_preference_condition_fix" + kwargs["feature_toggle"] = feature_toggle_mock + + self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = { + "S3Bucket": "bucket", + "S3Key": "key", + "S3ObjectVersion": "version", + } + resources = sam_func.to_cloudformation(**kwargs) + + deployment_preference_collection.update_policy.assert_called_once_with(self.sam_func.logical_id) + deployment_preference_collection.add.assert_called_once_with( + self.sam_func.logical_id, deploy_preference_dict, "Condition1" + ) + + aliases = [r.to_dict() for r in resources if r.resource_type == LambdaAlias.resource_type] + + self.assertTrue("UpdatePolicy" in list(aliases[0].values())[0]) + self.assertEqual(list(aliases[0].values())[0]["UpdatePolicy"], self.update_policy().to_dict()) + @patch("samtranslator.translator.logical_id_generator.LogicalIdGenerator") def test_version_creation(self, LogicalIdGeneratorMock): generator_mock = LogicalIdGeneratorMock.return_value From c7cbf793d652364ee923e72830ae3111c89d377a Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Sun, 8 May 2022 21:02:23 -0700 Subject: [PATCH 11/16] Fix passthrough condition logic and add tests --- samtranslator/model/sam_resources.py | 15 ++++-- tests/translator/test_function_resources.py | 55 +++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/samtranslator/model/sam_resources.py b/samtranslator/model/sam_resources.py index d2bc35f88..7b9360e11 100644 --- a/samtranslator/model/sam_resources.py +++ b/samtranslator/model/sam_resources.py @@ -852,9 +852,18 @@ def _validate_deployment_preference_and_add_update_policy( preference_type = mappings_resolver.resolve_parameter_refs(preference_type) self.DeploymentPreference["Type"] = preference_type - should_passthrough_condition = self.DeploymentPreference.get("PassthroughCondition", False) or ( - feature_toggle and feature_toggle.is_enabled("deployment_preference_condition_fix") - ) + if "PassthroughCondition" in self.DeploymentPreference: + should_passthrough_condition = self.DeploymentPreference.get("PassthroughCondition", False) + elif feature_toggle: + should_passthrough_condition = feature_toggle.is_enabled("deployment_preference_condition_fix") + else: # default behaviour - no condition passthrough + should_passthrough_condition = False + + if not isinstance(should_passthrough_condition, bool): + raise InvalidResourceException( + self.logical_id, + "'DeploymentPreference.PassthroughCondition' must be a boolean value and does not support intrinsic.", + ) condition = passthrough_resource_attributes.get("Condition") if should_passthrough_condition else None if deployment_preference_collection is None: diff --git a/tests/translator/test_function_resources.py b/tests/translator/test_function_resources.py index 3009c33cf..a0602bec4 100644 --- a/tests/translator/test_function_resources.py +++ b/tests/translator/test_function_resources.py @@ -1,5 +1,7 @@ from unittest import TestCase from unittest.mock import patch, Mock +from parameterized import parameterized + import os from samtranslator.model.sam_resources import SamFunction from samtranslator.model.lambda_ import LambdaAlias, LambdaVersion, LambdaFunction @@ -496,6 +498,59 @@ def test_sam_function_with_deployment_preference_passthrough_condition_through_f self.assertTrue("UpdatePolicy" in list(aliases[0].values())[0]) self.assertEqual(list(aliases[0].values())[0]["UpdatePolicy"], self.update_policy().to_dict()) + @parameterized.expand( + [ + ({"Ref": "Param1"},), + ("my_string",), + ] + ) + @patch("boto3.session.Session.region_name", "ap-southeast-1") + @patch.object(SamFunction, "_get_resolved_alias_name") + def test_sam_function_with_deployment_preference_passthrough_condition_invalid_input( + self, invalid_passthrough_condition, get_resolved_alias_name_mock + ): + deploy_preference_dict = {"Type": "LINEAR", "PassthroughCondition": invalid_passthrough_condition} + alias_name = "AliasName" + func = { + "Type": "AWS::Serverless::Function", + "Condition": "Condition1", + "Properties": { + "CodeUri": self.code_uri, + "Runtime": "nodejs12.x", + "Handler": "index.handler", + "AutoPublishAlias": alias_name, + "DeploymentPreference": deploy_preference_dict, + }, + } + + sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) + + kwargs = dict() + kwargs["managed_policy_map"] = {"a": "b"} + kwargs["event_resources"] = [] + kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock + kwargs["mappings_resolver"] = self.mappings_resolver_mock + deployment_preference_collection = self._make_deployment_preference_collection() + kwargs["deployment_preference_collection"] = deployment_preference_collection + get_resolved_alias_name_mock.return_value = alias_name + feature_toggle_mock = Mock() + feature_toggle_mock.is_enabled.side_effect = lambda x: x == "deployment_preference_condition_fix" + kwargs["feature_toggle"] = feature_toggle_mock + + self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = { + "S3Bucket": "bucket", + "S3Key": "key", + "S3ObjectVersion": "version", + } + + with self.assertRaises(InvalidResourceException) as e: + sam_func.to_cloudformation(**kwargs) + + self.assertEqual( + e.exception.message, + "Resource with id [foo] is invalid. 'DeploymentPreference.PassthroughCondition' must be a boolean value and does not support intrinsic.", + ) + @patch("samtranslator.translator.logical_id_generator.LogicalIdGenerator") def test_version_creation(self, LogicalIdGeneratorMock): generator_mock = LogicalIdGeneratorMock.return_value From 8405853347aee1efa23e1cc214762fd17c0cb968 Mon Sep 17 00:00:00 2001 From: Wing Fung Lau <4760060+hawflau@users.noreply.github.com> Date: Tue, 10 May 2022 13:51:31 -0700 Subject: [PATCH 12/16] Update PassthroughCondition to support intrinsic --- .../preferences/deployment_preference.py | 6 +- samtranslator/model/sam_resources.py | 23 ++-- tests/translator/test_function_resources.py | 104 +++++++++--------- 3 files changed, 68 insertions(+), 65 deletions(-) diff --git a/samtranslator/model/preferences/deployment_preference.py b/samtranslator/model/preferences/deployment_preference.py index cca77feea..ec65a84b4 100644 --- a/samtranslator/model/preferences/deployment_preference.py +++ b/samtranslator/model/preferences/deployment_preference.py @@ -74,6 +74,10 @@ def from_dict(cls, logical_id, deployment_preference_dict, condition=None): alarms = deployment_preference_dict.get("Alarms", None) role = deployment_preference_dict.get("Role", None) trigger_configurations = deployment_preference_dict.get("TriggerConfigurations", None) + passthrough_condition = deployment_preference_dict.get("PassthroughCondition", False) + # FIXME: We should support not only true/false, but also yes/no, on/off? See https://yaml.org/type/bool.html + passthrough_condition = True if passthrough_condition in [True, "true", "True"] else False + return DeploymentPreference( deployment_type, pre_traffic_hook, @@ -82,5 +86,5 @@ def from_dict(cls, logical_id, deployment_preference_dict, condition=None): enabled, role, trigger_configurations, - condition, + condition if passthrough_condition else None, ) diff --git a/samtranslator/model/sam_resources.py b/samtranslator/model/sam_resources.py index 7b9360e11..49c304c63 100644 --- a/samtranslator/model/sam_resources.py +++ b/samtranslator/model/sam_resources.py @@ -839,7 +839,7 @@ def _validate_deployment_preference_and_add_update_policy( feature_toggle=None, ): if "Enabled" in self.DeploymentPreference: - # resolve intrinsics and mappings for Type + # resolve intrinsics and mappings for Enabled enabled = self.DeploymentPreference["Enabled"] enabled = intrinsics_resolver.resolve_parameter_refs(enabled) enabled = mappings_resolver.resolve_parameter_refs(enabled) @@ -853,18 +853,17 @@ def _validate_deployment_preference_and_add_update_policy( self.DeploymentPreference["Type"] = preference_type if "PassthroughCondition" in self.DeploymentPreference: - should_passthrough_condition = self.DeploymentPreference.get("PassthroughCondition", False) + # resolve intrinsics and mappings for PassthroughCondition + passthrough_condition = self.DeploymentPreference["PassthroughCondition"] + passthrough_condition = intrinsics_resolver.resolve_parameter_refs(passthrough_condition) + passthrough_condition = mappings_resolver.resolve_parameter_refs(passthrough_condition) + self.DeploymentPreference["PassthroughCondition"] = passthrough_condition elif feature_toggle: - should_passthrough_condition = feature_toggle.is_enabled("deployment_preference_condition_fix") - else: # default behaviour - no condition passthrough - should_passthrough_condition = False - - if not isinstance(should_passthrough_condition, bool): - raise InvalidResourceException( - self.logical_id, - "'DeploymentPreference.PassthroughCondition' must be a boolean value and does not support intrinsic.", + self.DeploymentPreference["PassthroughCondition"] = feature_toggle.is_enabled( + "deployment_preference_condition_fix" ) - condition = passthrough_resource_attributes.get("Condition") if should_passthrough_condition else None + else: + self.DeploymentPreference["PassthroughCondition"] = False if deployment_preference_collection is None: raise ValueError("deployment_preference_collection required for parsing the deployment preference") @@ -872,7 +871,7 @@ def _validate_deployment_preference_and_add_update_policy( deployment_preference_collection.add( self.logical_id, self.DeploymentPreference, - condition, + passthrough_resource_attributes.get("Condition"), ) if deployment_preference_collection.get(self.logical_id).enabled: diff --git a/tests/translator/test_function_resources.py b/tests/translator/test_function_resources.py index a0602bec4..adade5d90 100644 --- a/tests/translator/test_function_resources.py +++ b/tests/translator/test_function_resources.py @@ -498,58 +498,58 @@ def test_sam_function_with_deployment_preference_passthrough_condition_through_f self.assertTrue("UpdatePolicy" in list(aliases[0].values())[0]) self.assertEqual(list(aliases[0].values())[0]["UpdatePolicy"], self.update_policy().to_dict()) - @parameterized.expand( - [ - ({"Ref": "Param1"},), - ("my_string",), - ] - ) - @patch("boto3.session.Session.region_name", "ap-southeast-1") - @patch.object(SamFunction, "_get_resolved_alias_name") - def test_sam_function_with_deployment_preference_passthrough_condition_invalid_input( - self, invalid_passthrough_condition, get_resolved_alias_name_mock - ): - deploy_preference_dict = {"Type": "LINEAR", "PassthroughCondition": invalid_passthrough_condition} - alias_name = "AliasName" - func = { - "Type": "AWS::Serverless::Function", - "Condition": "Condition1", - "Properties": { - "CodeUri": self.code_uri, - "Runtime": "nodejs12.x", - "Handler": "index.handler", - "AutoPublishAlias": alias_name, - "DeploymentPreference": deploy_preference_dict, - }, - } - - sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) - - kwargs = dict() - kwargs["managed_policy_map"] = {"a": "b"} - kwargs["event_resources"] = [] - kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock - kwargs["mappings_resolver"] = self.mappings_resolver_mock - deployment_preference_collection = self._make_deployment_preference_collection() - kwargs["deployment_preference_collection"] = deployment_preference_collection - get_resolved_alias_name_mock.return_value = alias_name - feature_toggle_mock = Mock() - feature_toggle_mock.is_enabled.side_effect = lambda x: x == "deployment_preference_condition_fix" - kwargs["feature_toggle"] = feature_toggle_mock - - self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = { - "S3Bucket": "bucket", - "S3Key": "key", - "S3ObjectVersion": "version", - } - - with self.assertRaises(InvalidResourceException) as e: - sam_func.to_cloudformation(**kwargs) - - self.assertEqual( - e.exception.message, - "Resource with id [foo] is invalid. 'DeploymentPreference.PassthroughCondition' must be a boolean value and does not support intrinsic.", - ) + # @parameterized.expand( + # [ + # ({"Ref": "Param1"},), + # ("my_string",), + # ] + # ) + # @patch("boto3.session.Session.region_name", "ap-southeast-1") + # @patch.object(SamFunction, "_get_resolved_alias_name") + # def test_sam_function_with_deployment_preference_passthrough_condition_invalid_input( + # self, invalid_passthrough_condition, get_resolved_alias_name_mock + # ): + # deploy_preference_dict = {"Type": "LINEAR", "PassthroughCondition": invalid_passthrough_condition} + # alias_name = "AliasName" + # func = { + # "Type": "AWS::Serverless::Function", + # "Condition": "Condition1", + # "Properties": { + # "CodeUri": self.code_uri, + # "Runtime": "nodejs12.x", + # "Handler": "index.handler", + # "AutoPublishAlias": alias_name, + # "DeploymentPreference": deploy_preference_dict, + # }, + # } + + # sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) + + # kwargs = dict() + # kwargs["managed_policy_map"] = {"a": "b"} + # kwargs["event_resources"] = [] + # kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock + # kwargs["mappings_resolver"] = self.mappings_resolver_mock + # deployment_preference_collection = self._make_deployment_preference_collection() + # kwargs["deployment_preference_collection"] = deployment_preference_collection + # get_resolved_alias_name_mock.return_value = alias_name + # feature_toggle_mock = Mock() + # feature_toggle_mock.is_enabled.side_effect = lambda x: x == "deployment_preference_condition_fix" + # kwargs["feature_toggle"] = feature_toggle_mock + + # self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = { + # "S3Bucket": "bucket", + # "S3Key": "key", + # "S3ObjectVersion": "version", + # } + + # with self.assertRaises(InvalidResourceException) as e: + # sam_func.to_cloudformation(**kwargs) + + # self.assertEqual( + # e.exception.message, + # "Resource with id [foo] is invalid. 'DeploymentPreference.PassthroughCondition' must be a boolean value and does not support intrinsic.", + # ) @patch("samtranslator.translator.logical_id_generator.LogicalIdGenerator") def test_version_creation(self, LogicalIdGeneratorMock): From 82ffbd96a4c0f36ea0d0251abaef266db2a74f4b Mon Sep 17 00:00:00 2001 From: Ruperto Torres Date: Tue, 17 May 2022 09:41:28 -0700 Subject: [PATCH 13/16] intrinscs support + tests --- .../preferences/deployment_preference.py | 2 - samtranslator/model/sam_resources.py | 47 +- ...through_condition_with_invalid_values.yaml | 72 ++ ...h_condition_with_supported_intrinsics.yaml | 110 +++ .../preferences/test_deployment_preference.py | 49 +- ...h_condition_with_supported_intrinsics.json | 649 ++++++++++++++++++ ...h_condition_with_supported_intrinsics.json | 649 ++++++++++++++++++ ...through_condition_with_invalid_values.json | 3 + ...h_condition_with_supported_intrinsics.json | 649 ++++++++++++++++++ tests/translator/test_function_resources.py | 3 +- tests/translator/test_translator.py | 1 + 11 files changed, 2216 insertions(+), 18 deletions(-) create mode 100644 tests/translator/input/error_function_with_deployment_preference_passthrough_condition_with_invalid_values.yaml create mode 100644 tests/translator/input/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.yaml create mode 100644 tests/translator/output/aws-cn/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.json create mode 100644 tests/translator/output/aws-us-gov/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.json create mode 100644 tests/translator/output/error_function_with_deployment_preference_passthrough_condition_with_invalid_values.json create mode 100644 tests/translator/output/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.json diff --git a/samtranslator/model/preferences/deployment_preference.py b/samtranslator/model/preferences/deployment_preference.py index ec65a84b4..0f876dcdc 100644 --- a/samtranslator/model/preferences/deployment_preference.py +++ b/samtranslator/model/preferences/deployment_preference.py @@ -75,8 +75,6 @@ def from_dict(cls, logical_id, deployment_preference_dict, condition=None): role = deployment_preference_dict.get("Role", None) trigger_configurations = deployment_preference_dict.get("TriggerConfigurations", None) passthrough_condition = deployment_preference_dict.get("PassthroughCondition", False) - # FIXME: We should support not only true/false, but also yes/no, on/off? See https://yaml.org/type/bool.html - passthrough_condition = True if passthrough_condition in [True, "true", "True"] else False return DeploymentPreference( deployment_type, diff --git a/samtranslator/model/sam_resources.py b/samtranslator/model/sam_resources.py index 49c304c63..95f21c11e 100644 --- a/samtranslator/model/sam_resources.py +++ b/samtranslator/model/sam_resources.py @@ -1,5 +1,7 @@ """ SAM macro definitions """ import copy +from typing import Union +from samtranslator.intrinsics.resolver import IntrinsicsResolver import samtranslator.model.eventsources import samtranslator.model.eventsources.pull @@ -853,11 +855,12 @@ def _validate_deployment_preference_and_add_update_policy( self.DeploymentPreference["Type"] = preference_type if "PassthroughCondition" in self.DeploymentPreference: - # resolve intrinsics and mappings for PassthroughCondition - passthrough_condition = self.DeploymentPreference["PassthroughCondition"] - passthrough_condition = intrinsics_resolver.resolve_parameter_refs(passthrough_condition) - passthrough_condition = mappings_resolver.resolve_parameter_refs(passthrough_condition) - self.DeploymentPreference["PassthroughCondition"] = passthrough_condition + self.DeploymentPreference["PassthroughCondition"] = self._resolve_property_to_boolean( + self.DeploymentPreference["PassthroughCondition"], + "PassthroughCondition", + intrinsics_resolver, + mappings_resolver, + ) elif feature_toggle: self.DeploymentPreference["PassthroughCondition"] = feature_toggle.is_enabled( "deployment_preference_condition_fix" @@ -886,6 +889,40 @@ def _validate_deployment_preference_and_add_update_policy( "UpdatePolicy", deployment_preference_collection.update_policy(self.logical_id).to_dict() ) + def _resolve_property_to_boolean( + self, + property_value: Union[bool, str, dict], + property_name: str, + intrinsics_resolver: IntrinsicsResolver, + mappings_resolver: IntrinsicsResolver, + ) -> bool: + """ + Resolves intrinsics, if any, and/or converts string in a given property to boolean. + Raises InvalidResourceException if can't resolve intrinsic or can't resolve string to boolean + + :param property_value: property value to resolve + :param property_name: name/key of property to resolve + :param intrinsics_resolver: resolves intrinsics + :param mappings_resolver: resolves FindInMap + :return bool: resolved boolean value + """ + processed_property_value = intrinsics_resolver.resolve_parameter_refs(property_value) + processed_property_value = mappings_resolver.resolve_parameter_refs(processed_property_value) + + # FIXME: We should support not only true/false, but also yes/no, on/off? See https://yaml.org/type/bool.html + if processed_property_value in [True, "true", "True"]: + return True + elif processed_property_value in [False, "false", "False"]: + return False + elif is_intrinsic(processed_property_value): # couldn't resolve intrinsic + raise InvalidResourceException( + self.logical_id, + f"Unsupported intrinsic: the only intrinsic functions supported for " + f"property {property_name} are FindInMap and parameter Refs.", + ) + else: + raise InvalidResourceException(self.logical_id, f"Invalid value for property {property_name}.") + def _construct_function_url(self, lambda_function, lambda_alias): """ This method is used to construct a lambda url resource diff --git a/tests/translator/input/error_function_with_deployment_preference_passthrough_condition_with_invalid_values.yaml b/tests/translator/input/error_function_with_deployment_preference_passthrough_condition_with_invalid_values.yaml new file mode 100644 index 000000000..6e110e927 --- /dev/null +++ b/tests/translator/input/error_function_with_deployment_preference_passthrough_condition_with_invalid_values.yaml @@ -0,0 +1,72 @@ +# Tests unsupported intrinsic and invalid type in the PassthroughCondition property + +Mappings: + HelloWorldMap: + hello: + key1: true + key2: false + world: + key1: false + key2: true + +Parameters: + FnName: + Type: String + ProvisionedConcurrency: + Type: String + Default: 10 + EnableAliasProvisionedConcurrency: + Type: String + AllowedValues: + - true + - false + Default: true + DefaultTrueParam: + Type: String + Default: "true" + DefaultFalseParam: + Type: String + Default: "false" + HelloWorldParam: + Type: String + Default: "hello" + +Conditions: + AliasProvisionedConcurrencyEnabled: !Equals [!Ref EnableAliasProvisionedConcurrency, true] + FunctionCondition: !Equals [true, true] + +Resources: + InvalidType: + Type: 'AWS::Serverless::Function' + Condition: FunctionCondition + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + AutoPublishAlias: live + DeploymentPreference: + Type: Linear10PercentEvery3Minutes + PassthroughCondition: ["hello"] + ProvisionedConcurrencyConfig: !If + - AliasProvisionedConcurrencyEnabled + - ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency + - !Ref 'AWS::NoValue' + + UnsupportedIntrinsic: + Type: 'AWS::Serverless::Function' + Condition: FunctionCondition + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + AutoPublishAlias: live + DeploymentPreference: + Type: Linear10PercentEvery3Minutes + PassthroughCondition: !If + - AliasProvisionedConcurrencyEnabled + - true + - false + ProvisionedConcurrencyConfig: !If + - AliasProvisionedConcurrencyEnabled + - ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency + - !Ref 'AWS::NoValue' diff --git a/tests/translator/input/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.yaml b/tests/translator/input/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.yaml new file mode 100644 index 000000000..0c438d554 --- /dev/null +++ b/tests/translator/input/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.yaml @@ -0,0 +1,110 @@ +# Tests supported intrinsics in the PassthroughCondition property + +Mappings: + HelloWorldMap: + hello: + key1: true + key2: false + world: + key1: false + key2: true + +Parameters: + FnName: + Type: String + ProvisionedConcurrency: + Type: String + Default: 10 + EnableAliasProvisionedConcurrency: + Type: String + AllowedValues: + - true + - false + Default: true + DefaultTrueParam: + Type: String + Default: "true" + DefaultFalseParam: + Type: String + Default: "false" + HelloParam: + Type: String + Default: "hello" + WorldParam: + Type: String + Default: "world" + +Conditions: + AliasProvisionedConcurrencyEnabled: !Equals [!Ref EnableAliasProvisionedConcurrency, true] + FunctionCondition: !Equals [true, true] + +Resources: + TrueRef: + Type: 'AWS::Serverless::Function' + Condition: FunctionCondition + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + AutoPublishAlias: live + DeploymentPreference: + Type: Linear10PercentEvery3Minutes + PassthroughCondition: !Ref DefaultTrueParam + ProvisionedConcurrencyConfig: !If + - AliasProvisionedConcurrencyEnabled + - ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency + - !Ref 'AWS::NoValue' + + FalseRef: + Type: 'AWS::Serverless::Function' + Condition: FunctionCondition + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + AutoPublishAlias: live + DeploymentPreference: + Type: Linear10PercentEvery3Minutes + PassthroughCondition: !Ref DefaultFalseParam + ProvisionedConcurrencyConfig: !If + - AliasProvisionedConcurrencyEnabled + - ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency + - !Ref 'AWS::NoValue' + + TrueFindInMap: + Type: 'AWS::Serverless::Function' + Condition: FunctionCondition + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + AutoPublishAlias: live + DeploymentPreference: + Type: Linear10PercentEvery3Minutes + PassthroughCondition: !FindInMap + - HelloWorldMap + - !Ref HelloParam + - key1 + ProvisionedConcurrencyConfig: !If + - AliasProvisionedConcurrencyEnabled + - ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency + - !Ref 'AWS::NoValue' + + FalseFindInMap: + Type: 'AWS::Serverless::Function' + Condition: FunctionCondition + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + AutoPublishAlias: live + DeploymentPreference: + Type: Linear10PercentEvery3Minutes + PassthroughCondition: !FindInMap + - HelloWorldMap + - !Ref WorldParam + - key1 + ProvisionedConcurrencyConfig: !If + - AliasProvisionedConcurrencyEnabled + - ProvisionedConcurrentExecutions: !Ref ProvisionedConcurrency + - !Ref 'AWS::NoValue' \ No newline at end of file diff --git a/tests/translator/model/preferences/test_deployment_preference.py b/tests/translator/model/preferences/test_deployment_preference.py index 8eab88baa..2f7a7645d 100644 --- a/tests/translator/model/preferences/test_deployment_preference.py +++ b/tests/translator/model/preferences/test_deployment_preference.py @@ -17,33 +17,50 @@ def setUp(self): "TriggerName": "TestTrigger", } self.condition = "condition" - self.expected_deployment_preference = DeploymentPreference( - self.deployment_type, + + def test_from_dict_with_intrinsic_function_type(self): + + type = {"Ref": "SomeType"} + expected_deployment_preference = DeploymentPreference( + type, self.pre_traffic_hook, self.post_traffic_hook, self.alarms, True, self.role, self.trigger_configurations, - self.condition, + None, ) - def test_from_dict_with_intrinsic_function_type(self): + deployment_preference_yaml_dict = dict() + deployment_preference_yaml_dict["Type"] = type + deployment_preference_yaml_dict["Hooks"] = { + "PreTraffic": self.pre_traffic_hook, + "PostTraffic": self.post_traffic_hook, + } + deployment_preference_yaml_dict["Alarms"] = self.alarms + deployment_preference_yaml_dict["Role"] = self.role + deployment_preference_yaml_dict["TriggerConfigurations"] = self.trigger_configurations + deployment_preference_from_yaml_dict = DeploymentPreference.from_dict( + "logical_id", deployment_preference_yaml_dict, self.condition + ) - type = {"Ref": "SomeType"} + self.assertEqual(expected_deployment_preference, deployment_preference_from_yaml_dict) + + def test_from_dict(self): expected_deployment_preference = DeploymentPreference( - type, + self.deployment_type, self.pre_traffic_hook, self.post_traffic_hook, self.alarms, True, self.role, self.trigger_configurations, - self.condition, + None, ) deployment_preference_yaml_dict = dict() - deployment_preference_yaml_dict["Type"] = type + deployment_preference_yaml_dict["Type"] = self.deployment_type deployment_preference_yaml_dict["Hooks"] = { "PreTraffic": self.pre_traffic_hook, "PostTraffic": self.post_traffic_hook, @@ -57,7 +74,18 @@ def test_from_dict_with_intrinsic_function_type(self): self.assertEqual(expected_deployment_preference, deployment_preference_from_yaml_dict) - def test_from_dict(self): + def test_from_dict_with_passthrough_condition(self): + expected_deployment_preference = DeploymentPreference( + self.deployment_type, + self.pre_traffic_hook, + self.post_traffic_hook, + self.alarms, + True, + self.role, + self.trigger_configurations, + self.condition, + ) + deployment_preference_yaml_dict = dict() deployment_preference_yaml_dict["Type"] = self.deployment_type deployment_preference_yaml_dict["Hooks"] = { @@ -67,11 +95,12 @@ def test_from_dict(self): deployment_preference_yaml_dict["Alarms"] = self.alarms deployment_preference_yaml_dict["Role"] = self.role deployment_preference_yaml_dict["TriggerConfigurations"] = self.trigger_configurations + deployment_preference_yaml_dict["PassthroughCondition"] = True deployment_preference_from_yaml_dict = DeploymentPreference.from_dict( "logical_id", deployment_preference_yaml_dict, self.condition ) - self.assertEqual(self.expected_deployment_preference, deployment_preference_from_yaml_dict) + self.assertEqual(expected_deployment_preference, deployment_preference_from_yaml_dict) def test_from_dict_with_disabled_preference_does_not_require_other_parameters(self): expected_deployment_preference = DeploymentPreference(None, None, None, None, False, None, None, None) diff --git a/tests/translator/output/aws-cn/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.json b/tests/translator/output/aws-cn/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.json new file mode 100644 index 000000000..7e26228d8 --- /dev/null +++ b/tests/translator/output/aws-cn/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.json @@ -0,0 +1,649 @@ +{ + "Mappings": { + "HelloWorldMap": { + "hello": { + "key1": true, + "key2": false + }, + "world": { + "key1": false, + "key2": true + } + } + }, + "Parameters": { + "FnName": { + "Type": "String" + }, + "ProvisionedConcurrency": { + "Type": "String", + "Default": 10 + }, + "EnableAliasProvisionedConcurrency": { + "Type": "String", + "AllowedValues": [ + true, + false + ], + "Default": true + }, + "DefaultTrueParam": { + "Type": "String", + "Default": "true" + }, + "DefaultFalseParam": { + "Type": "String", + "Default": "false" + }, + "HelloParam": { + "Type": "String", + "Default": "hello" + }, + "WorldParam": { + "Type": "String", + "Default": "world" + } + }, + "Conditions": { + "AliasProvisionedConcurrencyEnabled": { + "Fn::Equals": [ + { + "Ref": "EnableAliasProvisionedConcurrency" + }, + true + ] + }, + "FunctionCondition": { + "Fn::Equals": [ + true, + true + ] + } + }, + "Resources": { + "TrueRef": { + "Type": "AWS::Lambda::Function", + "Condition": "FunctionCondition", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "TrueRefRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "TrueRefVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "FunctionCondition", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "TrueRef" + } + } + }, + "TrueRefAliaslive": { + "Type": "AWS::Lambda::Alias", + "Condition": "FunctionCondition", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "TrueRefDeploymentGroup" + } + } + }, + "Properties": { + "Name": "live", + "FunctionName": { + "Ref": "TrueRef" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "TrueRefVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + } + } + }, + "TrueRefRole": { + "Type": "AWS::IAM::Role", + "Condition": "FunctionCondition", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "FalseRef": { + "Type": "AWS::Lambda::Function", + "Condition": "FunctionCondition", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "FalseRefRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "FalseRefVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "FunctionCondition", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "FalseRef" + } + } + }, + "FalseRefAliaslive": { + "Type": "AWS::Lambda::Alias", + "Condition": "FunctionCondition", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "FalseRefDeploymentGroup" + } + } + }, + "Properties": { + "Name": "live", + "FunctionName": { + "Ref": "FalseRef" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "FalseRefVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + } + } + }, + "FalseRefRole": { + "Type": "AWS::IAM::Role", + "Condition": "FunctionCondition", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "TrueFindInMap": { + "Type": "AWS::Lambda::Function", + "Condition": "FunctionCondition", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "TrueFindInMapRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "TrueFindInMapVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "FunctionCondition", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "TrueFindInMap" + } + } + }, + "TrueFindInMapAliaslive": { + "Type": "AWS::Lambda::Alias", + "Condition": "FunctionCondition", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "TrueFindInMapDeploymentGroup" + } + } + }, + "Properties": { + "Name": "live", + "FunctionName": { + "Ref": "TrueFindInMap" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "TrueFindInMapVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + } + } + }, + "TrueFindInMapRole": { + "Type": "AWS::IAM::Role", + "Condition": "FunctionCondition", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "FalseFindInMap": { + "Type": "AWS::Lambda::Function", + "Condition": "FunctionCondition", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "FalseFindInMapRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "FalseFindInMapVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "FunctionCondition", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "FalseFindInMap" + } + } + }, + "FalseFindInMapAliaslive": { + "Type": "AWS::Lambda::Alias", + "Condition": "FunctionCondition", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "FalseFindInMapDeploymentGroup" + } + } + }, + "Properties": { + "Name": "live", + "FunctionName": { + "Ref": "FalseFindInMap" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "FalseFindInMapVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + } + } + }, + "FalseFindInMapRole": { + "Type": "AWS::IAM::Role", + "Condition": "FunctionCondition", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, + "CodeDeployServiceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + ] + } + }, + "TrueRefDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Condition": "FunctionCondition", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + }, + "FalseRefDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + }, + "TrueFindInMapDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Condition": "FunctionCondition", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + }, + "FalseFindInMapDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + } + } +} \ No newline at end of file diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.json b/tests/translator/output/aws-us-gov/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.json new file mode 100644 index 000000000..05b248c26 --- /dev/null +++ b/tests/translator/output/aws-us-gov/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.json @@ -0,0 +1,649 @@ +{ + "Mappings": { + "HelloWorldMap": { + "hello": { + "key1": true, + "key2": false + }, + "world": { + "key1": false, + "key2": true + } + } + }, + "Parameters": { + "FnName": { + "Type": "String" + }, + "ProvisionedConcurrency": { + "Type": "String", + "Default": 10 + }, + "EnableAliasProvisionedConcurrency": { + "Type": "String", + "AllowedValues": [ + true, + false + ], + "Default": true + }, + "DefaultTrueParam": { + "Type": "String", + "Default": "true" + }, + "DefaultFalseParam": { + "Type": "String", + "Default": "false" + }, + "HelloParam": { + "Type": "String", + "Default": "hello" + }, + "WorldParam": { + "Type": "String", + "Default": "world" + } + }, + "Conditions": { + "AliasProvisionedConcurrencyEnabled": { + "Fn::Equals": [ + { + "Ref": "EnableAliasProvisionedConcurrency" + }, + true + ] + }, + "FunctionCondition": { + "Fn::Equals": [ + true, + true + ] + } + }, + "Resources": { + "TrueRef": { + "Type": "AWS::Lambda::Function", + "Condition": "FunctionCondition", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "TrueRefRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "TrueRefVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "FunctionCondition", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "TrueRef" + } + } + }, + "TrueRefAliaslive": { + "Type": "AWS::Lambda::Alias", + "Condition": "FunctionCondition", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "TrueRefDeploymentGroup" + } + } + }, + "Properties": { + "Name": "live", + "FunctionName": { + "Ref": "TrueRef" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "TrueRefVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + } + } + }, + "TrueRefRole": { + "Type": "AWS::IAM::Role", + "Condition": "FunctionCondition", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "FalseRef": { + "Type": "AWS::Lambda::Function", + "Condition": "FunctionCondition", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "FalseRefRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "FalseRefVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "FunctionCondition", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "FalseRef" + } + } + }, + "FalseRefAliaslive": { + "Type": "AWS::Lambda::Alias", + "Condition": "FunctionCondition", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "FalseRefDeploymentGroup" + } + } + }, + "Properties": { + "Name": "live", + "FunctionName": { + "Ref": "FalseRef" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "FalseRefVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + } + } + }, + "FalseRefRole": { + "Type": "AWS::IAM::Role", + "Condition": "FunctionCondition", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "TrueFindInMap": { + "Type": "AWS::Lambda::Function", + "Condition": "FunctionCondition", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "TrueFindInMapRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "TrueFindInMapVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "FunctionCondition", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "TrueFindInMap" + } + } + }, + "TrueFindInMapAliaslive": { + "Type": "AWS::Lambda::Alias", + "Condition": "FunctionCondition", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "TrueFindInMapDeploymentGroup" + } + } + }, + "Properties": { + "Name": "live", + "FunctionName": { + "Ref": "TrueFindInMap" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "TrueFindInMapVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + } + } + }, + "TrueFindInMapRole": { + "Type": "AWS::IAM::Role", + "Condition": "FunctionCondition", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "FalseFindInMap": { + "Type": "AWS::Lambda::Function", + "Condition": "FunctionCondition", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "FalseFindInMapRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "FalseFindInMapVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "FunctionCondition", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "FalseFindInMap" + } + } + }, + "FalseFindInMapAliaslive": { + "Type": "AWS::Lambda::Alias", + "Condition": "FunctionCondition", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "FalseFindInMapDeploymentGroup" + } + } + }, + "Properties": { + "Name": "live", + "FunctionName": { + "Ref": "FalseFindInMap" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "FalseFindInMapVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + } + } + }, + "FalseFindInMapRole": { + "Type": "AWS::IAM::Role", + "Condition": "FunctionCondition", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, + "CodeDeployServiceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + ] + } + }, + "TrueRefDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Condition": "FunctionCondition", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + }, + "FalseRefDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + }, + "TrueFindInMapDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Condition": "FunctionCondition", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + }, + "FalseFindInMapDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + } + } +} \ No newline at end of file diff --git a/tests/translator/output/error_function_with_deployment_preference_passthrough_condition_with_invalid_values.json b/tests/translator/output/error_function_with_deployment_preference_passthrough_condition_with_invalid_values.json new file mode 100644 index 000000000..97773ed2f --- /dev/null +++ b/tests/translator/output/error_function_with_deployment_preference_passthrough_condition_with_invalid_values.json @@ -0,0 +1,3 @@ +{ + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 2. Resource with id [InvalidType] is invalid. invalid value Resource with id [UnsupportedIntrinsic] is invalid. unsupported intrinsic" +} \ No newline at end of file diff --git a/tests/translator/output/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.json b/tests/translator/output/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.json new file mode 100644 index 000000000..9efc3b9c1 --- /dev/null +++ b/tests/translator/output/function_with_deployment_preference_passthrough_condition_with_supported_intrinsics.json @@ -0,0 +1,649 @@ +{ + "Mappings": { + "HelloWorldMap": { + "hello": { + "key1": true, + "key2": false + }, + "world": { + "key1": false, + "key2": true + } + } + }, + "Parameters": { + "FnName": { + "Type": "String" + }, + "ProvisionedConcurrency": { + "Type": "String", + "Default": 10 + }, + "EnableAliasProvisionedConcurrency": { + "Type": "String", + "AllowedValues": [ + true, + false + ], + "Default": true + }, + "DefaultTrueParam": { + "Type": "String", + "Default": "true" + }, + "DefaultFalseParam": { + "Type": "String", + "Default": "false" + }, + "HelloParam": { + "Type": "String", + "Default": "hello" + }, + "WorldParam": { + "Type": "String", + "Default": "world" + } + }, + "Conditions": { + "AliasProvisionedConcurrencyEnabled": { + "Fn::Equals": [ + { + "Ref": "EnableAliasProvisionedConcurrency" + }, + true + ] + }, + "FunctionCondition": { + "Fn::Equals": [ + true, + true + ] + } + }, + "Resources": { + "TrueRef": { + "Type": "AWS::Lambda::Function", + "Condition": "FunctionCondition", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "TrueRefRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "TrueRefVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "FunctionCondition", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "TrueRef" + } + } + }, + "TrueRefAliaslive": { + "Type": "AWS::Lambda::Alias", + "Condition": "FunctionCondition", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "TrueRefDeploymentGroup" + } + } + }, + "Properties": { + "Name": "live", + "FunctionName": { + "Ref": "TrueRef" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "TrueRefVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + } + } + }, + "TrueRefRole": { + "Type": "AWS::IAM::Role", + "Condition": "FunctionCondition", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "FalseRef": { + "Type": "AWS::Lambda::Function", + "Condition": "FunctionCondition", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "FalseRefRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "FalseRefVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "FunctionCondition", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "FalseRef" + } + } + }, + "FalseRefAliaslive": { + "Type": "AWS::Lambda::Alias", + "Condition": "FunctionCondition", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "FalseRefDeploymentGroup" + } + } + }, + "Properties": { + "Name": "live", + "FunctionName": { + "Ref": "FalseRef" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "FalseRefVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + } + } + }, + "FalseRefRole": { + "Type": "AWS::IAM::Role", + "Condition": "FunctionCondition", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "TrueFindInMap": { + "Type": "AWS::Lambda::Function", + "Condition": "FunctionCondition", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "TrueFindInMapRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "TrueFindInMapVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "FunctionCondition", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "TrueFindInMap" + } + } + }, + "TrueFindInMapAliaslive": { + "Type": "AWS::Lambda::Alias", + "Condition": "FunctionCondition", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "TrueFindInMapDeploymentGroup" + } + } + }, + "Properties": { + "Name": "live", + "FunctionName": { + "Ref": "TrueFindInMap" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "TrueFindInMapVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + } + } + }, + "TrueFindInMapRole": { + "Type": "AWS::IAM::Role", + "Condition": "FunctionCondition", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "FalseFindInMap": { + "Type": "AWS::Lambda::Function", + "Condition": "FunctionCondition", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "FalseFindInMapRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "FalseFindInMapVersion640128d35d": { + "Type": "AWS::Lambda::Version", + "Condition": "FunctionCondition", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": { + "Ref": "FalseFindInMap" + } + } + }, + "FalseFindInMapAliaslive": { + "Type": "AWS::Lambda::Alias", + "Condition": "FunctionCondition", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "FalseFindInMapDeploymentGroup" + } + } + }, + "Properties": { + "Name": "live", + "FunctionName": { + "Ref": "FalseFindInMap" + }, + "FunctionVersion": { + "Fn::GetAtt": [ + "FalseFindInMapVersion640128d35d", + "Version" + ] + }, + "ProvisionedConcurrencyConfig": { + "Fn::If": [ + "AliasProvisionedConcurrencyEnabled", + { + "ProvisionedConcurrentExecutions": { + "Ref": "ProvisionedConcurrency" + } + }, + { + "Ref": "AWS::NoValue" + } + ] + } + } + }, + "FalseFindInMapRole": { + "Type": "AWS::IAM::Role", + "Condition": "FunctionCondition", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + } + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, + "CodeDeployServiceRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "codedeploy.amazonaws.com" + ] + } + } + ] + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + ] + } + }, + "TrueRefDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Condition": "FunctionCondition", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + }, + "FalseRefDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + }, + "TrueFindInMapDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Condition": "FunctionCondition", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + }, + "FalseFindInMapDeploymentGroup": { + "Type": "AWS::CodeDeploy::DeploymentGroup", + "Properties": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "AutoRollbackConfiguration": { + "Enabled": true, + "Events": [ + "DEPLOYMENT_FAILURE", + "DEPLOYMENT_STOP_ON_ALARM", + "DEPLOYMENT_STOP_ON_REQUEST" + ] + }, + "DeploymentConfigName": { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Linear10PercentEvery3Minutes" + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + }, + "ServiceRoleArn": { + "Fn::GetAtt": [ + "CodeDeployServiceRole", + "Arn" + ] + } + } + } + } +} \ No newline at end of file diff --git a/tests/translator/test_function_resources.py b/tests/translator/test_function_resources.py index adade5d90..27a0d4bbd 100644 --- a/tests/translator/test_function_resources.py +++ b/tests/translator/test_function_resources.py @@ -436,6 +436,7 @@ def test_sam_function_with_deployment_preference_passthrough_condition_through_p "S3Key": "key", "S3ObjectVersion": "version", } + self.mappings_resolver_mock.resolve_parameter_refs.return_value = True resources = sam_func.to_cloudformation(**kwargs) deployment_preference_collection.update_policy.assert_called_once_with(self.sam_func.logical_id) @@ -453,7 +454,7 @@ def test_sam_function_with_deployment_preference_passthrough_condition_through_p def test_sam_function_with_deployment_preference_passthrough_condition_through_feature_flag( self, get_resolved_alias_name_mock ): - deploy_preference_dict = {"Type": "LINEAR", "PassthroughCondition": True} + deploy_preference_dict = {"Type": "LINEAR"} alias_name = "AliasName" func = { "Type": "AWS::Serverless::Function", diff --git a/tests/translator/test_translator.py b/tests/translator/test_translator.py index 81ec500e3..4a246747a 100644 --- a/tests/translator/test_translator.py +++ b/tests/translator/test_translator.py @@ -374,6 +374,7 @@ class TestTranslatorEndToEnd(AbstractTestTranslator): "function_with_deployment_preference_alarms_intrinsic_if", "function_with_deployment_preference_multiple_combinations_conditions_with_passthrough", "function_with_deployment_preference_multiple_combinations_conditions_without_passthrough", + "function_with_deployment_preference_passthrough_condition_with_supported_intrinsics", "function_with_alias_and_event_sources", "function_with_resource_refs", "function_with_deployment_and_custom_role", From d39ef47acc2cb4f58d2f540824d296d2efad1abd Mon Sep 17 00:00:00 2001 From: Ruperto Torres Date: Tue, 17 May 2022 09:54:07 -0700 Subject: [PATCH 14/16] update invalid intrinsics end-to-end test --- ...nt_preference_passthrough_condition_with_invalid_values.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/translator/output/error_function_with_deployment_preference_passthrough_condition_with_invalid_values.json b/tests/translator/output/error_function_with_deployment_preference_passthrough_condition_with_invalid_values.json index 97773ed2f..931b1c7d2 100644 --- a/tests/translator/output/error_function_with_deployment_preference_passthrough_condition_with_invalid_values.json +++ b/tests/translator/output/error_function_with_deployment_preference_passthrough_condition_with_invalid_values.json @@ -1,3 +1,3 @@ { - "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 2. Resource with id [InvalidType] is invalid. invalid value Resource with id [UnsupportedIntrinsic] is invalid. unsupported intrinsic" + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 2. Resource with id [InvalidType] is invalid. Invalid value for property PassthroughCondition. Resource with id [UnsupportedIntrinsic] is invalid. Unsupported intrinsic: the only intrinsic functions supported for property PassthroughCondition are FindInMap and parameter Refs." } \ No newline at end of file From 4597514e356a3d40d7cee7b3263fb1c20209ebf6 Mon Sep 17 00:00:00 2001 From: Ruperto Torres Date: Tue, 17 May 2022 10:15:16 -0700 Subject: [PATCH 15/16] uncomment and update invalid value unit test --- tests/translator/test_function_resources.py | 105 ++++++++++---------- 1 file changed, 53 insertions(+), 52 deletions(-) diff --git a/tests/translator/test_function_resources.py b/tests/translator/test_function_resources.py index 27a0d4bbd..607939f63 100644 --- a/tests/translator/test_function_resources.py +++ b/tests/translator/test_function_resources.py @@ -499,58 +499,59 @@ def test_sam_function_with_deployment_preference_passthrough_condition_through_f self.assertTrue("UpdatePolicy" in list(aliases[0].values())[0]) self.assertEqual(list(aliases[0].values())[0]["UpdatePolicy"], self.update_policy().to_dict()) - # @parameterized.expand( - # [ - # ({"Ref": "Param1"},), - # ("my_string",), - # ] - # ) - # @patch("boto3.session.Session.region_name", "ap-southeast-1") - # @patch.object(SamFunction, "_get_resolved_alias_name") - # def test_sam_function_with_deployment_preference_passthrough_condition_invalid_input( - # self, invalid_passthrough_condition, get_resolved_alias_name_mock - # ): - # deploy_preference_dict = {"Type": "LINEAR", "PassthroughCondition": invalid_passthrough_condition} - # alias_name = "AliasName" - # func = { - # "Type": "AWS::Serverless::Function", - # "Condition": "Condition1", - # "Properties": { - # "CodeUri": self.code_uri, - # "Runtime": "nodejs12.x", - # "Handler": "index.handler", - # "AutoPublishAlias": alias_name, - # "DeploymentPreference": deploy_preference_dict, - # }, - # } - - # sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) - - # kwargs = dict() - # kwargs["managed_policy_map"] = {"a": "b"} - # kwargs["event_resources"] = [] - # kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock - # kwargs["mappings_resolver"] = self.mappings_resolver_mock - # deployment_preference_collection = self._make_deployment_preference_collection() - # kwargs["deployment_preference_collection"] = deployment_preference_collection - # get_resolved_alias_name_mock.return_value = alias_name - # feature_toggle_mock = Mock() - # feature_toggle_mock.is_enabled.side_effect = lambda x: x == "deployment_preference_condition_fix" - # kwargs["feature_toggle"] = feature_toggle_mock - - # self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = { - # "S3Bucket": "bucket", - # "S3Key": "key", - # "S3ObjectVersion": "version", - # } - - # with self.assertRaises(InvalidResourceException) as e: - # sam_func.to_cloudformation(**kwargs) - - # self.assertEqual( - # e.exception.message, - # "Resource with id [foo] is invalid. 'DeploymentPreference.PassthroughCondition' must be a boolean value and does not support intrinsic.", - # ) + @parameterized.expand( + [ + ({"Fn::Sub": ["${Hello}", {"Hello": "helloworld"}]}, "Resource with id [foo] is invalid. Unsupported intrinsic: the only intrinsic functions supported for property PassthroughCondition are FindInMap and parameter Refs."), + ("my_string", "Resource with id [foo] is invalid. Invalid value for property PassthroughCondition."), + ] + ) + @patch("boto3.session.Session.region_name", "ap-southeast-1") + @patch.object(SamFunction, "_get_resolved_alias_name") + def test_sam_function_with_deployment_preference_passthrough_condition_invalid_input( + self, invalid_passthrough_condition, expected_exception_message, get_resolved_alias_name_mock + ): + deploy_preference_dict = {"Type": "LINEAR", "PassthroughCondition": invalid_passthrough_condition} + alias_name = "AliasName" + func = { + "Type": "AWS::Serverless::Function", + "Condition": "Condition1", + "Properties": { + "CodeUri": self.code_uri, + "Runtime": "nodejs12.x", + "Handler": "index.handler", + "AutoPublishAlias": alias_name, + "DeploymentPreference": deploy_preference_dict, + }, + } + + sam_func = SamFunction.from_dict(logical_id="foo", resource_dict=func) + + kwargs = dict() + kwargs["managed_policy_map"] = {"a": "b"} + kwargs["event_resources"] = [] + kwargs["intrinsics_resolver"] = self.intrinsics_resolver_mock + kwargs["mappings_resolver"] = self.mappings_resolver_mock + deployment_preference_collection = self._make_deployment_preference_collection() + kwargs["deployment_preference_collection"] = deployment_preference_collection + get_resolved_alias_name_mock.return_value = alias_name + feature_toggle_mock = Mock() + feature_toggle_mock.is_enabled.side_effect = lambda x: x == "deployment_preference_condition_fix" + kwargs["feature_toggle"] = feature_toggle_mock + + self.intrinsics_resolver_mock.resolve_parameter_refs.return_value = { + "S3Bucket": "bucket", + "S3Key": "key", + "S3ObjectVersion": "version", + } + self.mappings_resolver_mock.resolve_parameter_refs.return_value = invalid_passthrough_condition + + with self.assertRaises(InvalidResourceException) as e: + sam_func.to_cloudformation(**kwargs) + + self.assertEqual( + e.exception.message, + expected_exception_message, + ) @patch("samtranslator.translator.logical_id_generator.LogicalIdGenerator") def test_version_creation(self, LogicalIdGeneratorMock): From bdaded1aa1b446e28a473853db874ba32004a1c7 Mon Sep 17 00:00:00 2001 From: Ruperto Torres Date: Tue, 17 May 2022 11:12:42 -0700 Subject: [PATCH 16/16] black --- tests/translator/test_function_resources.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/translator/test_function_resources.py b/tests/translator/test_function_resources.py index 607939f63..bb3c8e5cc 100644 --- a/tests/translator/test_function_resources.py +++ b/tests/translator/test_function_resources.py @@ -501,7 +501,10 @@ def test_sam_function_with_deployment_preference_passthrough_condition_through_f @parameterized.expand( [ - ({"Fn::Sub": ["${Hello}", {"Hello": "helloworld"}]}, "Resource with id [foo] is invalid. Unsupported intrinsic: the only intrinsic functions supported for property PassthroughCondition are FindInMap and parameter Refs."), + ( + {"Fn::Sub": ["${Hello}", {"Hello": "helloworld"}]}, + "Resource with id [foo] is invalid. Unsupported intrinsic: the only intrinsic functions supported for property PassthroughCondition are FindInMap and parameter Refs.", + ), ("my_string", "Resource with id [foo] is invalid. Invalid value for property PassthroughCondition."), ] )