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
fix(ecs): gate DescribeClusters fields on include (#1942)
iot-rocketd204a60ceaf4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e459d459..d3c32426 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -9,6 +9,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).### Fixed+- **ECS — `DescribeClusters` honours `include`** — `settings` and `tags` come back empty and `attachments` and `configuration` are left out unless requested, `CreateCluster` keeps `configuration`, and a CloudFormation cluster reports its tags and the default `containerInsights` setting.- **CloudFormation — `AWS::S3::MultiRegionAccessPoint` and `AWS::AutoScaling::LaunchConfiguration` are replaced on update** — every property of both types is create-only, so a change now creates the resource under a new generated name and deletes the old one after the update (a `Regions` change was dropped and the stack reported the alias as the physical id, a launch configuration was overwritten under its old name), fails with the custom-named-resource error when the name is explicit, and is reported as `Replacement: True` in a change set.- **CloudFormation — `AWS::SQS::Queue` with `FifoQueue` gets a generated `.fifo` name** — `FifoQueue: true` without a `QueueName` creates a FIFO queue with a generated `.fifo` name instead of a standard queue, and a `QueueName` whose `.fifo` suffix disagrees with `FifoQueue` fails the resource. Contributed by @iot-rocket.- **RDS — `DescribeDBClusterSnapshotAttributes`** — was unimplemented (`InvalidAction: Unknown RDS action`), so Terraform's `aws_db_cluster_snapshot` resource failed on read (`reading RDS DB Cluster Snapshot … attribute`) after creating the snapshot. Returns the `restore` attribute with no shared accounts (the manual-snapshot default); `ModifyDBClusterSnapshotAttribute` is not implemented. Unknown snapshot ids answer `DBClusterSnapshotNotFoundFault`.diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.pyindex fed4fec7..03011ace 100644--- a/ministack/services/cloudformation/provisioners.py+++ b/ministack/services/cloudformation/provisioners.py@@ -7609,28 +7609,29 @@ def _ec2_subnet_rtb_assoc_delete(physical_id, props):def _ecs_cluster_create(logical_id, props, stack_name):name = props.get("ClusterName", f"{stack_name}-{logical_id}")- arn = f"arn:aws:ecs:{get_region()}:{get_account_id()}:cluster/{name}"- _ecs._clusters[name] = {- "clusterArn": arn,+ tags = _pascal_to_camel(props.get("Tags") or [])+ # A stack update runs the create again; tags added outside the template stay.+ old = _ecs._clusters.get(name)+ foreign = []+ if old:+ template_keys = {t["key"] for t in old["tags"] + tags}+ foreign = [t for t in _ecs._tags.get(old["clusterArn"], []) if t["key"] not in template_keys]+ cluster = _ecs._put_cluster({"clusterName": name,- "status": "ACTIVE",- "registeredContainerInstancesCount": 0,- "runningTasksCount": 0,- "pendingTasksCount": 0,- "activeServicesCount": 0,+ "tags": tags,"settings": _pascal_to_camel(props.get("ClusterSettings") or []),"capacityProviders": props.get("CapacityProviders", []),"defaultCapacityProviderStrategy": _pascal_to_camel(props.get("DefaultCapacityProviderStrategy") or []),- "tags": [{"key": t["Key"], "value": t["Value"]} for t in props.get("Tags", [])],- }- if props.get("Configuration"):- _ecs._clusters[name]["configuration"] = _pascal_to_camel(props["Configuration"])- return name, {"Arn": arn, "ClusterName": name}+ "configuration": _pascal_to_camel(props.get("Configuration") or {}),+ })+ if old:+ _ecs._tags[cluster["clusterArn"]] = tags + foreign+ return name, {"Arn": cluster["clusterArn"], "ClusterName": name}def _ecs_cluster_delete(physical_id, props):- _ecs._clusters.pop(physical_id, None)+ _ecs._delete_cluster({"cluster": physical_id})def _cfn_to_camel(key):diff --git a/ministack/services/ecs.py b/ministack/services/ecs.pyindex 283ac061..5a39ac4c 100644--- a/ministack/services/ecs.py+++ b/ministack/services/ecs.py@@ -505,7 +505,12 @@ def _create_cluster(data):name = data.get("clusterName", "default")if name in _clusters and _clusters[name]["status"] == "ACTIVE":return json_response({"cluster": _clusters[name]})+ return json_response({"cluster": _put_cluster(data)})++def _put_cluster(data):+ """Store a cluster built from CreateCluster fields, replacing one of the same name."""+ name = data.get("clusterName", "default")arn = f"arn:aws:ecs:{get_region()}:{get_account_id()}:cluster/{name}"cluster = {"clusterArn": arn,@@ -516,19 +521,21 @@ def _create_cluster(data):"pendingTasksCount": 0,"activeServicesCount": 0,"tags": data.get("tags", []),- "settings": data.get("settings", [+ "settings": data.get("settings") or [{"name": "containerInsights", "value": "disabled"},- ]),+ ],"capacityProviders": data.get("capacityProviders", []),"defaultCapacityProviderStrategy": data.get("defaultCapacityProviderStrategy", []),"statistics": [],"attachments": [],"attachmentsStatus": "",}+ if data.get("configuration"):+ cluster["configuration"] = data["configuration"]_clusters[name] = clusterif cluster["tags"]:_tags[arn] = list(cluster["tags"])- return json_response({"cluster": cluster})+ return clusterdef _delete_cluster(data):@@ -549,22 +556,28 @@ def _describe_clusters(data):for ref in names:n = _resolve_cluster_name(ref)if n in _clusters:- c = dict(_clusters[n])- if "TAGS" in include:- c["tags"] = _tags.get(c["clusterArn"], [])_recount_cluster(n)- c.update({- "runningTasksCount": _clusters[n]["runningTasksCount"],- "pendingTasksCount": _clusters[n]["pendingTasksCount"],- "activeServicesCount": _clusters[n]["activeServicesCount"],- })- result.append(c)+ result.append(_cluster_view(_clusters[n], include))else:arn = ref if ref.startswith("arn:") else f"arn:aws:ecs:{get_region()}:{get_account_id()}:cluster/{ref}"failures.append({"arn": arn, "reason": "MISSING"})return json_response({"clusters": result, "failures": failures})+def _cluster_view(cluster, include):+ """Cluster as DescribeClusters returns it for the given include values."""+ c = {k: v for k, v in cluster.items()+ if k not in ("attachments", "attachmentsStatus", "configuration")}+ c["settings"] = cluster["settings"] if "SETTINGS" in include else []+ c["statistics"] = []+ c["tags"] = _tags.get(cluster["clusterArn"], []) if "TAGS" in include else []+ if "ATTACHMENTS" in include:+ c["attachments"] = cluster.get("attachments", [])+ if "CONFIGURATIONS" in include and "configuration" in cluster:+ c["configuration"] = cluster["configuration"]+ return c++def _list_clusters(data):arns = [c["clusterArn"] for c in _clusters.values() if c["status"] == "ACTIVE"]return json_response({"clusterArns": arns})diff --git a/tests/test_cfn.py b/tests/test_cfn.pyindex 337d02c1..c4eccc62 100644--- a/tests/test_cfn.py+++ b/tests/test_cfn.py@@ -18216,6 +18216,56 @@ def test_cfn_ecs_cluster_settings_read_back_in_the_api_shape(cfn, ecs):_delete_cfn_test_stack(cfn, stack_name)+def test_cfn_ecs_cluster_include_gated_fields(cfn, ecs):+ """Template tags come back under TAGS and the default containerInsights setting under SETTINGS."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cfn-ecs-incl-{suffix}"+ tagged, bare = f"cfn-ecs-incl-t-{suffix}", f"cfn-ecs-incl-b-{suffix}"+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps({"Resources": {+ "Tagged": {"Type": "AWS::ECS::Cluster", "Properties": {+ "ClusterName": tagged, "Tags": [{"Key": "k", "Value": "v"}]}},+ "Bare": {"Type": "AWS::ECS::Cluster", "Properties": {"ClusterName": bare}},+ }}))+ try:+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")+ full = {c["clusterName"]: c for c in ecs.describe_clusters(+ clusters=[tagged, bare], include=["SETTINGS", "TAGS"])["clusters"]}+ assert {"key": "k", "value": "v"} in full[tagged]["tags"]+ assert full[bare]["settings"] == [{"name": "containerInsights", "value": "disabled"}]+ for c in ecs.describe_clusters(clusters=[tagged, bare])["clusters"]:+ assert c["tags"] == [] and c["settings"] == [] and c["statistics"] == []+ finally:+ _delete_cfn_test_stack(cfn, stack_name)+++def test_cfn_ecs_cluster_update_keeps_tags_added_outside_the_template(cfn, ecs):+ """A stack update that runs the cluster create again keeps a TagResource tag next to the template tags."""+ suffix = _uuid_mod.uuid4().hex[:8]+ stack_name = cluster = f"cfn-ecs-oob-{suffix}"++ def template(insights):+ return json.dumps({"Resources": {"Cluster": {"Type": "AWS::ECS::Cluster", "Properties": {+ "ClusterName": cluster, "Tags": [{"Key": "stage", "Value": "v1"}],+ "ClusterSettings": [{"Name": "containerInsights", "Value": insights}]}}}})++ cfn.create_stack(StackName=stack_name, TemplateBody=template("disabled"))+ try:+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")+ arn = ecs.describe_clusters(clusters=[cluster])["clusters"][0]["clusterArn"]+ ecs.tag_resource(resourceArn=arn, tags=[{"key": "oob", "value": "1"}])++ cfn.update_stack(StackName=stack_name, TemplateBody=template("enabled"))+ stack = _wait_stack(cfn, stack_name)+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")+ tags = ecs.list_tags_for_resource(resourceArn=arn)["tags"]+ assert {t["key"]: t["value"] for t in tags if not t["key"].startswith("aws:")} == {+ "stage": "v1", "oob": "1"}+ finally:+ _delete_cfn_test_stack(cfn, stack_name)++def test_cfn_ecs_service_network_and_load_balancers_read_back_in_the_api_shape(cfn, ecs, elbv2):"""NetworkConfiguration and LoadBalancers read back in camelCase, on create and on update."""suffix = _uuid_mod.uuid4().hex[:8]diff --git a/tests/test_ecs.py b/tests/test_ecs.pyindex 20017038..f52bcced 100644--- a/tests/test_ecs.py+++ b/tests/test_ecs.py@@ -1107,6 +1107,30 @@ def test_ecs_update_cluster(ecs):)assert resp["cluster"]["clusterName"] == "upd-cl"+[email protected]("include", [+ [], ["ATTACHMENTS"], ["CONFIGURATIONS"], ["SETTINGS"], ["STATISTICS"], ["TAGS"],+])+def test_ecs_describe_clusters_include_gates_fields(ecs, include):+ """Each include value returns only its own field; the others stay empty or absent."""+ name = f"incl-{_uuid_mod.uuid4().hex[:8]}"+ settings = [{"name": "containerInsights", "value": "enabled"}]+ tags = [{"key": "k", "value": "v"}]+ configuration = {"executeCommandConfiguration": {"logging": "DEFAULT"}}+ ecs.create_cluster(clusterName=name, tags=tags, settings=settings, configuration=configuration)+ try:+ c = ecs.describe_clusters(clusters=[name], include=include)["clusters"][0]+ assert c["settings"] == (settings if "SETTINGS" in include else [])+ assert c["tags"] == (tags if "TAGS" in include else [])+ assert c.get("configuration") == (configuration if "CONFIGURATIONS" in include else None)+ assert ("attachments" in c) == ("ATTACHMENTS" in include)+ assert "attachmentsStatus" not in c+ if "STATISTICS" not in include:+ assert c["statistics"] == []+ finally:+ ecs.delete_cluster(cluster=name)++def test_ecs_timestamps_are_epoch(ecs):"""ECS timestamps should be epoch numbers, not ISO strings."""ecs.create_cluster(clusterName="ts-test-v44")