From 759fb8dfc230f5a8ab067b572d65481c4e4baafd Mon Sep 17 00:00:00 2001 From: Manvendra Singh Date: Sat, 9 Mar 2019 18:05:36 +0530 Subject: [PATCH 1/7] Added Support for referencing custom configuration in CodeDeploy --- .../deployment_preference_collection.py | 20 ++++++++++-- .../function_with_deployment_preference.yaml | 2 +- ...ment_preference_multiple_combinations.yaml | 2 +- .../test_deployment_preference_collection.py | 32 +++++++++++++++++-- .../function_with_deployment_preference.json | 2 +- ...ment_preference_multiple_combinations.json | 2 +- .../function_with_deployment_preference.json | 2 +- ...ment_preference_multiple_combinations.json | 2 +- .../function_with_deployment_preference.json | 2 +- ...ment_preference_multiple_combinations.json | 2 +- 10 files changed, 55 insertions(+), 13 deletions(-) diff --git a/samtranslator/model/preferences/deployment_preference_collection.py b/samtranslator/model/preferences/deployment_preference_collection.py index 25ac19158..37a3df137 100644 --- a/samtranslator/model/preferences/deployment_preference_collection.py +++ b/samtranslator/model/preferences/deployment_preference_collection.py @@ -8,6 +8,16 @@ CODE_DEPLOY_SERVICE_ROLE_LOGICAL_ID = 'CodeDeployServiceRole' CODEDEPLOY_APPLICATION_LOGICAL_ID = 'ServerlessDeploymentApplication' +CODEDEPLOY_PREDEFINED_CONFIGURATIONS_LIST = ["Canary10Percent5Minutes", + "Canary10Percent10Minutes", + "Canary10Percent15Minutes", + "Canary10Percent30Minutes", + "Linear10PercentEvery1Minute", + "Linear10PercentEvery2Minutes", + "Linear10PercentEvery3Minutes", + "Linear10PercentEvery10Minutes", + "AllAtOnce" + ] class DeploymentPreferenceCollection(object): @@ -95,6 +105,7 @@ def deployment_group(self, function_logical_id): :param function_logical_id: logical_id of the function this deployment group belongs to :return: CodeDeployDeploymentGroup resource """ + deployment_preference = self.get(function_logical_id) deployment_group = CodeDeployDeploymentGroup(self.deployment_group_logical_id(function_logical_id)) @@ -109,8 +120,13 @@ def deployment_group(self, function_logical_id): 'Events': ['DEPLOYMENT_FAILURE', 'DEPLOYMENT_STOP_ON_ALARM', 'DEPLOYMENT_STOP_ON_REQUEST']} - deployment_group.DeploymentConfigName = fnSub("CodeDeployDefault.Lambda${ConfigName}", - {"ConfigName": deployment_preference.deployment_type}) + + if deployment_preference.deployment_type in CODEDEPLOY_PREDEFINED_CONFIGURATIONS_LIST: + deployment_group.DeploymentConfigName = fnSub("CodeDeployDefault.Lambda${ConfigName}", + {"ConfigName": deployment_preference.deployment_type}) + else: + deployment_group.DeploymentConfigName = deployment_preference.deployment_type + deployment_group.DeploymentStyle = {'DeploymentType': 'BLUE_GREEN', 'DeploymentOption': 'WITH_TRAFFIC_CONTROL'} diff --git a/tests/translator/input/function_with_deployment_preference.yaml b/tests/translator/input/function_with_deployment_preference.yaml index f2215849e..19eaa9431 100644 --- a/tests/translator/input/function_with_deployment_preference.yaml +++ b/tests/translator/input/function_with_deployment_preference.yaml @@ -7,4 +7,4 @@ Resources: Runtime: python2.7 AutoPublishAlias: live DeploymentPreference: - Type: Linear10PercentEvery3Minute \ No newline at end of file + Type: Linear10PercentEvery3Minutes \ No newline at end of file diff --git a/tests/translator/input/function_with_deployment_preference_multiple_combinations.yaml b/tests/translator/input/function_with_deployment_preference_multiple_combinations.yaml index 079d10950..588983aa0 100644 --- a/tests/translator/input/function_with_deployment_preference_multiple_combinations.yaml +++ b/tests/translator/input/function_with_deployment_preference_multiple_combinations.yaml @@ -25,7 +25,7 @@ Resources: Runtime: python2.7 AutoPublishAlias: livewithdeploymentwithhooksandalarms DeploymentPreference: - Type: Linear10PercentEvery2Minute + Type: Linear10PercentEvery2Minutes Hooks: PreTraffic: !Ref MySanityTestFunction PostTraffic: !Ref MyValidationTestFunction diff --git a/tests/translator/model/preferences/test_deployment_preference_collection.py b/tests/translator/model/preferences/test_deployment_preference_collection.py index 0481628b6..bc0464f76 100644 --- a/tests/translator/model/preferences/test_deployment_preference_collection.py +++ b/tests/translator/model/preferences/test_deployment_preference_collection.py @@ -63,12 +63,11 @@ def test_deployment_group_with_minimal_parameters(self): 'Events': ['DEPLOYMENT_FAILURE', 'DEPLOYMENT_STOP_ON_ALARM', 'DEPLOYMENT_STOP_ON_REQUEST']} - deployment_type = 'deployment_type' expected_deployment_group.DeploymentConfigName = { 'Fn::Sub': [ 'CodeDeployDefault.Lambda${ConfigName}', { - 'ConfigName': deployment_type + 'ConfigName': self.deployment_type_global } ] } @@ -77,12 +76,39 @@ def test_deployment_group_with_minimal_parameters(self): expected_deployment_group.ServiceRoleArn = {'Fn::GetAtt': [CODE_DEPLOY_SERVICE_ROLE_LOGICAL_ID, 'Arn']} deployment_preference_collection = DeploymentPreferenceCollection() - deployment_preference_collection.add(self.function_logical_id, {'Type': deployment_type}) + deployment_preference_collection.add(self.function_logical_id, {'Type': self.deployment_type_global}) deployment_group = deployment_preference_collection.deployment_group(self.function_logical_id) self.assertEqual(deployment_group.to_dict(), expected_deployment_group.to_dict()) + @patch('boto3.session.Session.region_name', 'ap-southeast-1') + def test_deployment_preference_with_codedeploy_custom_configuration(self): + deployment_type = "TestDeploymentConfiguration" + deployment_preference_collection = DeploymentPreferenceCollection() + deployment_preference_collection.add(self.function_logical_id, {'Type': deployment_type}) + deployment_group = deployment_preference_collection.deployment_group(self.function_logical_id) + + self.assertEqual(deployment_type, deployment_group.DeploymentConfigName) + + @patch('boto3.session.Session.region_name', 'ap-southeast-1') + def test_deployment_preference_with_codedeploy_predifined_configuration(self): + deployment_type = "Canary10Percent5Minutes" + expected_deployment_configname = { + 'Fn::Sub': [ + 'CodeDeployDefault.Lambda${ConfigName}', + { + 'ConfigName': deployment_type + } + ] + } + deployment_preference_collection = DeploymentPreferenceCollection() + deployment_preference_collection.add(self.function_logical_id, {'Type': deployment_type}) + deployment_group = deployment_preference_collection.deployment_group(self.function_logical_id) + + print(deployment_group.DeploymentConfigName) + self.assertEqual(expected_deployment_configname, deployment_group.DeploymentConfigName) + @patch('boto3.session.Session.region_name', 'ap-southeast-1') def test_deployment_group_with_all_parameters(self): expected_deployment_group = CodeDeployDeploymentGroup(self.function_logical_id + 'DeploymentGroup') diff --git a/tests/translator/output/aws-cn/function_with_deployment_preference.json b/tests/translator/output/aws-cn/function_with_deployment_preference.json index 81771bafc..d61057286 100644 --- a/tests/translator/output/aws-cn/function_with_deployment_preference.json +++ b/tests/translator/output/aws-cn/function_with_deployment_preference.json @@ -24,7 +24,7 @@ "Fn::Sub": [ "CodeDeployDefault.Lambda${ConfigName}", { - "ConfigName": "Linear10PercentEvery3Minute" + "ConfigName": "Linear10PercentEvery3Minutes" } ] }, diff --git a/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations.json b/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations.json index 9600d5271..6fed70de7 100644 --- a/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations.json +++ b/tests/translator/output/aws-cn/function_with_deployment_preference_multiple_combinations.json @@ -148,7 +148,7 @@ "Fn::Sub": [ "CodeDeployDefault.Lambda${ConfigName}", { - "ConfigName": "Linear10PercentEvery2Minute" + "ConfigName": "Linear10PercentEvery2Minutes" } ] }, diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_preference.json b/tests/translator/output/aws-us-gov/function_with_deployment_preference.json index f02c6a1e4..a16ba0a1d 100644 --- a/tests/translator/output/aws-us-gov/function_with_deployment_preference.json +++ b/tests/translator/output/aws-us-gov/function_with_deployment_preference.json @@ -24,7 +24,7 @@ "Fn::Sub": [ "CodeDeployDefault.Lambda${ConfigName}", { - "ConfigName": "Linear10PercentEvery3Minute" + "ConfigName": "Linear10PercentEvery3Minutes" } ] }, diff --git a/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations.json b/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations.json index a3cd8f87b..ab1cdabb4 100644 --- a/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations.json +++ b/tests/translator/output/aws-us-gov/function_with_deployment_preference_multiple_combinations.json @@ -148,7 +148,7 @@ "Fn::Sub": [ "CodeDeployDefault.Lambda${ConfigName}", { - "ConfigName": "Linear10PercentEvery2Minute" + "ConfigName": "Linear10PercentEvery2Minutes" } ] }, diff --git a/tests/translator/output/function_with_deployment_preference.json b/tests/translator/output/function_with_deployment_preference.json index bfdbb7bdc..75ba69524 100644 --- a/tests/translator/output/function_with_deployment_preference.json +++ b/tests/translator/output/function_with_deployment_preference.json @@ -24,7 +24,7 @@ "Fn::Sub": [ "CodeDeployDefault.Lambda${ConfigName}", { - "ConfigName": "Linear10PercentEvery3Minute" + "ConfigName": "Linear10PercentEvery3Minutes" } ] }, diff --git a/tests/translator/output/function_with_deployment_preference_multiple_combinations.json b/tests/translator/output/function_with_deployment_preference_multiple_combinations.json index 925b0ceb5..774d48121 100644 --- a/tests/translator/output/function_with_deployment_preference_multiple_combinations.json +++ b/tests/translator/output/function_with_deployment_preference_multiple_combinations.json @@ -148,7 +148,7 @@ "Fn::Sub": [ "CodeDeployDefault.Lambda${ConfigName}", { - "ConfigName": "Linear10PercentEvery2Minute" + "ConfigName": "Linear10PercentEvery2Minutes" } ] }, From d7f0089ebe698012bea1e1edf52e0caaf886fc44 Mon Sep 17 00:00:00 2001 From: Manvendra Singh Date: Sat, 9 Mar 2019 18:21:40 +0530 Subject: [PATCH 2/7] Added end to end test for referencing custom codedeploy configurations. --- ...stom_codedeploy_deployment_preference.yaml | 10 ++ ...stom_codedeploy_deployment_preference.json | 142 ++++++++++++++++++ ...stom_codedeploy_deployment_preference.json | 142 ++++++++++++++++++ ...stom_codedeploy_deployment_preference.json | 142 ++++++++++++++++++ 4 files changed, 436 insertions(+) create mode 100644 tests/translator/input/function_with_custom_codedeploy_deployment_preference.yaml create mode 100644 tests/translator/output/aws-cn/function_with_custom_codedeploy_deployment_preference.json create mode 100644 tests/translator/output/aws-us-gov/function_with_custom_codedeploy_deployment_preference.json create mode 100644 tests/translator/output/function_with_custom_codedeploy_deployment_preference.json diff --git a/tests/translator/input/function_with_custom_codedeploy_deployment_preference.yaml b/tests/translator/input/function_with_custom_codedeploy_deployment_preference.yaml new file mode 100644 index 000000000..25846f700 --- /dev/null +++ b/tests/translator/input/function_with_custom_codedeploy_deployment_preference.yaml @@ -0,0 +1,10 @@ +Resources: + MinimalFunction: + Type: 'AWS::Serverless::Function' + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: hello.handler + Runtime: python2.7 + AutoPublishAlias: live + DeploymentPreference: + Type: TestDeploymentConfiguration \ No newline at end of file diff --git a/tests/translator/output/aws-cn/function_with_custom_codedeploy_deployment_preference.json b/tests/translator/output/aws-cn/function_with_custom_codedeploy_deployment_preference.json new file mode 100644 index 000000000..d0d846442 --- /dev/null +++ b/tests/translator/output/aws-cn/function_with_custom_codedeploy_deployment_preference.json @@ -0,0 +1,142 @@ +{ + "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": "TestDeploymentConfiguration", + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + } + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + } + } + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + }, + "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" + } + } + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "MinimalFunction" + }, + "Name": "live" + } + } + } + } \ No newline at end of file diff --git a/tests/translator/output/aws-us-gov/function_with_custom_codedeploy_deployment_preference.json b/tests/translator/output/aws-us-gov/function_with_custom_codedeploy_deployment_preference.json new file mode 100644 index 000000000..d0d846442 --- /dev/null +++ b/tests/translator/output/aws-us-gov/function_with_custom_codedeploy_deployment_preference.json @@ -0,0 +1,142 @@ +{ + "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": "TestDeploymentConfiguration", + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + } + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + } + } + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + }, + "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" + } + } + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "MinimalFunction" + }, + "Name": "live" + } + } + } + } \ No newline at end of file diff --git a/tests/translator/output/function_with_custom_codedeploy_deployment_preference.json b/tests/translator/output/function_with_custom_codedeploy_deployment_preference.json new file mode 100644 index 000000000..d0d846442 --- /dev/null +++ b/tests/translator/output/function_with_custom_codedeploy_deployment_preference.json @@ -0,0 +1,142 @@ +{ + "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": "TestDeploymentConfiguration", + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + } + }, + "MinimalFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + } + } + }, + "MinimalFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "S3Bucket": "sam-demo-bucket", + "S3Key": "hello.zip" + }, + "Handler": "hello.handler", + "Role": { + "Fn::GetAtt": [ + "MinimalFunctionRole", + "Arn" + ] + }, + "Runtime": "python2.7", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + }, + "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" + } + } + }, + "MinimalFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "MinimalFunctionDeploymentGroup" + } + } + }, + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "MinimalFunctionVersion640128d35d", + "Version" + ] + }, + "FunctionName": { + "Ref": "MinimalFunction" + }, + "Name": "live" + } + } + } + } \ No newline at end of file From 897a54afdb400f77884e753123ca75feb60e6c54 Mon Sep 17 00:00:00 2001 From: Manvendra singh Date: Sun, 24 Mar 2019 04:25:53 +0530 Subject: [PATCH 3/7] Added support for conditional deployments type while specifying a custom codedeploy configuration name --- .../deployment_preference_collection.py | 30 ++- ...ional_codedeploy_deployment_preference.yml | 17 ++ .../test_deployment_preference_collection.py | 35 +++- ...onal_codedeploy_deployment_preference.json | 192 ++++++++++++++++++ ...onal_codedeploy_deployment_preference.json | 192 ++++++++++++++++++ ...onal_codedeploy_deployment_preference.json | 192 ++++++++++++++++++ 6 files changed, 652 insertions(+), 6 deletions(-) create mode 100644 tests/translator/input/function_with_custom_conditional_codedeploy_deployment_preference.yml create mode 100644 tests/translator/output/aws-cn/function_with_custom_conditional_codedeploy_deployment_preference.json create mode 100644 tests/translator/output/aws-us-gov/function_with_custom_conditional_codedeploy_deployment_preference.json create mode 100644 tests/translator/output/function_with_custom_conditional_codedeploy_deployment_preference.json diff --git a/samtranslator/model/preferences/deployment_preference_collection.py b/samtranslator/model/preferences/deployment_preference_collection.py index 37a3df137..4e81627a7 100644 --- a/samtranslator/model/preferences/deployment_preference_collection.py +++ b/samtranslator/model/preferences/deployment_preference_collection.py @@ -120,12 +120,19 @@ def deployment_group(self, function_logical_id): 'Events': ['DEPLOYMENT_FAILURE', 'DEPLOYMENT_STOP_ON_ALARM', 'DEPLOYMENT_STOP_ON_REQUEST']} + if isinstance(deployment_preference.deployment_type, dict): + modified_deployment_type = self.__handle_intrinsic_function_in_deployment_type( + deployment_preference.deployment_type) - if deployment_preference.deployment_type in CODEDEPLOY_PREDEFINED_CONFIGURATIONS_LIST: - deployment_group.DeploymentConfigName = fnSub("CodeDeployDefault.Lambda${ConfigName}", - {"ConfigName": deployment_preference.deployment_type}) - else: + # Using replace method to modify the namedtuple + deployment_preference._replace(deployment_type=modified_deployment_type) deployment_group.DeploymentConfigName = deployment_preference.deployment_type + else: + if deployment_preference.deployment_type in CODEDEPLOY_PREDEFINED_CONFIGURATIONS_LIST: + deployment_group.DeploymentConfigName = fnSub("CodeDeployDefault.Lambda${ConfigName}", + {"ConfigName": deployment_preference.deployment_type}) + else: + deployment_group.DeploymentConfigName = deployment_preference.deployment_type deployment_group.DeploymentStyle = {'DeploymentType': 'BLUE_GREEN', 'DeploymentOption': 'WITH_TRAFFIC_CONTROL'} @@ -136,6 +143,21 @@ def deployment_group(self, function_logical_id): return deployment_group + def __handle_intrinsic_function_in_deployment_type(self, deployment_type): + for key, value in deployment_type.items(): + deployment_type[key] = [self.__process_element(element) for element in value] + return deployment_type + + def __process_element(self, element): + if element in CODEDEPLOY_PREDEFINED_CONFIGURATIONS_LIST: + return fnSub("CodeDeployDefault.Lambda${ConfigName}", {"ConfigName": element}) + elif isinstance(element, dict): + if element.keys()[0] == "Fn::If": + return self.__handle_intrinsic_function_in_deployment_type(element) + return element + else: + return element + def update_policy(self, function_logical_id): deployment_preference = self.get(function_logical_id) diff --git a/tests/translator/input/function_with_custom_conditional_codedeploy_deployment_preference.yml b/tests/translator/input/function_with_custom_conditional_codedeploy_deployment_preference.yml new file mode 100644 index 000000000..c587ad8a2 --- /dev/null +++ b/tests/translator/input/function_with_custom_conditional_codedeploy_deployment_preference.yml @@ -0,0 +1,17 @@ +AWSTemplateFormatVersion : '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Parameters: + EnvType: + Default: dev +Conditions: + IsDevEnv: !Equals [!Ref EnvType, dev] + IsDevEnv2: !Equals [!Ref EnvType, prod] +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function + Properties: + Handler: index.handler + Runtime: nodejs8.10 + AutoPublishAlias: live + DeploymentPreference: + Type: !If [IsDevEnv, !If [IsDevEnv2, AllAtOnce, TestCustomDeploymentConfig], Canary10Percent15Minutes] \ No newline at end of file diff --git a/tests/translator/model/preferences/test_deployment_preference_collection.py b/tests/translator/model/preferences/test_deployment_preference_collection.py index bc0464f76..ae3404fc5 100644 --- a/tests/translator/model/preferences/test_deployment_preference_collection.py +++ b/tests/translator/model/preferences/test_deployment_preference_collection.py @@ -94,7 +94,7 @@ def test_deployment_preference_with_codedeploy_custom_configuration(self): @patch('boto3.session.Session.region_name', 'ap-southeast-1') def test_deployment_preference_with_codedeploy_predifined_configuration(self): deployment_type = "Canary10Percent5Minutes" - expected_deployment_configname = { + expected_deployment_config_name = { 'Fn::Sub': [ 'CodeDeployDefault.Lambda${ConfigName}', { @@ -107,7 +107,38 @@ def test_deployment_preference_with_codedeploy_predifined_configuration(self): deployment_group = deployment_preference_collection.deployment_group(self.function_logical_id) print(deployment_group.DeploymentConfigName) - self.assertEqual(expected_deployment_configname, deployment_group.DeploymentConfigName) + self.assertEqual(expected_deployment_config_name, deployment_group.DeploymentConfigName) + + @patch('boto3.session.Session.region_name', 'ap-southeast-1') + def test_deployment_preference_with_conditional_custom_configuration(self): + deployment_type = {'Fn::If': ['IsDevEnv', {'Fn::If': + ['IsDevEnv1', 'AllAtOnce', 'TestDeploymentConfiguration']}, + 'Canary10Percent15Minutes']} + + expected_deployment_config_name = {'Fn::If': + ['IsDevEnv', {'Fn::If': + ['IsDevEnv1', {'Fn::Sub': [ + 'CodeDeployDefault.Lambda${ConfigName}', + { + 'ConfigName': 'AllAtOnce' + } + ] + }, + 'TestDeploymentConfiguration']}, + {'Fn::Sub': [ + 'CodeDeployDefault.Lambda${ConfigName}', + { + 'ConfigName': 'Canary10Percent15Minutes' + } + ] + } + ] + } + deployment_preference_collection = DeploymentPreferenceCollection() + deployment_preference_collection.add(self.function_logical_id, {'Type': deployment_type}) + deployment_group = deployment_preference_collection.deployment_group(self.function_logical_id) + print(deployment_group.DeploymentConfigName) + self.assertEqual(expected_deployment_config_name, deployment_group.DeploymentConfigName) @patch('boto3.session.Session.region_name', 'ap-southeast-1') def test_deployment_group_with_all_parameters(self): diff --git a/tests/translator/output/aws-cn/function_with_custom_conditional_codedeploy_deployment_preference.json b/tests/translator/output/aws-cn/function_with_custom_conditional_codedeploy_deployment_preference.json new file mode 100644 index 000000000..ed0c47981 --- /dev/null +++ b/tests/translator/output/aws-cn/function_with_custom_conditional_codedeploy_deployment_preference.json @@ -0,0 +1,192 @@ +{ + "AWSTemplateFormatVersion": "2010-09-09", + "Conditions": { + "IsDevEnv2": { + "Fn::Equals": [ + { + "Ref": "EnvType" + }, + "prod" + ] + }, + "IsDevEnv": { + "Fn::Equals": [ + { + "Ref": "EnvType" + }, + "dev" + ] + } + }, + "Resources": { + "HelloWorldFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + } + } + }, + "HelloWorldFunctionVersion1bc9589ea5": { + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "HelloWorldFunction" + } + } + }, + "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" + ] + } + } + ] + } + } + }, + "HelloWorldFunctionDeploymentGroup": { + "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::If": [ + "IsDevEnv", + { + "Fn::If": [ + "IsDevEnv2", + { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "AllAtOnce" + } + ] + }, + "TestCustomDeploymentConfig" + ] + }, + { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Canary10Percent15Minutes" + } + ] + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + } + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, + "HelloWorldFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "HelloWorldFunctionDeploymentGroup" + } + } + }, + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "HelloWorldFunctionVersion1bc9589ea5", + "Version" + ] + }, + "FunctionName": { + "Ref": "HelloWorldFunction" + }, + "Name": "live" + } + }, + "HelloWorldFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "index.handler", + "Code": { + "S3Bucket": "manvendra-sam-test", + "S3Key": "b9cfacca4a48ef2097bb36f980cc4481" + }, + "Role": { + "Fn::GetAtt": [ + "HelloWorldFunctionRole", + "Arn" + ] + }, + "Runtime": "nodejs8.10", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + } + }, + "Parameters": { + "EnvType": { + "Default": "dev" + } + } +} \ No newline at end of file diff --git a/tests/translator/output/aws-us-gov/function_with_custom_conditional_codedeploy_deployment_preference.json b/tests/translator/output/aws-us-gov/function_with_custom_conditional_codedeploy_deployment_preference.json new file mode 100644 index 000000000..ed0c47981 --- /dev/null +++ b/tests/translator/output/aws-us-gov/function_with_custom_conditional_codedeploy_deployment_preference.json @@ -0,0 +1,192 @@ +{ + "AWSTemplateFormatVersion": "2010-09-09", + "Conditions": { + "IsDevEnv2": { + "Fn::Equals": [ + { + "Ref": "EnvType" + }, + "prod" + ] + }, + "IsDevEnv": { + "Fn::Equals": [ + { + "Ref": "EnvType" + }, + "dev" + ] + } + }, + "Resources": { + "HelloWorldFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + } + } + }, + "HelloWorldFunctionVersion1bc9589ea5": { + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "HelloWorldFunction" + } + } + }, + "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" + ] + } + } + ] + } + } + }, + "HelloWorldFunctionDeploymentGroup": { + "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::If": [ + "IsDevEnv", + { + "Fn::If": [ + "IsDevEnv2", + { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "AllAtOnce" + } + ] + }, + "TestCustomDeploymentConfig" + ] + }, + { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Canary10Percent15Minutes" + } + ] + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + } + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, + "HelloWorldFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "HelloWorldFunctionDeploymentGroup" + } + } + }, + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "HelloWorldFunctionVersion1bc9589ea5", + "Version" + ] + }, + "FunctionName": { + "Ref": "HelloWorldFunction" + }, + "Name": "live" + } + }, + "HelloWorldFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "index.handler", + "Code": { + "S3Bucket": "manvendra-sam-test", + "S3Key": "b9cfacca4a48ef2097bb36f980cc4481" + }, + "Role": { + "Fn::GetAtt": [ + "HelloWorldFunctionRole", + "Arn" + ] + }, + "Runtime": "nodejs8.10", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + } + }, + "Parameters": { + "EnvType": { + "Default": "dev" + } + } +} \ No newline at end of file diff --git a/tests/translator/output/function_with_custom_conditional_codedeploy_deployment_preference.json b/tests/translator/output/function_with_custom_conditional_codedeploy_deployment_preference.json new file mode 100644 index 000000000..ed0c47981 --- /dev/null +++ b/tests/translator/output/function_with_custom_conditional_codedeploy_deployment_preference.json @@ -0,0 +1,192 @@ +{ + "AWSTemplateFormatVersion": "2010-09-09", + "Conditions": { + "IsDevEnv2": { + "Fn::Equals": [ + { + "Ref": "EnvType" + }, + "prod" + ] + }, + "IsDevEnv": { + "Fn::Equals": [ + { + "Ref": "EnvType" + }, + "dev" + ] + } + }, + "Resources": { + "HelloWorldFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], + "AssumeRolePolicyDocument": { + "Version": "2012-10-17", + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ] + } + } + }, + "HelloWorldFunctionVersion1bc9589ea5": { + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", + "Properties": { + "FunctionName": { + "Ref": "HelloWorldFunction" + } + } + }, + "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" + ] + } + } + ] + } + } + }, + "HelloWorldFunctionDeploymentGroup": { + "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::If": [ + "IsDevEnv", + { + "Fn::If": [ + "IsDevEnv2", + { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "AllAtOnce" + } + ] + }, + "TestCustomDeploymentConfig" + ] + }, + { + "Fn::Sub": [ + "CodeDeployDefault.Lambda${ConfigName}", + { + "ConfigName": "Canary10Percent15Minutes" + } + ] + } + ] + }, + "DeploymentStyle": { + "DeploymentType": "BLUE_GREEN", + "DeploymentOption": "WITH_TRAFFIC_CONTROL" + } + } + }, + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, + "HelloWorldFunctionAliaslive": { + "Type": "AWS::Lambda::Alias", + "UpdatePolicy": { + "CodeDeployLambdaAliasUpdate": { + "ApplicationName": { + "Ref": "ServerlessDeploymentApplication" + }, + "DeploymentGroupName": { + "Ref": "HelloWorldFunctionDeploymentGroup" + } + } + }, + "Properties": { + "FunctionVersion": { + "Fn::GetAtt": [ + "HelloWorldFunctionVersion1bc9589ea5", + "Version" + ] + }, + "FunctionName": { + "Ref": "HelloWorldFunction" + }, + "Name": "live" + } + }, + "HelloWorldFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Handler": "index.handler", + "Code": { + "S3Bucket": "manvendra-sam-test", + "S3Key": "b9cfacca4a48ef2097bb36f980cc4481" + }, + "Role": { + "Fn::GetAtt": [ + "HelloWorldFunctionRole", + "Arn" + ] + }, + "Runtime": "nodejs8.10", + "Tags": [ + { + "Value": "SAM", + "Key": "lambda:createdBy" + } + ] + } + } + }, + "Parameters": { + "EnvType": { + "Default": "dev" + } + } +} \ No newline at end of file From 06f7a83324b73a7cef589604a556acf9605ad840 Mon Sep 17 00:00:00 2001 From: Manvendra singh Date: Sun, 24 Mar 2019 04:50:08 +0530 Subject: [PATCH 4/7] Added python3 compatibility while accessin dict_keys --- .../model/preferences/deployment_preference_collection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samtranslator/model/preferences/deployment_preference_collection.py b/samtranslator/model/preferences/deployment_preference_collection.py index 4e81627a7..9996cbd9b 100644 --- a/samtranslator/model/preferences/deployment_preference_collection.py +++ b/samtranslator/model/preferences/deployment_preference_collection.py @@ -152,7 +152,7 @@ def __process_element(self, element): if element in CODEDEPLOY_PREDEFINED_CONFIGURATIONS_LIST: return fnSub("CodeDeployDefault.Lambda${ConfigName}", {"ConfigName": element}) elif isinstance(element, dict): - if element.keys()[0] == "Fn::If": + if list(element)[0] == "Fn::If": return self.__handle_intrinsic_function_in_deployment_type(element) return element else: From da00339b605a0e7e366e1ee3848154c175fd22f9 Mon Sep 17 00:00:00 2001 From: Manvendra singh Date: Thu, 4 Apr 2019 00:25:39 +0530 Subject: [PATCH 5/7] Added recursive update for custom deployments type --- .../deployment_preference_collection.py | 45 ++++++++----------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/samtranslator/model/preferences/deployment_preference_collection.py b/samtranslator/model/preferences/deployment_preference_collection.py index 9996cbd9b..5720a2f2f 100644 --- a/samtranslator/model/preferences/deployment_preference_collection.py +++ b/samtranslator/model/preferences/deployment_preference_collection.py @@ -2,9 +2,10 @@ from samtranslator.model.codedeploy import CodeDeployApplication from samtranslator.model.codedeploy import CodeDeployDeploymentGroup from samtranslator.model.iam import IAMRole -from samtranslator.model.intrinsics import fnSub +from samtranslator.model.intrinsics import fnSub, is_instrinsic from samtranslator.model.update_policy import UpdatePolicy from samtranslator.translator.arn_generator import ArnGenerator +import copy CODE_DEPLOY_SERVICE_ROLE_LOGICAL_ID = 'CodeDeployServiceRole' CODEDEPLOY_APPLICATION_LOGICAL_ID = 'ServerlessDeploymentApplication' @@ -120,19 +121,9 @@ def deployment_group(self, function_logical_id): 'Events': ['DEPLOYMENT_FAILURE', 'DEPLOYMENT_STOP_ON_ALARM', 'DEPLOYMENT_STOP_ON_REQUEST']} - if isinstance(deployment_preference.deployment_type, dict): - modified_deployment_type = self.__handle_intrinsic_function_in_deployment_type( - deployment_preference.deployment_type) - - # Using replace method to modify the namedtuple - deployment_preference._replace(deployment_type=modified_deployment_type) - deployment_group.DeploymentConfigName = deployment_preference.deployment_type - else: - if deployment_preference.deployment_type in CODEDEPLOY_PREDEFINED_CONFIGURATIONS_LIST: - deployment_group.DeploymentConfigName = fnSub("CodeDeployDefault.Lambda${ConfigName}", - {"ConfigName": deployment_preference.deployment_type}) - else: - deployment_group.DeploymentConfigName = deployment_preference.deployment_type + + deployment_group.DeploymentConfigName = self._replace_deployment_types(copy.deepcopy( + deployment_preference.deployment_type)) deployment_group.DeploymentStyle = {'DeploymentType': 'BLUE_GREEN', 'DeploymentOption': 'WITH_TRAFFIC_CONTROL'} @@ -143,20 +134,20 @@ def deployment_group(self, function_logical_id): return deployment_group - def __handle_intrinsic_function_in_deployment_type(self, deployment_type): - for key, value in deployment_type.items(): - deployment_type[key] = [self.__process_element(element) for element in value] - return deployment_type - - def __process_element(self, element): - if element in CODEDEPLOY_PREDEFINED_CONFIGURATIONS_LIST: - return fnSub("CodeDeployDefault.Lambda${ConfigName}", {"ConfigName": element}) - elif isinstance(element, dict): - if list(element)[0] == "Fn::If": - return self.__handle_intrinsic_function_in_deployment_type(element) - return element + def _replace_deployment_types(self, value): + if isinstance(value, list): + for i in range(len(value)): + value[i] = self._replace_deployment_types(value[i]) + return value + elif is_instrinsic(value): + for (k, v) in value.items(): + value[k] = self._replace_deployment_types(v) + return value else: - return element + if value in CODEDEPLOY_PREDEFINED_CONFIGURATIONS_LIST: + return fnSub("CodeDeployDefault.Lambda${ConfigName}", {"ConfigName": value}) + return value + def update_policy(self, function_logical_id): deployment_preference = self.get(function_logical_id) From ec862f6bcc5db7275802c53a021cd96c84176c1b Mon Sep 17 00:00:00 2001 From: Manvendra singh Date: Thu, 4 Apr 2019 13:03:36 +0530 Subject: [PATCH 6/7] Removed formatting issues --- docs/safe_lambda_deployments.rst | 3 ++- .../model/preferences/deployment_preference_collection.py | 5 ++--- .../preferences/test_deployment_preference_collection.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/safe_lambda_deployments.rst b/docs/safe_lambda_deployments.rst index c6c4b1454..539d12424 100644 --- a/docs/safe_lambda_deployments.rst +++ b/docs/safe_lambda_deployments.rst @@ -223,7 +223,8 @@ They work as follows: - **AllAtOnce**: This is an instant shifting of 100% of traffic to new version. This is useful if you want to run run pre/post hooks but don't want a gradual deployment. If you have a pipeline, you can set Beta/Gamma stages to deploy instantly because the speed of deployments matter more than safety here. - +- **Custom**: Aside from Above mentioned Configurations, Custom Codedeploy configuration are also supported. + (Example. Type: CustomCodeDeployConfiguration) PreTraffic & PostTraffic Hooks ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/samtranslator/model/preferences/deployment_preference_collection.py b/samtranslator/model/preferences/deployment_preference_collection.py index 5720a2f2f..1c67e8b11 100644 --- a/samtranslator/model/preferences/deployment_preference_collection.py +++ b/samtranslator/model/preferences/deployment_preference_collection.py @@ -5,7 +5,7 @@ from samtranslator.model.intrinsics import fnSub, is_instrinsic from samtranslator.model.update_policy import UpdatePolicy from samtranslator.translator.arn_generator import ArnGenerator -import copy +import copy CODE_DEPLOY_SERVICE_ROLE_LOGICAL_ID = 'CodeDeployServiceRole' CODEDEPLOY_APPLICATION_LOGICAL_ID = 'ServerlessDeploymentApplication' @@ -121,7 +121,7 @@ def deployment_group(self, function_logical_id): 'Events': ['DEPLOYMENT_FAILURE', 'DEPLOYMENT_STOP_ON_ALARM', 'DEPLOYMENT_STOP_ON_REQUEST']} - + deployment_group.DeploymentConfigName = self._replace_deployment_types(copy.deepcopy( deployment_preference.deployment_type)) @@ -148,7 +148,6 @@ def _replace_deployment_types(self, value): return fnSub("CodeDeployDefault.Lambda${ConfigName}", {"ConfigName": value}) return value - def update_policy(self, function_logical_id): deployment_preference = self.get(function_logical_id) diff --git a/tests/translator/model/preferences/test_deployment_preference_collection.py b/tests/translator/model/preferences/test_deployment_preference_collection.py index ae3404fc5..4415c01c2 100644 --- a/tests/translator/model/preferences/test_deployment_preference_collection.py +++ b/tests/translator/model/preferences/test_deployment_preference_collection.py @@ -122,7 +122,7 @@ def test_deployment_preference_with_conditional_custom_configuration(self): { 'ConfigName': 'AllAtOnce' } - ] + ] }, 'TestDeploymentConfiguration']}, {'Fn::Sub': [ From 0b67e7aca16a0bd074f82d4c4dcff64a1129849f Mon Sep 17 00:00:00 2001 From: Keeton Hodgson Date: Tue, 16 Apr 2019 10:45:50 -0700 Subject: [PATCH 7/7] chore: fix test failures --- ...nal_codedeploy_deployment_preference.yaml} | 4 +- ...stom_codedeploy_deployment_preference.json | 6 +- ...onal_codedeploy_deployment_preference.json | 143 +++++++++--------- ...stom_codedeploy_deployment_preference.json | 6 +- ...onal_codedeploy_deployment_preference.json | 143 +++++++++--------- ...onal_codedeploy_deployment_preference.json | 139 ++++++++--------- tests/translator/test_translator.py | 2 + 7 files changed, 225 insertions(+), 218 deletions(-) rename tests/translator/input/{function_with_custom_conditional_codedeploy_deployment_preference.yml => function_with_custom_conditional_codedeploy_deployment_preference.yaml} (83%) diff --git a/tests/translator/input/function_with_custom_conditional_codedeploy_deployment_preference.yml b/tests/translator/input/function_with_custom_conditional_codedeploy_deployment_preference.yaml similarity index 83% rename from tests/translator/input/function_with_custom_conditional_codedeploy_deployment_preference.yml rename to tests/translator/input/function_with_custom_conditional_codedeploy_deployment_preference.yaml index c587ad8a2..e1a53328e 100644 --- a/tests/translator/input/function_with_custom_conditional_codedeploy_deployment_preference.yml +++ b/tests/translator/input/function_with_custom_conditional_codedeploy_deployment_preference.yaml @@ -3,6 +3,7 @@ Transform: AWS::Serverless-2016-10-31 Parameters: EnvType: Default: dev + Type: String Conditions: IsDevEnv: !Equals [!Ref EnvType, dev] IsDevEnv2: !Equals [!Ref EnvType, prod] @@ -12,6 +13,7 @@ Resources: Properties: Handler: index.handler Runtime: nodejs8.10 + CodeUri: s3://bucket/key AutoPublishAlias: live DeploymentPreference: - Type: !If [IsDevEnv, !If [IsDevEnv2, AllAtOnce, TestCustomDeploymentConfig], Canary10Percent15Minutes] \ No newline at end of file + Type: !If [IsDevEnv, !If [IsDevEnv2, AllAtOnce, TestCustomDeploymentConfig], Canary10Percent15Minutes] diff --git a/tests/translator/output/aws-cn/function_with_custom_codedeploy_deployment_preference.json b/tests/translator/output/aws-cn/function_with_custom_codedeploy_deployment_preference.json index d0d846442..e15c61776 100644 --- a/tests/translator/output/aws-cn/function_with_custom_codedeploy_deployment_preference.json +++ b/tests/translator/output/aws-cn/function_with_custom_codedeploy_deployment_preference.json @@ -31,7 +31,7 @@ "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ - "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" ], "AssumeRolePolicyDocument": { "Version": "2012-10-17", @@ -78,7 +78,7 @@ "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ - "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + "arn:aws-cn:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" ], "AssumeRolePolicyDocument": { "Version": "2012-10-17", @@ -139,4 +139,4 @@ } } } - } \ No newline at end of file + } diff --git a/tests/translator/output/aws-cn/function_with_custom_conditional_codedeploy_deployment_preference.json b/tests/translator/output/aws-cn/function_with_custom_conditional_codedeploy_deployment_preference.json index ed0c47981..e78aeb5bf 100644 --- a/tests/translator/output/aws-cn/function_with_custom_conditional_codedeploy_deployment_preference.json +++ b/tests/translator/output/aws-cn/function_with_custom_conditional_codedeploy_deployment_preference.json @@ -1,38 +1,50 @@ { - "AWSTemplateFormatVersion": "2010-09-09", + "AWSTemplateFormatVersion": "2010-09-09", + "Parameters": { + "EnvType": { + "Default": "dev", + "Type": "String" + } + }, "Conditions": { "IsDevEnv2": { "Fn::Equals": [ { "Ref": "EnvType" - }, + }, "prod" ] - }, + }, "IsDevEnv": { "Fn::Equals": [ { "Ref": "EnvType" - }, + }, "dev" ] } - }, + }, "Resources": { + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, "HelloWorldFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ - "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + "arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -42,30 +54,21 @@ ] } } - }, - "HelloWorldFunctionVersion1bc9589ea5": { - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", - "Properties": { - "FunctionName": { - "Ref": "HelloWorldFunction" - } - } - }, + }, "CodeDeployServiceRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ - "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" - ], + "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" @@ -75,118 +78,116 @@ ] } } - }, + }, "HelloWorldFunctionDeploymentGroup": { - "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::If": [ - "IsDevEnv", + "IsDevEnv", { "Fn::If": [ - "IsDevEnv2", + "IsDevEnv2", { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "AllAtOnce" } ] - }, + }, "TestCustomDeploymentConfig" ] - }, + }, { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Canary10Percent15Minutes" } ] } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } } - }, - "ServerlessDeploymentApplication": { - "Type": "AWS::CodeDeploy::Application", + }, + "HelloWorldFunctionVersionfb53d5c2e6": { + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { - "ComputePlatform": "Lambda" + "FunctionName": { + "Ref": "HelloWorldFunction" + } } - }, + }, "HelloWorldFunctionAliaslive": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "HelloWorldFunctionDeploymentGroup" } } - }, + }, "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "HelloWorldFunctionVersion1bc9589ea5", + "HelloWorldFunctionVersionfb53d5c2e6", "Version" ] - }, + }, "FunctionName": { "Ref": "HelloWorldFunction" - }, + }, "Name": "live" } - }, + }, "HelloWorldFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "index.handler", + "Handler": "index.handler", "Code": { - "S3Bucket": "manvendra-sam-test", - "S3Key": "b9cfacca4a48ef2097bb36f980cc4481" - }, + "S3Bucket": "bucket", + "S3Key": "key" + }, "Role": { "Fn::GetAtt": [ - "HelloWorldFunctionRole", + "HelloWorldFunctionRole", "Arn" ] - }, - "Runtime": "nodejs8.10", + }, + "Runtime": "nodejs8.10", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] } } - }, - "Parameters": { - "EnvType": { - "Default": "dev" - } } -} \ No newline at end of file +} diff --git a/tests/translator/output/aws-us-gov/function_with_custom_codedeploy_deployment_preference.json b/tests/translator/output/aws-us-gov/function_with_custom_codedeploy_deployment_preference.json index d0d846442..60928c399 100644 --- a/tests/translator/output/aws-us-gov/function_with_custom_codedeploy_deployment_preference.json +++ b/tests/translator/output/aws-us-gov/function_with_custom_codedeploy_deployment_preference.json @@ -31,7 +31,7 @@ "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ - "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" ], "AssumeRolePolicyDocument": { "Version": "2012-10-17", @@ -78,7 +78,7 @@ "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ - "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" + "arn:aws-us-gov:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" ], "AssumeRolePolicyDocument": { "Version": "2012-10-17", @@ -139,4 +139,4 @@ } } } - } \ No newline at end of file + } diff --git a/tests/translator/output/aws-us-gov/function_with_custom_conditional_codedeploy_deployment_preference.json b/tests/translator/output/aws-us-gov/function_with_custom_conditional_codedeploy_deployment_preference.json index ed0c47981..c2da9a1fd 100644 --- a/tests/translator/output/aws-us-gov/function_with_custom_conditional_codedeploy_deployment_preference.json +++ b/tests/translator/output/aws-us-gov/function_with_custom_conditional_codedeploy_deployment_preference.json @@ -1,38 +1,50 @@ { - "AWSTemplateFormatVersion": "2010-09-09", + "AWSTemplateFormatVersion": "2010-09-09", + "Parameters": { + "EnvType": { + "Default": "dev", + "Type": "String" + } + }, "Conditions": { "IsDevEnv2": { "Fn::Equals": [ { "Ref": "EnvType" - }, + }, "prod" ] - }, + }, "IsDevEnv": { "Fn::Equals": [ { "Ref": "EnvType" - }, + }, "dev" ] } - }, + }, "Resources": { + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, "HelloWorldFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ - "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + "arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ], "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -42,30 +54,21 @@ ] } } - }, - "HelloWorldFunctionVersion1bc9589ea5": { - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", - "Properties": { - "FunctionName": { - "Ref": "HelloWorldFunction" - } - } - }, + }, "CodeDeployServiceRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ - "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda" - ], + "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" @@ -75,118 +78,116 @@ ] } } - }, + }, "HelloWorldFunctionDeploymentGroup": { - "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::If": [ - "IsDevEnv", + "IsDevEnv", { "Fn::If": [ - "IsDevEnv2", + "IsDevEnv2", { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "AllAtOnce" } ] - }, + }, "TestCustomDeploymentConfig" ] - }, + }, { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Canary10Percent15Minutes" } ] } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } } - }, - "ServerlessDeploymentApplication": { - "Type": "AWS::CodeDeploy::Application", + }, + "HelloWorldFunctionVersionfb53d5c2e6": { + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { - "ComputePlatform": "Lambda" + "FunctionName": { + "Ref": "HelloWorldFunction" + } } - }, + }, "HelloWorldFunctionAliaslive": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "HelloWorldFunctionDeploymentGroup" } } - }, + }, "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "HelloWorldFunctionVersion1bc9589ea5", + "HelloWorldFunctionVersionfb53d5c2e6", "Version" ] - }, + }, "FunctionName": { "Ref": "HelloWorldFunction" - }, + }, "Name": "live" } - }, + }, "HelloWorldFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "index.handler", + "Handler": "index.handler", "Code": { - "S3Bucket": "manvendra-sam-test", - "S3Key": "b9cfacca4a48ef2097bb36f980cc4481" - }, + "S3Bucket": "bucket", + "S3Key": "key" + }, "Role": { "Fn::GetAtt": [ - "HelloWorldFunctionRole", + "HelloWorldFunctionRole", "Arn" ] - }, - "Runtime": "nodejs8.10", + }, + "Runtime": "nodejs8.10", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] } } - }, - "Parameters": { - "EnvType": { - "Default": "dev" - } } -} \ No newline at end of file +} diff --git a/tests/translator/output/function_with_custom_conditional_codedeploy_deployment_preference.json b/tests/translator/output/function_with_custom_conditional_codedeploy_deployment_preference.json index ed0c47981..413b8913d 100644 --- a/tests/translator/output/function_with_custom_conditional_codedeploy_deployment_preference.json +++ b/tests/translator/output/function_with_custom_conditional_codedeploy_deployment_preference.json @@ -1,38 +1,50 @@ { - "AWSTemplateFormatVersion": "2010-09-09", + "AWSTemplateFormatVersion": "2010-09-09", + "Parameters": { + "EnvType": { + "Default": "dev", + "Type": "String" + } + }, "Conditions": { "IsDevEnv2": { "Fn::Equals": [ { "Ref": "EnvType" - }, + }, "prod" ] - }, + }, "IsDevEnv": { "Fn::Equals": [ { "Ref": "EnvType" - }, + }, "dev" ] } - }, + }, "Resources": { + "ServerlessDeploymentApplication": { + "Type": "AWS::CodeDeploy::Application", + "Properties": { + "ComputePlatform": "Lambda" + } + }, "HelloWorldFunctionRole": { - "Type": "AWS::IAM::Role", + "Type": "AWS::IAM::Role", "Properties": { "ManagedPolicyArns": [ "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" - ], + ], "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -42,30 +54,21 @@ ] } } - }, - "HelloWorldFunctionVersion1bc9589ea5": { - "DeletionPolicy": "Retain", - "Type": "AWS::Lambda::Version", - "Properties": { - "FunctionName": { - "Ref": "HelloWorldFunction" - } - } - }, + }, "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" @@ -75,118 +78,116 @@ ] } } - }, + }, "HelloWorldFunctionDeploymentGroup": { - "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::If": [ - "IsDevEnv", + "IsDevEnv", { "Fn::If": [ - "IsDevEnv2", + "IsDevEnv2", { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "AllAtOnce" } ] - }, + }, "TestCustomDeploymentConfig" ] - }, + }, { "Fn::Sub": [ - "CodeDeployDefault.Lambda${ConfigName}", + "CodeDeployDefault.Lambda${ConfigName}", { "ConfigName": "Canary10Percent15Minutes" } ] } ] - }, + }, "DeploymentStyle": { - "DeploymentType": "BLUE_GREEN", + "DeploymentType": "BLUE_GREEN", "DeploymentOption": "WITH_TRAFFIC_CONTROL" } } - }, - "ServerlessDeploymentApplication": { - "Type": "AWS::CodeDeploy::Application", + }, + "HelloWorldFunctionVersionfb53d5c2e6": { + "DeletionPolicy": "Retain", + "Type": "AWS::Lambda::Version", "Properties": { - "ComputePlatform": "Lambda" + "FunctionName": { + "Ref": "HelloWorldFunction" + } } - }, + }, "HelloWorldFunctionAliaslive": { - "Type": "AWS::Lambda::Alias", + "Type": "AWS::Lambda::Alias", "UpdatePolicy": { "CodeDeployLambdaAliasUpdate": { "ApplicationName": { "Ref": "ServerlessDeploymentApplication" - }, + }, "DeploymentGroupName": { "Ref": "HelloWorldFunctionDeploymentGroup" } } - }, + }, "Properties": { "FunctionVersion": { "Fn::GetAtt": [ - "HelloWorldFunctionVersion1bc9589ea5", + "HelloWorldFunctionVersionfb53d5c2e6", "Version" ] - }, + }, "FunctionName": { "Ref": "HelloWorldFunction" - }, + }, "Name": "live" } - }, + }, "HelloWorldFunction": { - "Type": "AWS::Lambda::Function", + "Type": "AWS::Lambda::Function", "Properties": { - "Handler": "index.handler", + "Handler": "index.handler", "Code": { - "S3Bucket": "manvendra-sam-test", - "S3Key": "b9cfacca4a48ef2097bb36f980cc4481" - }, + "S3Bucket": "bucket", + "S3Key": "key" + }, "Role": { "Fn::GetAtt": [ - "HelloWorldFunctionRole", + "HelloWorldFunctionRole", "Arn" ] - }, - "Runtime": "nodejs8.10", + }, + "Runtime": "nodejs8.10", "Tags": [ { - "Value": "SAM", + "Value": "SAM", "Key": "lambda:createdBy" } ] } } - }, - "Parameters": { - "EnvType": { - "Default": "dev" - } } -} \ No newline at end of file +} diff --git a/tests/translator/test_translator.py b/tests/translator/test_translator.py index 62a8bd0ea..8542c0f18 100644 --- a/tests/translator/test_translator.py +++ b/tests/translator/test_translator.py @@ -206,6 +206,8 @@ class TestTranslatorEndToEnd(TestCase): 'function_with_kmskeyarn', 'function_with_alias', 'function_with_alias_intrinsics', + 'function_with_custom_codedeploy_deployment_preference', + 'function_with_custom_conditional_codedeploy_deployment_preference', 'function_with_disabled_deployment_preference', 'function_with_deployment_preference', 'function_with_deployment_preference_all_parameters',