Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

feat(servicediscovery): add Cloud Map service to MiniStack with Route53-backed DNS namespaces (#147)

Johannes Grumböck11adac2791fe
diff --git a/ministack/app.py b/ministack/app.py
index 60f30c25..3fae9d13 100644
--- a/ministack/app.py
+++ b/ministack/app.py
@@ -75,6 +75,7 @@ from ministack.services import (
stepfunctions,
waf,
cloudfront,
+ servicediscovery,
)
from ministack.services import iam_sts
from ministack.services.iam_sts import handle_iam_request, handle_sts_request
@@ -124,6 +125,7 @@ SERVICE_HANDLERS = {
"kms": kms.handle_request,
"cloudfront": cloudfront.handle_request,
"appsync": appsync.handle_request,
+ "servicediscovery": servicediscovery.handle_request,
}
SERVICE_NAME_ALIASES = {
@@ -178,7 +180,7 @@ BANNER = r"""
SSM, EventBridge, Kinesis, CloudWatch, SES, SES v2, ACM, WAF v2, Step Functions,
ECS, RDS, ElastiCache, Glue, Athena, API Gateway, Firehose, Route53,
Cognito, EC2, EMR, EBS, EFS, ALB/ELBv2, CloudFormation, KMS, ECR, CloudFront,
- AppSync
+ AppSync, Cloud Map
"""
@@ -603,6 +605,7 @@ async def _handle_lifespan(scope, receive, send):
"firehose": firehose.get_state,
"ses": ses.get_state,
"ses_v2": ses_v2.get_state,
+ "servicediscovery": servicediscovery.get_state,
})
await send({"type": "lifespan.shutdown.complete"})
return
@@ -618,6 +621,10 @@ def _load_persisted_state():
if data_v1:
apigateway_v1.load_persisted_state(data_v1)
logger.info("Loaded persisted state for apigateway_v1")
+ data_sd = load_state("servicediscovery")
+ if data_sd:
+ servicediscovery.load_persisted_state(data_sd)
+ logger.info("Loaded persisted state for servicediscovery")
async def _wait_for_port(port, timeout=30):
@@ -734,6 +741,7 @@ def _reset_all_state():
(cloudfront, cloudfront.reset),
(ecr, ecr.reset),
(appsync, appsync.reset),
+ (servicediscovery, servicediscovery.reset),
]:
try:
fn()
diff --git a/ministack/core/router.py b/ministack/core/router.py
index 56839579..68e069af 100644
--- a/ministack/core/router.py
+++ b/ministack/core/router.py
@@ -167,6 +167,11 @@ SERVICE_PATTERNS = {
"path_prefixes": ["/v1/apis", "/v1/tags"],
"credential_scope": "appsync",
},
+ "servicediscovery": {
+ "target_prefixes": ["Route53AutoNaming_v20170314"],
+ "host_patterns": [r"servicediscovery\."],
+ "credential_scope": "servicediscovery",
+ },
}
@@ -219,6 +224,7 @@ def detect_service(method: str, path: str, headers: dict, query_params: dict) ->
"kms": "kms",
"cloudfront": "cloudfront",
"appsync": "appsync",
+ "servicediscovery": "servicediscovery",
}
if svc_name in scope_map:
return scope_map[svc_name]
diff --git a/ministack/services/servicediscovery.py b/ministack/services/servicediscovery.py
new file mode 100644
index 00000000..dc66ecbf
--- /dev/null
+++ b/ministack/services/servicediscovery.py
@@ -0,0 +1,644 @@
+"""
+AWS Cloud Map (Service Discovery) emulator.
+JSON-based API via X-Amz-Target: Route53AutoNaming_v20170314.
+"""
+
+import copy
+import json
+import logging
+import os
+import time
+import xml.etree.ElementTree as ET
+
+from ministack.core.responses import error_response_json, json_response, new_uuid
+from ministack.services import route53
+
+logger = logging.getLogger("servicediscovery")
+
+ACCOUNT_ID = os.environ.get("MINISTACK_ACCOUNT_ID", "000000000000")
+REGION = os.environ.get("MINISTACK_REGION", "us-east-1")
+
+# In-memory state
+_namespaces: dict = {} # ns_id -> namespace dict
+_services: dict = {} # svc_id -> service dict
+_instances: dict = {} # svc_id -> {instance_id -> instance dict}
+_operations: dict = {} # op_id -> operation dict
+_resource_tags: dict = {} # resource_arn -> [{"Key": ..., "Value": ...}]
+_service_attributes: dict = {} # svc_id -> {key: value}
+_instance_health_status: dict = {} # svc_id -> {instance_id: status}
+_instances_revision: dict = {} # svc_id -> int
+
+
+def get_state():
+ return {
+ "namespaces": copy.deepcopy(_namespaces),
+ "services": copy.deepcopy(_services),
+ "instances": copy.deepcopy(_instances),
+ "operations": copy.deepcopy(_operations),
+ "resource_tags": copy.deepcopy(_resource_tags),
+ "service_attributes": copy.deepcopy(_service_attributes),
+ "instance_health_status": copy.deepcopy(_instance_health_status),
+ "instances_revision": copy.deepcopy(_instances_revision),
+ }
+
+
+def load_persisted_state(data):
+ if not data:
+ return
+ _namespaces.update(data.get("namespaces", {}))
+ _services.update(data.get("services", {}))
+ _instances.update(data.get("instances", {}))
+ _operations.update(data.get("operations", {}))
+ _resource_tags.update(data.get("resource_tags", {}))
+ _service_attributes.update(data.get("service_attributes", {}))
+ _instance_health_status.update(data.get("instance_health_status", {}))
+ _instances_revision.update(data.get("instances_revision", {}))
+
+
+def reset():
+ _namespaces.clear()
+ _services.clear()
+ _instances.clear()
+ _operations.clear()
+ _resource_tags.clear()
+ _service_attributes.clear()
+ _instance_health_status.clear()
+ _instances_revision.clear()
+
+
+async def handle_request(method, path, headers, body, query_params):
+ target = headers.get("x-amz-target", "")
+ action = target.split(".")[-1] if "." in target else ""
+
+ try:
+ data = json.loads(body) if body else {}
+ except json.JSONDecodeError:
+ return error_response_json("SerializationException", "Invalid JSON", 400)
+
+ # Keep action in payload for handlers that need context
+ data["_action"] = action
+
+ handlers = {
+ "CreateHttpNamespace": _create_namespace,
+ "CreatePrivateDnsNamespace": _create_namespace,
+ "CreatePublicDnsNamespace": _create_namespace,
+ "CreateService": _create_service,
+ "DeleteNamespace": _delete_namespace,
+ "DeleteService": _delete_service,
+ "DeleteServiceAttributes": _delete_service_attributes,
+ "DeregisterInstance": _deregister_instance,
+ "DiscoverInstances": _discover_instances,
+ "DiscoverInstancesRevision": _discover_instances_revision,
+ "GetInstance": _get_instance,
+ "GetInstancesHealthStatus": _get_instances_health_status,
+ "GetNamespace": _get_namespace,
+ "GetOperation": _get_operation,
+ "GetService": _get_service,
+ "GetServiceAttributes": _get_service_attributes,
+ "ListInstances": _list_instances,
+ "ListNamespaces": _list_namespaces,
+ "ListOperations": _list_operations,
+ "ListServices": _list_services,
+ "ListTagsForResource": _list_tags_for_resource,
+ "RegisterInstance": _register_instance,
+ "TagResource": _tag_resource,
+ "UntagResource": _untag_resource,
+ "UpdateHttpNamespace": _update_namespace,
+ "UpdateInstanceCustomHealthStatus": _update_instance_custom_health_status,
+ "UpdatePrivateDnsNamespace": _update_namespace,
+ "UpdatePublicDnsNamespace": _update_namespace,
+ "UpdateService": _update_service,
+ "UpdateServiceAttributes": _update_service_attributes,
+ }
+
+ handler = handlers.get(action)
+ if not handler:
+ logger.warning("Unsupported Cloud Map action: %s", action)
+ return error_response_json("InvalidAction", f"Unknown action: {action}", 400)
+
+ import inspect
+
+ if inspect.iscoroutinefunction(handler):
+ return await handler(data)
+ return handler(data)
+
+
+def _namespace_arn(ns_id: str) -> str:
+ return f"arn:aws:servicediscovery:{REGION}:{ACCOUNT_ID}:namespace/{ns_id}"
+
+
+def _service_arn(svc_id: str) -> str:
+ return f"arn:aws:servicediscovery:{REGION}:{ACCOUNT_ID}:service/{svc_id}"
+
+
+def _create_operation(op_type: str, targets=None):
+ op_id = new_uuid()
+ targets = targets or {}
+
+ now = time.time()
+ op = {
+ "Id": op_id,
+ "Status": "SUCCESS",
+ "Type": op_type,
+ "Targets": targets,
+ "CreateDate": now,
+ "UpdateDate": now,
+ }
+ _operations[op_id] = op
+ return op_id
+
+
+def _touch_instances_revision(service_id: str):
+ _instances_revision[service_id] = int(_instances_revision.get(service_id, 0)) + 1
+
+
+async def _create_namespace(data):
+ ns_name = data.get("Name")
+ if not ns_name:
+ return error_response_json("InvalidInput", "Name is required", 400)
+
+ ns_id = f"ns-{new_uuid()[:8]}"
+ action = data.get("_action", "")
+
+ # Infer namespace type from action; fallback uses request fields.
+ is_private = action == "CreatePrivateDnsNamespace" or "Vpc" in data
+ is_public = action == "CreatePublicDnsNamespace"
+ is_http = action == "CreateHttpNamespace"
+ if not action:
+ if "Vpc" in data:
+ is_private = True
+ elif data.get("DnsConfig"):
+ is_public = True
+ else:
+ lowered = ns_name.lower()
+ if "http" in lowered:
+ is_http = True
+ elif "private" in lowered:
+ is_private = True
+ else:
+ is_public = True
+
+ ns_type = "DNS_PUBLIC"
+ if is_private:
+ ns_type = "DNS_PRIVATE"
+ elif is_http:
+ ns_type = "HTTP"
+
+ namespace = {
+ "Id": ns_id,
+ "Arn": _namespace_arn(ns_id),
+ "Name": ns_name,
+ "Type": ns_type,
+ "Description": data.get("Description"),
+ "CreateDate": time.time(),
+ }
+
+ if ns_type != "HTTP":
+ zone_name = ns_name if ns_name.endswith(".") else ns_name + "."
+ xml_body = f"""<CreateHostedZoneRequest xmlns=\"https://route53.amazonaws.com/doc/2013-04-01/\">
+ <Name>{zone_name}</Name>
+ <CallerReference>{new_uuid()}</CallerReference>
+ <HostedZoneConfig>
+ <Comment>Created by Cloud Map</Comment>
+ <PrivateZone>{"true" if is_private else "false"}</PrivateZone>
+ </HostedZoneConfig>
+ </CreateHostedZoneRequest>"""
+
+ status, _, body = await route53.handle_request(
+ "POST",
+ "/2013-04-01/hostedzone",
+ {},
+ xml_body.encode("utf-8"),
+ {},
+ )
+ if status >= 300:
+ return error_response_json("InternalFailure", "Failed to create Route53 hosted zone", 500)
+
+ root = ET.fromstring(body)
+ zone_id_el = root.find(".//{https://route53.amazonaws.com/doc/2013-04-01/}Id")
+ if zone_id_el is None or not zone_id_el.text:
+ return error_response_json("InternalFailure", "Hosted zone ID missing in Route53 response", 500)
+ zone_id = zone_id_el.text.split("/")[-1]
+
+ namespace["Properties"] = {
+ "DnsProperties": {
+ "HostedZoneId": zone_id,
+ }
+ }
+ else:
+ namespace["Properties"] = {
+ "HttpProperties": {
+ "HttpName": ns_name,
+ }
+ }
+
+ _namespaces[ns_id] = namespace
+ tags = data.get("Tags", [])
+ if tags:
+ _resource_tags[namespace["Arn"]] = tags
+
+ op_id = _create_operation("CREATE_NAMESPACE", {"NAMESPACE": ns_id})
+ return json_response({"OperationId": op_id})
+
+
+def _delete_namespace(data):
+ ns_id = data.get("Id")
+ if not ns_id:
+ return error_response_json("InvalidInput", "Id is required", 400)
+ if ns_id not in _namespaces:
+ return error_response_json("NamespaceNotFound", "Namespace not found", 404)
+
+ namespace = _namespaces.pop(ns_id)
+ _resource_tags.pop(namespace.get("Arn", ""), None)
+
+ op_id = _create_operation("DELETE_NAMESPACE", {"NAMESPACE": ns_id})
+ return json_response({"OperationId": op_id})
+
+
+def _get_namespace(data):
+ ns_id = data.get("Id")
+ ns = _namespaces.get(ns_id)
+ if not ns:
+ return error_response_json("NamespaceNotFound", "Namespace not found", 404)
+ return json_response({"Namespace": ns})
+
+
+def _list_namespaces(data):
+ return json_response({"Namespaces": list(_namespaces.values())})
+
+
+def _create_service(data):
+ ns_id = data.get("NamespaceId")
+ if not ns_id:
+ return error_response_json("InvalidInput", "NamespaceId is required", 400)
+ if ns_id not in _namespaces:
+ return error_response_json("NamespaceNotFound", "Namespace not found", 404)
+
+ name = data.get("Name")
+ if not name:
+ return error_response_json("InvalidInput", "Name is required", 400)
+
+ svc_id = f"srv-{new_uuid()[:8]}"
+ service = {
+ "Id": svc_id,
+ "Arn": _service_arn(svc_id),
+ "Name": name,
+ "NamespaceId": ns_id,
+ "Description": data.get("Description"),
+ "DnsConfig": data.get("DnsConfig"),
+ "HealthCheckConfig": data.get("HealthCheckConfig"),
+ "HealthCheckCustomConfig": data.get("HealthCheckCustomConfig"),
+ "CreateDate": time.time(),
+ }
+ _services[svc_id] = service
+ _service_attributes.setdefault(svc_id, {})
+ _instance_health_status.setdefault(svc_id, {})
+ _instances_revision.setdefault(svc_id, 1)
+
+ tags = data.get("Tags", [])
+ if tags:
+ _resource_tags[service["Arn"]] = tags
+
+ return json_response({"Service": service})
+
+
+def _delete_service(data):
+ svc_id = data.get("Id")
+ if not svc_id:
+ return error_response_json("InvalidInput", "Id is required", 400)
+ if svc_id not in _services:
+ return error_response_json("ServiceNotFound", "Service not found", 404)
+
+ service = _services.pop(svc_id)
+ _instances.pop(svc_id, None)
+ _service_attributes.pop(svc_id, None)
+ _instance_health_status.pop(svc_id, None)
+ _instances_revision.pop(svc_id, None)
+ _resource_tags.pop(service.get("Arn", ""), None)
+ return json_response({})
+
+
+def _delete_service_attributes(data):
+ svc_id = data.get("ServiceId")
+ attributes = data.get("Attributes", [])
+ if not svc_id:
+ return error_response_json("InvalidInput", "ServiceId is required", 400)
+ if svc_id not in _services:
+ return error_response_json("ServiceNotFound", "Service not found", 404)
+
+ current = _service_attributes.setdefault(svc_id, {})
+ for key in attributes:
+ current.pop(key, None)
+ return json_response({})
+
+
+def _get_service(data):
+ svc_id = data.get("Id")
+ svc = _services.get(svc_id)
+ if not svc:
+ return error_response_json("ServiceNotFound", "Service not found", 404)
+ return json_response({"Service": svc})
+
+
+def _list_services(data):
+ return json_response({"Services": list(_services.values())})
+
+
+def _register_instance(data):
+ svc_id = data.get("ServiceId")
+ inst_id = data.get("InstanceId")
+ if not svc_id:
+ return error_response_json("InvalidInput", "ServiceId is required", 400)
+ if not inst_id:
+ return error_response_json("InvalidInput", "InstanceId is required", 400)
+ if svc_id not in _services:
+ return error_response_json("ServiceNotFound", "Service not found", 404)
+
+ _instances.setdefault(svc_id, {})[inst_id] = {
+ "Id": inst_id,
+ "Attributes": data.get("Attributes", {}),
+ }
+ _instance_health_status.setdefault(svc_id, {})[inst_id] = "HEALTHY"
+ _touch_instances_revision(svc_id)
+
+ op_id = _create_operation("REGISTER_INSTANCE", {"INSTANCE": inst_id, "SERVICE": svc_id})
+ return json_response({"OperationId": op_id})
+
+
+def _deregister_instance(data):
+ svc_id = data.get("ServiceId")
+ inst_id = data.get("InstanceId")
+ if not svc_id:
+ return error_response_json("InvalidInput", "ServiceId is required", 400)
+ if not inst_id:
+ return error_response_json("InvalidInput", "InstanceId is required", 400)
+ if svc_id not in _services:
+ return error_response_json("ServiceNotFound", "Service not found", 404)
+
+ if svc_id in _instances and inst_id in _instances[svc_id]:
+ del _instances[svc_id][inst_id]
+ if svc_id in _instance_health_status and inst_id in _instance_health_status[svc_id]:
+ del _instance_health_status[svc_id][inst_id]
+ _touch_instances_revision(svc_id)
+
+ op_id = _create_operation("DEREGISTER_INSTANCE", {"INSTANCE": inst_id, "SERVICE": svc_id})
+ return json_response({"OperationId": op_id})
+
+
+def _get_instance(data):
+ svc_id = data.get("ServiceId")
+ inst_id = data.get("InstanceId")
+ inst = _instances.get(svc_id, {}).get(inst_id)
+ if not inst:
+ return error_response_json("InstanceNotFound", "Instance not found", 404)
+ return json_response({"Instance": inst})
+
+
+def _list_instances(data):
+ svc_id = data.get("ServiceId")
+ if not svc_id:
+ return error_response_json("InvalidInput", "ServiceId is required", 400)
+ if svc_id not in _services:
+ return error_response_json("ServiceNotFound", "Service not found", 404)
+ return json_response({"Instances": list(_instances.get(svc_id, {}).values())})
+
+
+def _discover_instances(data):
+ ns_name = data.get("NamespaceName")
+ svc_name = data.get("ServiceName")
+ if not ns_name or not svc_name:
+ return error_response_json("InvalidInput", "NamespaceName and ServiceName are required", 400)
+
+ namespace = next((n for n in _namespaces.values() if n.get("Name") == ns_name), None)
+ if not namespace:
+ return error_response_json("NamespaceNotFound", "Namespace not found", 404)
+
+ service = next(
+ (
+ s
+ for s in _services.values()
+ if s.get("Name") == svc_name and s.get("NamespaceId") == namespace.get("Id")
+ ),
+ None,
+ )
+ if not service:
+ return error_response_json("ServiceNotFound", "Service not found", 404)
+
+ out = []
+ requested_health_status = data.get("HealthStatus", "")
+ for inst in _instances.get(service["Id"], {}).values():
+ health = _instance_health_status.get(service["Id"], {}).get(inst["Id"], "HEALTHY")
+ if requested_health_status and requested_health_status != "ALL" and health != requested_health_status:
+ continue
+ out.append(
+ {
+ "InstanceId": inst["Id"],
+ "NamespaceName": ns_name,
+ "ServiceName": svc_name,
+ "Attributes": inst.get("Attributes", {}),
+ "HealthStatus": health,
+ }
+ )
+ return json_response({"Instances": out})
+
+
+def _discover_instances_revision(data):
+ ns_name = data.get("NamespaceName")
+ svc_name = data.get("ServiceName")
+ if not ns_name or not svc_name:
+ return error_response_json("InvalidInput", "NamespaceName and ServiceName are required", 400)
+
+ namespace = next((n for n in _namespaces.values() if n.get("Name") == ns_name), None)
+ if not namespace:
+ return error_response_json("NamespaceNotFound", "Namespace not found", 404)
+
+ service = next(
+ (
+ s
+ for s in _services.values()
+ if s.get("Name") == svc_name and s.get("NamespaceId") == namespace.get("Id")
+ ),
+ None,
+ )
+ if not service:
+ return error_response_json("ServiceNotFound", "Service not found", 404)
+
+ return json_response({"InstancesRevision": int(_instances_revision.get(service["Id"], 1))})
+
+
+def _get_instances_health_status(data):
+ svc_id = data.get("ServiceId")
+ if not svc_id:
+ return error_response_json("InvalidInput", "ServiceId is required", 400)
+ if svc_id not in _services:
+ return error_response_json("ServiceNotFound", "Service not found", 404)
+
+ statuses = _instance_health_status.get(svc_id, {})
+ filtered_instances = data.get("Instances") or list(statuses.keys())
+ filtered = {iid: statuses.get(iid, "UNKNOWN") for iid in filtered_instances}
+
+ max_results = int(data.get("MaxResults", len(filtered) or 1))
+ next_token = data.get("NextToken")
+ start = int(next_token) if str(next_token).isdigit() else 0
+ items = list(filtered.items())
+ page = items[start:start + max_results]
+ out = {k: v for k, v in page}
+
+ result = {"Status": out}
+ if start + max_results < len(items):
+ result["NextToken"] = str(start + max_results)
+ return json_response(result)
+
+
+def _get_operation(data):
+ op_id = data.get("OperationId")
+ op = _operations.get(op_id)
+ if not op:
+ return error_response_json("OperationNotFound", "Operation not found", 404)
+ return json_response({"Operation": op})
+
+
+def _list_operations(data):
+ ops = list(_operations.values())
+
+ for f in data.get("Filters", []) or []:
+ name = f.get("Name")
+ values = set(f.get("Values", []))
+ if not name or not values:
+ continue
+ if name == "STATUS":
+ ops = [o for o in ops if o.get("Status") in values]
+ elif name == "TYPE":
+ ops = [o for o in ops if o.get("Type") in values]
+ elif name == "NAMESPACE_ID":
+ ops = [o for o in ops if o.get("Targets", {}).get("NAMESPACE") in values]
+ elif name == "SERVICE_ID":
+ ops = [o for o in ops if o.get("Targets", {}).get("SERVICE") in values]
+
+ max_results = int(data.get("MaxResults", len(ops) or 1))
+ next_token = data.get("NextToken")
+ start = int(next_token) if str(next_token).isdigit() else 0
+ page = ops[start:start + max_results]
+
+ result = {"Operations": page}
+ if start + max_results < len(ops):
+ result["NextToken"] = str(start + max_results)
+ return json_response(result)
+
+
+def _get_service_attributes(data):
+ svc_id = data.get("ServiceId")
+ if not svc_id:
+ return error_response_json("InvalidInput", "ServiceId is required", 400)
+ if svc_id not in _services:
+ return error_response_json("ServiceNotFound", "Service not found", 404)
+ return json_response(
+ {
+ "ServiceAttributes": {
+ "ServiceArn": _service_arn(svc_id),
+ "ResourceOwner": "SELF",
+ "Attributes": _service_attributes.get(svc_id, {}),
+ }
+ }
+ )
+
+
+def _update_service_attributes(data):
+ svc_id = data.get("ServiceId")
+ attrs = data.get("Attributes", {})
+ if not svc_id:
+ return error_response_json("InvalidInput", "ServiceId is required", 400)
+ if svc_id not in _services:
+ return error_response_json("ServiceNotFound", "Service not found", 404)
+
+ current = _service_attributes.setdefault(svc_id, {})
+ current.update(attrs)
+ return json_response({})
+
+
+def _update_namespace(data):
+ ns_id = data.get("Id")
+ if not ns_id:
+ return error_response_json("InvalidInput", "Id is required", 400)
+ ns = _namespaces.get(ns_id)
+ if not ns:
+ return error_response_json("NamespaceNotFound", "Namespace not found", 404)
+
+ namespace_update = data.get("Namespace", {})
+ if "Description" in namespace_update:
+ ns["Description"] = namespace_update.get("Description")
+
+ op_id = _create_operation("UPDATE_NAMESPACE", {"NAMESPACE": ns_id})
+ return json_response({"OperationId": op_id})
+
+
+def _update_instance_custom_health_status(data):
+ svc_id = data.get("ServiceId")
+ inst_id = data.get("InstanceId")
+ status = data.get("Status")
+
+ if not svc_id or not inst_id or not status:
+ return error_response_json("InvalidInput", "ServiceId, InstanceId, and Status are required", 400)
+ if svc_id not in _services:
+ return error_response_json("ServiceNotFound", "Service not found", 404)
+ if inst_id not in _instances.get(svc_id, {}):
+ return error_response_json("InstanceNotFound", "Instance not found", 404)
+
+ _instance_health_status.setdefault(svc_id, {})[inst_id] = status
+ _touch_instances_revision(svc_id)
+ return 200, {"Content-Type": "application/x-amz-json-1.0"}, b""
+
+
+def _update_service(data):
+ svc_id = data.get("Id")
+ if not svc_id:
+ return error_response_json("InvalidInput", "Id is required", 400)
+ svc = _services.get(svc_id)
+ if not svc:
+ return error_response_json("ServiceNotFound", "Service not found", 404)
+
+ update = data.get("Service", {})
+ if "Description" in update:
+ svc["Description"] = update.get("Description")
+ if "DnsConfig" in update:
+ svc["DnsConfig"] = update.get("DnsConfig")
+ if "HealthCheckConfig" in update:
+ svc["HealthCheckConfig"] = update.get("HealthCheckConfig")
+ if "HealthCheckCustomConfig" in update:
+ svc["HealthCheckCustomConfig"] = update.get("HealthCheckCustomConfig")
+
+ op_id = _create_operation("UPDATE_SERVICE", {"SERVICE": svc_id})
+ return json_response({"OperationId": op_id})
+
+
+def _tag_resource(data):
+ arn = data.get("ResourceARN")
+ if not arn:
+ return error_response_json("InvalidInput", "ResourceARN is required", 400)
+
+ incoming = data.get("Tags", [])
+ existing = {t.get("Key"): t for t in _resource_tags.get(arn, []) if t.get("Key")}
+ for tag in incoming:
+ key = tag.get("Key")
+ if key:
+ existing[key] = {"Key": key, "Value": tag.get("Value", "")}
+ _resource_tags[arn] = list(existing.values())
+ return json_response({})
+
+
+def _untag_resource(data):
+ arn = data.get("ResourceARN")
+ if not arn:
+ return error_response_json("InvalidInput", "ResourceARN is required", 400)
+
+ keys = set(data.get("TagKeys", []))
+ if arn in _resource_tags:
+ _resource_tags[arn] = [t for t in _resource_tags[arn] if t.get("Key") not in keys]
+ return json_response({})
+
+
+def _list_tags_for_resource(data):
+ arn = data.get("ResourceARN")
+ if not arn:
+ return error_response_json("InvalidInput", "ResourceARN is required", 400)
+ return json_response({"Tags": _resource_tags.get(arn, [])})
diff --git a/tests/conftest.py b/tests/conftest.py
index 1f9bf8c2..79871de5 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -219,3 +219,20 @@ def sfn_sync():
@pytest.fixture(scope="session")
def cloudfront():
return make_client("cloudfront")
+
[email protected](scope="session")
+def sd():
+ """SD client for DiscoverInstances — forces same endpoint (boto3 normally prefixes data-)."""
+ from botocore.config import Config as BotoConfig
+ return boto3.client(
+ "servicediscovery",
+ endpoint_url=ENDPOINT,
+ aws_access_key_id="test",
+ aws_secret_access_key="test",
+ region_name=REGION,
+ config=BotoConfig(
+ region_name=REGION,
+ retries={"max_attempts": 0},
+ inject_host_prefix=False,
+ ),
+ )
diff --git a/tests/test_services.py b/tests/test_services.py
index cef5dc79..d84bf8dc 100644
--- a/tests/test_services.py
+++ b/tests/test_services.py
@@ -19994,6 +19994,195 @@ def test_cfn_stack_with_s3_lambda_dynamodb(cfn, s3, lam, ddb):
assert len(active) == 0
+def test_servicediscovery_flow(sd):
+ # 1. Create Private DNS Namespace
+ ns_name = "example.terraform.local"
+ resp = sd.create_private_dns_namespace(
+ Name=ns_name,
+ Description="example",
+ Vpc="vpc-12345"
+ )
+ op_id = resp["OperationId"]
+ assert op_id
+
+ # Verify Operation
+ op = sd.get_operation(OperationId=op_id)["Operation"]
+ assert op["Status"] == "SUCCESS"
+ ns_id = op["Targets"]["NAMESPACE"]
+
+ # Verify Namespace
+ ns = sd.get_namespace(Id=ns_id)["Namespace"]
+ assert ns["Name"] == ns_name
+
+ # Verify Hosted Zone integration
+ props = ns.get("Properties", {})
+ dns_props = props.get("DnsProperties", {})
+ hz_id = dns_props.get("HostedZoneId")
+ assert hz_id, f"Expected HostedZoneId in namespace properties: {ns}"
+
+ from conftest import make_client
+ r53 = make_client("route53")
+ hz = r53.get_hosted_zone(Id=hz_id)["HostedZone"]
+ assert hz["Name"] == ns_name + "."
+ assert hz["Config"]["PrivateZone"] is True
+
+ # 2. Create Service
+ svc_name = "example-service"
+ resp = sd.create_service(
+ Name=svc_name,
+ NamespaceId=ns_id,
+ DnsConfig={
+ "DnsRecords": [{"Type": "A", "TTL": 10}],
+ "RoutingPolicy": "MULTIVALUE"
+ }
+ )
+ svc_id = resp["Service"]["Id"]
+ assert svc_id
+
+ # 3. Register Instance
+ inst_id = "example-instance-id"
+ resp = sd.register_instance(
+ ServiceId=svc_id,
+ InstanceId=inst_id,
+ Attributes={
+ "AWS_INSTANCE_IPV4": "172.18.0.1",
+ "custom_attribute": "custom"
+ }
+ )
+ assert resp["OperationId"]
+
+ # 4. Discover Instances
+ resp = sd.discover_instances(
+ NamespaceName=ns_name,
+ ServiceName=svc_name
+ )
+ instances = resp["Instances"]
+ assert len(instances) == 1
+ assert instances[0]["InstanceId"] == inst_id
+ assert instances[0]["Attributes"]["AWS_INSTANCE_IPV4"] == "172.18.0.1"
+
+ # 5. List Operations
+ namespaces = sd.list_namespaces()["Namespaces"]
+ assert any(n["Id"] == ns_id for n in namespaces)
+
+ services = sd.list_services()["Services"]
+ assert any(s["Id"] == svc_id for s in services)
+
+ insts = sd.list_instances(ServiceId=svc_id)["Instances"]
+ assert any(i["Id"] == inst_id for i in insts)
+
+ # 6. Deregister & Delete
+ sd.deregister_instance(ServiceId=svc_id, InstanceId=inst_id)
+ insts = sd.list_instances(ServiceId=svc_id)["Instances"]
+ assert len(insts) == 0
+
+ sd.delete_service(Id=svc_id)
+ sd.delete_namespace(Id=ns_id)
+
+
+def test_servicediscovery_tagging(sd):
+ # 1. Create Namespace with tags
+ ns_name = "tag-test-ns"
+ resp = sd.create_http_namespace(
+ Name=ns_name,
+ Tags=[{"Key": "Owner", "Value": "TeamA"}]
+ )
+ op_id = resp["OperationId"]
+ op = sd.get_operation(OperationId=op_id)["Operation"]
+ ns_id = op["Targets"]["NAMESPACE"]
+ ns = sd.get_namespace(Id=ns_id)["Namespace"]
+ ns_arn = ns["Arn"]
+
+ # 2. List tags
+ resp = sd.list_tags_for_resource(ResourceARN=ns_arn)
+ assert any(t["Key"] == "Owner" and t["Value"] == "TeamA" for t in resp["Tags"])
+
+ # 3. Add more tags
+ sd.tag_resource(
+ ResourceARN=ns_arn,
+ Tags=[{"Key": "Env", "Value": "Dev"}]
+ )
+ resp = sd.list_tags_for_resource(ResourceARN=ns_arn)
+ assert len(resp["Tags"]) == 2
+
+ # 4. Untag
+ sd.untag_resource(ResourceARN=ns_arn, TagKeys=["Owner"])
+ resp = sd.list_tags_for_resource(ResourceARN=ns_arn)
+ assert len(resp["Tags"]) == 1
+ assert resp["Tags"][0]["Key"] == "Env"
+
+ # Cleanup
+ sd.delete_namespace(Id=ns_id)
+
+
+def test_servicediscovery_additional_operations(sd):
+ ns_name = "ops-test.local"
+ ns_op = sd.create_private_dns_namespace(
+ Name=ns_name,
+ Description="ops test",
+ Vpc="vpc-12345",
+ )
+ ns_id = sd.get_operation(OperationId=ns_op["OperationId"])["Operation"]["Targets"]["NAMESPACE"]
+
+ svc = sd.create_service(
+ Name="ops-service",
+ NamespaceId=ns_id,
+ DnsConfig={"DnsRecords": [{"Type": "A", "TTL": 10}], "RoutingPolicy": "MULTIVALUE"},
+ )["Service"]
+ svc_id = svc["Id"]
+
+ # service attributes CRUD
+ sd.update_service_attributes(ServiceId=svc_id, Attributes={"team": "core", "env": "test"})
+ attrs = sd.get_service_attributes(ServiceId=svc_id)["ServiceAttributes"]["Attributes"]
+ assert attrs["team"] == "core"
+ assert attrs["env"] == "test"
+
+ sd.delete_service_attributes(ServiceId=svc_id, Attributes=["env"])
+ attrs = sd.get_service_attributes(ServiceId=svc_id)["ServiceAttributes"]["Attributes"]
+ assert "env" not in attrs
+ assert attrs["team"] == "core"
+
+ # namespace/service update operations
+ ns_update_op = sd.update_private_dns_namespace(
+ Id=ns_id,
+ UpdaterRequestId="upd-ns-1",
+ Namespace={"Description": "updated namespace"},
+ )["OperationId"]
+ assert sd.get_operation(OperationId=ns_update_op)["Operation"]["Targets"]["NAMESPACE"] == ns_id
+
+ svc_update_op = sd.update_service(
+ Id=svc_id,
+ Service={"Description": "updated service"},
+ )["OperationId"]
+ assert sd.get_operation(OperationId=svc_update_op)["Operation"]["Targets"]["SERVICE"] == svc_id
+
+ # operations listing
+ ops = sd.list_operations(MaxResults=50)["Operations"]
+ assert any(o["Id"] == ns_update_op for o in ops)
+ assert any(o["Id"] == svc_update_op for o in ops)
+
+ # instance health + revision
+ sd.register_instance(
+ ServiceId=svc_id,
+ InstanceId="inst-1",
+ Attributes={"AWS_INSTANCE_IPV4": "10.0.0.1"},
+ )
+ rev_before = sd.discover_instances_revision(NamespaceName=ns_name, ServiceName="ops-service")["InstancesRevision"]
+
+ sd.update_instance_custom_health_status(ServiceId=svc_id, InstanceId="inst-1", Status="UNHEALTHY")
+ health = sd.get_instances_health_status(ServiceId=svc_id)["Status"]
+ assert health["inst-1"] == "UNHEALTHY"
+
+ discovered = sd.discover_instances(NamespaceName=ns_name, ServiceName="ops-service", HealthStatus="ALL")["Instances"]
+ assert discovered[0]["HealthStatus"] == "UNHEALTHY"
+
+ rev_after = sd.discover_instances_revision(NamespaceName=ns_name, ServiceName="ops-service")["InstancesRevision"]
+ assert rev_after > rev_before
+
+ # cleanup
+ sd.deregister_instance(ServiceId=svc_id, InstanceId="inst-1")
+ sd.delete_service(Id=svc_id)
+ sd.delete_namespace(Id=ns_id)
# ---------------------------------------------------------------------------
# CloudFront v1.1.42 — tags
# ---------------------------------------------------------------------------