Skip to content

Commit b21750f

Browse files
committed
Merge branch 'morosi-feat' into 'master'
Improve the BlueGreen API See merge request it/e3-aws!66
2 parents 657506b + 02ee01c commit b21750f

File tree

2 files changed

+113
-11
lines changed

2 files changed

+113
-11
lines changed

src/e3/aws/troposphere/awslambda/__init__.py

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ def __init__(
574574
name: str,
575575
description: str,
576576
lambda_arn: str | GetAtt | Ref,
577-
lambda_version: str,
577+
lambda_version: str | GetAtt | Version,
578578
alias_name: str | None = None,
579579
provisioned_concurrency_config: (
580580
awslambda.ProvisionedConcurrencyConfiguration | None
@@ -610,11 +610,17 @@ def ref(self) -> Ref:
610610

611611
def resources(self, stack: Stack) -> list[AWSObject]:
612612
"""Return list of AWSObject associated with the construct."""
613+
lambda_version = (
614+
self.lambda_version.version
615+
if isinstance(self.lambda_version, Version)
616+
else self.lambda_version
617+
)
618+
613619
params = {
614620
"Name": self.alias_name if self.alias_name is not None else self.name,
615621
"Description": self.description,
616622
"FunctionName": self.lambda_arn,
617-
"FunctionVersion": self.lambda_version,
623+
"FunctionVersion": lambda_version,
618624
}
619625

620626
if self.provisioned_concurrency_config is not None:
@@ -780,12 +786,69 @@ def resources(self, stack: Stack) -> list[AWSObject]:
780786
return self.versions
781787

782788

789+
class BlueGreenVersions(AutoVersion):
790+
"""Automatic blue/green lambda versions."""
791+
792+
def __init__(
793+
self,
794+
blue_version: int,
795+
green_version: int,
796+
min_version: int | None = None,
797+
lambda_name: str | None = None,
798+
lambda_arn: str | GetAtt | Ref | None = None,
799+
lambda_function: Function | None = None,
800+
provisioned_concurrency_config: (
801+
awslambda.ProvisionedConcurrencyConfiguration | None
802+
) = None,
803+
code_sha256: str | None = None,
804+
) -> None:
805+
"""Create lambda versions from min_version to blue/green versions included.
806+
807+
See AutoVersion.
808+
809+
:param blue_version: number of the blue version
810+
:param green_version: number of the green version
811+
:param min_version: minimum deployed version (default 1)
812+
:param lambda_name: the name of the Lambda function
813+
:param lambda_arn: the arn of the Lambda function
814+
:param lambda_function: the Lambda function
815+
:param provisioned_concurrency_config: specifies a provisioned concurrency
816+
configuration for a function's version. Updates are not supported for this
817+
property.
818+
:param code_sha256: only publish a version if the hash value matches the value
819+
that's specified. Use this option to avoid publishing a version if the
820+
function code has changed since you last updated it. Updates are not
821+
supported for this property
822+
"""
823+
super().__init__(
824+
version=max(blue_version, green_version),
825+
min_version=min_version,
826+
lambda_name=lambda_name,
827+
lambda_arn=lambda_arn,
828+
lambda_function=lambda_function,
829+
provisioned_concurrency_config=provisioned_concurrency_config,
830+
code_sha256=code_sha256,
831+
)
832+
self.blue_version = blue_version
833+
self.green_version = green_version
834+
835+
@property
836+
def blue(self) -> Version:
837+
"""Return the blue version."""
838+
return self.get_version(self.blue_version)
839+
840+
@property
841+
def green(self) -> Version:
842+
"""Return the green version."""
843+
return self.get_version(self.green_version)
844+
845+
783846
class BlueGreenAliasConfiguration(object):
784847
"""Blue/Green alias configuration."""
785848

786849
def __init__(
787850
self,
788-
version: str,
851+
version: str | GetAtt | Version,
789852
name: str | None = None,
790853
provisioned_concurrency_config: (
791854
awslambda.ProvisionedConcurrencyConfiguration | None

tests/tests_e3_aws/troposphere/awslambda/awslambda_test.py

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
Alias,
3030
Version,
3131
AutoVersion,
32+
BlueGreenVersions,
3233
BlueGreenAliases,
3334
BlueGreenAliasConfiguration,
3435
)
@@ -370,13 +371,37 @@
370371
},
371372
}
372373

374+
EXPECTED_BLUEGREENVERSIONS_TEMPLATE = {
375+
"MypylambdaVersion1": {
376+
"Properties": {
377+
"Description": "version 1 of mypylambda lambda",
378+
"FunctionName": {"Fn::GetAtt": ["Mypylambda", "Arn"]},
379+
},
380+
"Type": "AWS::Lambda::Version",
381+
},
382+
"MypylambdaVersion2": {
383+
"Properties": {
384+
"Description": "version 2 of mypylambda lambda",
385+
"FunctionName": {"Fn::GetAtt": ["Mypylambda", "Arn"]},
386+
},
387+
"Type": "AWS::Lambda::Version",
388+
},
389+
"MypylambdaVersion3": {
390+
"Properties": {
391+
"Description": "version 3 of mypylambda lambda",
392+
"FunctionName": {"Fn::GetAtt": ["Mypylambda", "Arn"]},
393+
},
394+
"Type": "AWS::Lambda::Version",
395+
},
396+
}
397+
373398
EXPECTED_BLUEGREENALIASES_DEFAULT_TEMPLATE = {
374399
"MypylambdaBlueAlias": {
375400
"Properties": {
376401
"Name": "MypylambdaBlueAlias",
377402
"Description": "blue alias for mypylambda lambda",
378403
"FunctionName": {"Fn::GetAtt": ["Mypylambda", "Arn"]},
379-
"FunctionVersion": {"Ref": "MypylambdaVersion1"},
404+
"FunctionVersion": {"Fn::GetAtt": ["MypylambdaVersion1", "Version"]},
380405
},
381406
"Type": "AWS::Lambda::Alias",
382407
},
@@ -385,7 +410,7 @@
385410
"Name": "MypylambdaGreenAlias",
386411
"Description": "green alias for mypylambda lambda",
387412
"FunctionName": {"Fn::GetAtt": ["Mypylambda", "Arn"]},
388-
"FunctionVersion": {"Ref": "MypylambdaVersion2"},
413+
"FunctionVersion": {"Fn::GetAtt": ["MypylambdaVersion2", "Version"]},
389414
},
390415
"Type": "AWS::Lambda::Alias",
391416
},
@@ -397,7 +422,7 @@
397422
"Name": "prod",
398423
"Description": "prod alias for mypylambda lambda",
399424
"FunctionName": {"Fn::GetAtt": ["Mypylambda", "Arn"]},
400-
"FunctionVersion": {"Ref": "MypylambdaVersion1"},
425+
"FunctionVersion": {"Fn::GetAtt": ["MypylambdaVersion1", "Version"]},
401426
"ProvisionedConcurrencyConfig": {"ProvisionedConcurrentExecutions": 1},
402427
"RoutingConfig": {
403428
"AdditionalVersionWeights": [
@@ -417,7 +442,7 @@
417442
"Name": "beta",
418443
"Description": "beta alias for mypylambda lambda",
419444
"FunctionName": {"Fn::GetAtt": ["Mypylambda", "Arn"]},
420-
"FunctionVersion": {"Ref": "MypylambdaVersion2"},
445+
"FunctionVersion": {"Fn::GetAtt": ["MypylambdaVersion2", "Version"]},
421446
"ProvisionedConcurrencyConfig": {"ProvisionedConcurrentExecutions": 1},
422447
"RoutingConfig": {
423448
"AdditionalVersionWeights": [
@@ -837,6 +862,20 @@ def test_autoversion(stack: Stack, simple_lambda_function: PyFunction) -> None:
837862
assert auto_version.latest.name == "mypylambdaVersion3"
838863

839864

865+
def test_bluegreenversions(stack: Stack, simple_lambda_function: PyFunction) -> None:
866+
"""Test BlueGreenVersions creation."""
867+
versions = BlueGreenVersions(
868+
blue_version=2,
869+
green_version=3,
870+
lambda_function=simple_lambda_function,
871+
)
872+
stack.add(versions)
873+
print(stack.export()["Resources"])
874+
assert stack.export()["Resources"] == EXPECTED_BLUEGREENVERSIONS_TEMPLATE
875+
assert versions.blue.name == "mypylambdaVersion2"
876+
assert versions.green.name == "mypylambdaVersion3"
877+
878+
840879
def test_bluegreenaliases_default(
841880
stack: Stack, simple_lambda_function: PyFunction
842881
) -> None:
@@ -846,8 +885,8 @@ def test_bluegreenaliases_default(
846885
lambda_function=simple_lambda_function,
847886
)
848887
aliases = BlueGreenAliases(
849-
blue_config=BlueGreenAliasConfiguration(version=auto_version.previous.ref),
850-
green_config=BlueGreenAliasConfiguration(version=auto_version.latest.ref),
888+
blue_config=BlueGreenAliasConfiguration(version=auto_version.previous),
889+
green_config=BlueGreenAliasConfiguration(version=auto_version.latest),
851890
lambda_function=simple_lambda_function,
852891
)
853892
stack.add(aliases)
@@ -865,7 +904,7 @@ def test_bluegreenaliases(stack: Stack, simple_lambda_function: PyFunction) -> N
865904
)
866905
aliases = BlueGreenAliases(
867906
blue_config=BlueGreenAliasConfiguration(
868-
version=auto_version.previous.ref,
907+
version=auto_version.previous,
869908
name="prod",
870909
provisioned_concurrency_config=ProvisionedConcurrencyConfiguration(
871910
ProvisionedConcurrentExecutions=1
@@ -879,7 +918,7 @@ def test_bluegreenaliases(stack: Stack, simple_lambda_function: PyFunction) -> N
879918
),
880919
),
881920
green_config=BlueGreenAliasConfiguration(
882-
version=auto_version.latest.ref,
921+
version=auto_version.latest,
883922
name="beta",
884923
provisioned_concurrency_config=ProvisionedConcurrencyConfiguration(
885924
ProvisionedConcurrentExecutions=1

0 commit comments

Comments
 (0)