Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Added Support for referencing custom configuration in CodeDeploy #848

Merged
merged 8 commits into from
Apr 16, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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))
Expand All @@ -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:
jlhood marked this conversation as resolved.
Show resolved Hide resolved
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'}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Resources:
Runtime: python2.7
AutoPublishAlias: live
DeploymentPreference:
Type: Linear10PercentEvery3Minute
Type: Linear10PercentEvery3Minutes
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Resources:
Runtime: python2.7
AutoPublishAlias: livewithdeploymentwithhooksandalarms
DeploymentPreference:
Type: Linear10PercentEvery2Minute
Type: Linear10PercentEvery2Minutes
Hooks:
PreTraffic: !Ref MySanityTestFunction
PostTraffic: !Ref MyValidationTestFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
Expand All @@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
jlhood marked this conversation as resolved.
Show resolved Hide resolved
],
"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"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"Fn::Sub": [
"CodeDeployDefault.Lambda${ConfigName}",
{
"ConfigName": "Linear10PercentEvery3Minute"
"ConfigName": "Linear10PercentEvery3Minutes"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
"Fn::Sub": [
"CodeDeployDefault.Lambda${ConfigName}",
{
"ConfigName": "Linear10PercentEvery2Minute"
"ConfigName": "Linear10PercentEvery2Minutes"
}
]
},
Expand Down
Loading