Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
feat(cloudformation): publish AWS::Lambda::Version through PublishVersion and update FunctionScalingConfig in place (#1904)
iot-rocketf2a6706eea83
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 1b0e4e5b..45778b42 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,8 +7,13 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]+### Added++- **CloudFormation — `AWS::Lambda::Version` updates `FunctionScalingConfig` in place** — a stack update re-ran the create, which published a new version for every change; a `FunctionScalingConfig` change now keeps the version, also when the update rolls back. Contributed by @iot-rocket.+### Fixed- **CloudFormation — `AWS::EC2::VPCGatewayAttachment` updates in place** — a changed `InternetGatewayId` or `VpnGatewayId` moves the attachment under the same `IGW|vpc-…` / `VGW|vpc-…` physical id, `VpnGatewayId` is attached at all, and a `VpcId` change leaves the gateway attached to the new VPC, or to the old one when the update rolls back.+- **CloudFormation — `AWS::Lambda::Version` publishes through `PublishVersion`** — the version takes its `Description`, a version of a function unchanged since its latest version fails with the `AlreadyExists` error AWS reports, `FunctionScalingConfig` on a function without a capacity provider is refused, and a function keeps its `CapacityProviderConfig`. Contributed by @iot-rocket.- **SESv2 — `CreateEmailIdentity`/`GetEmailIdentity` return Easy DKIM tokens for a DOMAIN identity** — a domain identity created without `DkimSigningAttributes` answered an empty `Tokens` list with `Status: NOT_STARTED`. AWS provides a set of DKIM tokens for its CNAME records in that case (Easy DKIM), so the Terraform `aws_sesv2_email_identity` resource's `dkim_signing_attributes[0].tokens` indexing failed. A DOMAIN identity now gets three tokens, `SigningAttributesOrigin: AWS_SES` and `Status: PENDING`, unless `DkimSigningAttributes` brings its own key (BYODKIM); EMAIL_ADDRESS identities are unchanged.- **API Gateway v2 (HTTP API) — a missing identity source is a 401 without caching too** — a REQUEST authorizer's short circuit for a declared identity source missing from the request only fired with `authorizerResultTtlInSeconds` caching on; with caching off the authorizer was invoked. AWS answers `401 {"message":"Unauthorized"}` (compact JSON) without invoking it either way, as observed on a deployed HTTP API with a TTL of 0. `$context.*` identity sources, which MiniStack does not model, do not count as missing without caching.diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.pyindex 13fdd614..162aaffa 100644--- a/ministack/services/cloudformation/provisioners.py+++ b/ministack/services/cloudformation/provisioners.py@@ -2002,6 +2002,8 @@ def _lambda_create(logical_id, props, stack_name):func["config"]["ImageUri"] = image_uriif props.get("ImageConfig"):func["config"]["ImageConfigResponse"] = {"ImageConfig": props["ImageConfig"]}+ if props.get("CapacityProviderConfig"):+ func["config"]["CapacityProviderConfig"] = props["CapacityProviderConfig"]replaced = name in _lambda_svc._functions_lambda_svc._functions[name] = func# On a stack UPDATE this re-provisions over an existing function; recycle the@@ -3666,28 +3668,45 @@ def _lambda_permission_delete(physical_id, props, logical_id=None):# --- Lambda Version ----def _lambda_version_create(logical_id, props, stack_name):+def _lambda_version_function(props):+ """The target function and its name; FunctionScalingConfig needs a capacity provider."""func, func_name, _resource_arn, _qualifier = _lambda_function_for_cfn_ref(props.get("FunctionName", ""))+ if func and props.get("FunctionScalingConfig") and not func["config"].get("CapacityProviderConfig"):+ raise ValueError("FunctionScalingConfig can't be specified for this Lambda function type.")+ return func, func_name+++def _lambda_version_create(logical_id, props, stack_name):+ func, func_name = _lambda_version_function(props)if func:- import copy- ver_num = func["next_version"]- func["next_version"] = ver_num + 1- ver_str = str(ver_num)- ver_config = copy.deepcopy(func["config"])- ver_config["Version"] = ver_str+ latest = max(func["versions"], key=int, default=None)+ if latest and func["versions"][latest].get("function_revision") == func["config"]["RevisionId"]:+ raise ValueError(+ f"A version for this Lambda function exists ( {latest} ). "+ "Modify the function to create a new version."+ )+ status, _headers, body = _lambda_svc._publish_version(+ func_name, {"Description": props.get("Description")})+ if status >= 400:+ raise ValueError(f"AWS::Lambda::Version PublishVersion failed: {body.decode()}")# Ref on AWS::Lambda::Version returns the *qualified* ARN# (arn:...:function:name:version) — that qualifier is also what lets# the delete handler find the version it published.- ver_arn = f"{ver_config['FunctionArn']}:{ver_str}"- func["versions"][ver_str] = {- "config": ver_config,- "code_zip": func.get("code_zip"),- }- return ver_arn, {"Version": ver_str}+ config = json.loads(body)+ return config["FunctionArn"], {"Version": config["Version"]}ver_arn = f"arn:aws:lambda:{get_region()}:{get_account_id()}:function:{func_name}:1"return ver_arn, {"Version": "1"}+def _lambda_version_update(physical_id, old_props, new_props, stack_name, logical_id=None):+ """FunctionScalingConfig updates in place; every other property publishes a new version."""+ if ({k: v for k, v in old_props.items() if k != "FunctionScalingConfig"}+ != {k: v for k, v in new_props.items() if k != "FunctionScalingConfig"}):+ return _lambda_version_create(logical_id or physical_id, new_props, stack_name)+ _lambda_version_function(new_props)+ return physical_id, {"Version": physical_id.rsplit(":", 1)[-1]}++def _lambda_version_delete(physical_id, props):"""Remove the published version, as DeleteFunction with a qualifier does.@@ -11483,7 +11502,12 @@ _RESOURCE_HANDLERS = {"delete": _lambda_permission_delete,"delete_with_logical_id": True,},- "AWS::Lambda::Version": {"create": _lambda_version_create, "delete": _lambda_version_delete},+ "AWS::Lambda::Version": {+ "create": _lambda_version_create,+ "update": _lambda_version_update,+ "update_with_logical_id": True,+ "delete": _lambda_version_delete,+ },"AWS::CloudFormation::WaitCondition": {"create": _cfn_wait_condition_create, "update": _cfn_wait_condition_update},"AWS::CloudFormation::WaitConditionHandle": {"create": _cfn_wait_condition_handle_create, "delete": _cfn_wait_condition_handle_delete},"AWS::CloudFormation::Stack": {diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.pyindex 5cb8ca84..db5996e2 100644--- a/ministack/services/lambda_svc.py+++ b/ministack/services/lambda_svc.py@@ -5768,6 +5768,7 @@ def _publish_version(name: str, data: dict):ver_record = {"config": ver_config,"code_zip": func.get("code_zip"),+ "function_revision": func["config"].get("RevisionId"),}func["versions"][str(ver_num)] = ver_recordif _stamp_snapstart_published_version(ver_config):diff --git a/tests/test_cfn.py b/tests/test_cfn.pyindex e551ab34..a5eb0e38 100644--- a/tests/test_cfn.py+++ b/tests/test_cfn.py@@ -17459,6 +17459,122 @@ def test_cfn_lambda_version_stack_delete_removes_version(cfn, lam):pass+def _cfn_lambda_version_template(fn, result=1, version=None, capacity_provider=False, extra=None):+ function = {"FunctionName": fn, "Runtime": "python3.12", "Handler": "index.handler",+ "Role": _CR_LAMBDA_ROLE,+ "Code": {"ZipFile": f"def handler(e, c):\n return {result}\n"}}+ if capacity_provider:+ function["CapacityProviderConfig"] = {"LambdaManagedInstancesCapacityProviderConfig": {+ "CapacityProviderArn": "arn:aws:lambda:us-east-1:000000000000:capacity-provider:cp"}}+ return json.dumps({"Resources": {+ "Fn": {"Type": "AWS::Lambda::Function", "Properties": function},+ "V": {"Type": "AWS::Lambda::Version",+ "Properties": {"FunctionName": {"Ref": "Fn"}, **(version or {})}},+ **(extra or {}),+ }})+++def _cfn_lambda_versions(lam, fn):+ return [(v["Version"], v["Description"])+ for v in lam.list_versions_by_function(FunctionName=fn)["Versions"]+ if v["Version"] != "$LATEST"]+++def _cfn_lambda_version_update(cfn, stack_name, template, status):+ cfn.update_stack(StackName=stack_name, TemplateBody=template)+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == status, stack.get("StackStatusReason")+++def test_cfn_lambda_version_scaling_config_updates_in_place(cfn, lam):+ """A FunctionScalingConfig change keeps the version, also when the update rolls back."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = fn = f"cfn-ver-fsc-{suffix}"++ def scaling(high):+ return {"Description": "one", "FunctionScalingConfig": {+ "MinExecutionEnvironments": 1, "MaxExecutionEnvironments": high}}++ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=_cfn_lambda_version_template(+ fn, version=scaling(1), capacity_provider=True))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")+ physical_id = _stack_physical_id(cfn, stack_name, "V")+ assert physical_id.endswith(f":function:{fn}:1")++ _cfn_lambda_version_update(cfn, stack_name, _cfn_lambda_version_template(+ fn, version=scaling(2), capacity_provider=True), "UPDATE_COMPLETE")+ assert _stack_physical_id(cfn, stack_name, "V") == physical_id+ assert _cfn_lambda_versions(lam, fn) == [("1", "one")]++ _cfn_lambda_version_update(cfn, stack_name, _cfn_with_failing_resource(+ _cfn_lambda_version_template(fn, version=scaling(3), capacity_provider=True), "V"),+ "UPDATE_ROLLBACK_COMPLETE")+ assert _stack_physical_id(cfn, stack_name, "V") == physical_id+ assert _cfn_lambda_versions(lam, fn) == [("1", "one")]+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_lambda_version_of_an_unchanged_function_is_refused(cfn, lam):+ """A Description change alone fails; with a code change it publishes the next version."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = fn = f"cfn-ver-same-{suffix}"+ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=_cfn_lambda_version_template(+ fn, version={"Description": "one"}))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")++ _cfn_lambda_version_update(cfn, stack_name, _cfn_lambda_version_template(+ fn, version={"Description": "two"}), "UPDATE_ROLLBACK_COMPLETE")+ assert "A version for this Lambda function exists ( 1 )" in _stack_event_reasons(+ cfn, stack_name)+ assert _stack_physical_id(cfn, stack_name, "V").endswith(":1")+ assert _cfn_lambda_versions(lam, fn) == [("1", "one")]++ _cfn_lambda_version_update(cfn, stack_name, _cfn_lambda_version_template(+ fn, result=2, version={"Description": "two"}), "UPDATE_COMPLETE")+ assert _stack_physical_id(cfn, stack_name, "V").endswith(":2")+ assert _cfn_lambda_versions(lam, fn) == [("2", "two")]+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_lambda_second_version_of_an_unchanged_function_is_refused(cfn):+ """Two versions of the same unchanged function in one template roll the stack back."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = fn = f"cfn-ver-two-{suffix}"+ second = {"V2": {"Type": "AWS::Lambda::Version", "DependsOn": "V",+ "Properties": {"FunctionName": {"Ref": "Fn"}}}}+ try:+ cfn.create_stack(StackName=stack_name,+ TemplateBody=_cfn_lambda_version_template(fn, extra=second))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "ROLLBACK_COMPLETE", stack.get("StackStatusReason")+ assert "A version for this Lambda function exists ( 1 )" in _stack_event_reasons(+ cfn, stack_name)+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_lambda_version_scaling_config_needs_a_capacity_provider(cfn):+ """FunctionScalingConfig on a function without a capacity provider fails the version."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = fn = f"cfn-ver-nocp-{suffix}"+ try:+ cfn.create_stack(StackName=stack_name, TemplateBody=_cfn_lambda_version_template(+ fn, version={"FunctionScalingConfig": {+ "MinExecutionEnvironments": 0, "MaxExecutionEnvironments": 1}}))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "ROLLBACK_COMPLETE", stack.get("StackStatusReason")+ assert "FunctionScalingConfig can't be specified for this Lambda function type" in (+ _stack_event_reasons(cfn, stack_name))+ finally:+ _delete_cfn_test_stack(cfn, stack_name)++def test_cfn_appsync_schema_stack_delete_removes_schema(cfn, appsync):"""AWS::AppSync::GraphQLSchema now has a real delete handler: deleting thestack removes the schema from the (externally owned) API."""