Files
floci/docs/services/cloudmap.md
T
Abhigya 04878b8c57 feat(cloudmap): resolve DNS namespaces and register ECS tasks in them (#4122)
* feat(cloudmap): resolve DNS namespaces and register ECS tasks in them

Cloud Map's control plane already accepted namespaces, services and
instances and read them back correctly, but nothing answered DNS for
<service>.<namespace>. A container that registered successfully still
failed to resolve its peers, and the gap only surfaced at application
runtime because every API call looked right.

The embedded DNS server now consults DnsRecordSource implementations
before forwarding a query upstream, and Cloud Map provides one: a DNS
namespace resolves to the AWS_INSTANCE_IPV4 of the instances registered
under it, one A record each. The SPI keeps the direction right, since
core must not import a service. Which instances answer follows Route 53:
the healthy ones while any instance is healthy, all of them when none
is, and at most eight records either way. HTTP namespaces do not
resolve, matching AWS, where they are reachable only through
DiscoverInstances.

An ECS service that declares serviceRegistries now registers each running
task as an instance of the named Cloud Map service, and deregisters it
when the task stops, mirroring EcsLoadBalancerRegistrar. The address is
the task's ENI address on an awsvpc task and the container's Docker
network address otherwise, which is the one a peer container can reach.
Alongside it the instance carries the metadata attributes the ECS
service discovery documentation lists: AVAILABILITY_ZONE, REGION,
ECS_SERVICE_NAME, ECS_CLUSTER_NAME and ECS_TASK_DEFINITION_FAMILY.
serviceRegistries stays raw: registryArn, containerName and
containerPort are the only members anything acts on.

* test(ecs): pin the Cloud Map registration hooks with a docker mode test

EcsServiceDiscoveryRegistrarTest drives the registrar directly, so removing
EcsService's calls to it left every ECS test green. The registration only runs
in docker mode, which the suite mocks out, so nothing covered the wiring.

The new test runs ECS unmocked under a profile, creates a service that declares
serviceRegistries, and drives one reconciler tick to launch a real container. It
asserts the Cloud Map name resolves to the task's ENI address while it runs, and
that stopping the task removes the instance and the answer. Commenting out
either hook fails it: the register hook on the resolve assertion, the deregister
hook on the instance one.

Security-group enforcement goes back to its shipped default of off for the
profile. The suite turns it on and its helper needs rootful Docker, which is a
requirement of security groups rather than of service discovery.

Also corrects the health status deviation note. An instance registered without
AWS_INIT_HEALTH_STATUS starts HEALTHY on AWS too, so the deviation is not the
initial status but that Floci never moves it: AWS registers an ECS task
UNHEALTHY and promotes it once the container health check passes, for a service
that declares HealthCheckCustomConfig.

* fix(cloudmap): retain DNS ownership and ECS registry targets

* fix(cloudmap): keep namespace apex queries authoritative

* style(dns): remove em dash from test heading
2026-09-25 11:11:28 -04:00

146 lines
6.8 KiB
Markdown

# AWS Cloud Map
**Protocol:** JSON 1.1
**Header:** `X-Amz-Target: Route53AutoNaming_v20170314.<Action>`
**Endpoint prefix:** `servicediscovery`
Floci emulates the AWS Cloud Map (service discovery) control plane in-process:
namespaces, services, and registered instances are stored as account-aware
domain objects, and discovery queries resolve against that in-memory state.
Asynchronous operations (`CreateHttpNamespace`, `CreatePublicDnsNamespace`,
`CreatePrivateDnsNamespace`, `DeleteNamespace`, `RegisterInstance`,
`DeregisterInstance`) apply their effect synchronously and return an operation
id. The operation reaches `SUCCESS` immediately by default, so a follow-up
`GetOperation` call returns a completed operation without polling. No Route 53
hosted zones are created: public and private DNS namespaces are assigned a
synthetic `HostedZoneId` so SDK and CLI clients can exercise the full Cloud Map
control flow locally.
### DNS resolution
A DNS namespace is answerable, not just storable. Floci's embedded DNS server
resolves `<service>.<namespace>` to the `AWS_INSTANCE_IPV4` of the instances
registered under that service, one A record each, so a container that uses Floci
as its resolver reaches its peers by name. Instances registered directly through
`RegisterInstance` and instances an ECS service registers through its
`serviceRegistries` resolve the same way.
Which instances answer follows Route 53: the healthy ones while any instance is
healthy, and all of them when none is, so a service whose instances have all gone
unhealthy still resolves rather than disappearing from DNS. At most eight records
go back either way, as Route 53 answers a service discovery query.
Names inside a DNS namespace stay local when no instance has a usable address:
the resolver answers negatively instead of forwarding them to upstream DNS.
Three limits are worth knowing. `HTTP` namespaces do not resolve, matching AWS,
where they are reachable only through `DiscoverInstances`. Only A records are
served, so a name that AWS would answer with an `SRV` record answers with the
address alone. And the embedded DNS server only runs when Floci itself runs
inside Docker, so name resolution is available to containers, not to processes on
the host.
## Supported Operations
| Operation | Notes |
|-----------|-------|
| `CreateHttpNamespace` | Creates an `HTTP` namespace; returns an operation id |
| `CreatePublicDnsNamespace` | Creates a `DNS_PUBLIC` namespace with a synthetic `HostedZoneId` |
| `CreatePrivateDnsNamespace` | Creates a `DNS_PRIVATE` namespace; requires `Vpc`, assigns a `HostedZoneId` |
| `GetNamespace` | Returns a namespace by `Id` |
| `ListNamespaces` | Lists namespaces in the current region |
| `DeleteNamespace` | Deletes an empty namespace; fails with `ResourceInUse` if it still has services |
| `CreateService` | Creates a service (optionally under a namespace), tracks revision and instance count |
| `GetService` | Returns a service by `Id` |
| `ListServices` | Lists services, optionally filtered by `NamespaceId` |
| `DeleteService` | Deletes a service with no instances; fails with `ResourceInUse` otherwise |
| `RegisterInstance` | Registers an instance under a service; requires `InstanceId` and `Attributes`, bumps service revision |
| `DeregisterInstance` | Removes a registered instance and bumps service revision |
| `GetInstance` | Returns a registered instance by `ServiceId` and `InstanceId` |
| `ListInstances` | Lists instances registered under a service |
| `GetInstancesHealthStatus` | Returns health status keyed by instance id, with optional `Instances` filter |
| `DiscoverInstances` | Resolves instances by `NamespaceName` + `ServiceName`, with `HealthStatus` and `QueryParameters` filtering |
| `DiscoverInstancesRevision` | Returns the current revision for a discovered service |
| `GetOperation` | Returns an operation by `Id` |
| `ListOperations` | Lists operations with optional `STATUS`, `TYPE`, `NAMESPACE_ID`, and `SERVICE_ID` filters |
| `TagResource` | Adds tags to a namespace or service ARN |
| `UntagResource` | Removes tag keys from a namespace or service ARN |
| `ListTagsForResource` | Lists tags for a namespace or service ARN |
`DiscoverInstances` supports the `HEALTHY`, `UNHEALTHY`, `HEALTHY_OR_ELSE_ALL`,
and `ALL` health filters, and matches instances whose attributes contain every
key/value pair supplied in `QueryParameters`.
## Configuration
| Variable | Default | Description |
|---|---|---|
| `FLOCI_SERVICES_CLOUDMAP_ENABLED` | `true` | Enable or disable the service |
| `FLOCI_SERVICES_CLOUDMAP_OPERATION_COMPLETION_DELAY_SECONDS` | `0` | Delay before an async operation transitions from `PENDING` to `SUCCESS`; `0` completes immediately |
## Examples
```bash
export AWS_ENDPOINT_URL=http://localhost:4566
export AWS_DEFAULT_REGION=us-east-1
export AWS_ACCESS_KEY_ID=test
export AWS_SECRET_ACCESS_KEY=test
aws servicediscovery create-http-namespace --name floci-demo
aws servicediscovery list-namespaces
aws servicediscovery create-service \
--name backend \
--namespace-id ns-xxxxxxxxxxxxxxxxxxxx
aws servicediscovery register-instance \
--service-id srv-xxxxxxxxxxxxxxxxxxxx \
--instance-id i-0123456789 \
--attributes AWS_INSTANCE_IPV4=10.0.0.10
aws servicediscovery discover-instances \
--namespace-name floci-demo \
--service-name backend
```
```python
import boto3
client = boto3.client(
"servicediscovery",
endpoint_url="http://localhost:4566",
region_name="us-east-1",
)
ns = client.create_http_namespace(Name="floci-demo")
client.get_operation(OperationId=ns["OperationId"]) # Status == "SUCCESS"
namespace_id = client.list_namespaces()["Namespaces"][0]["Id"]
service = client.create_service(Name="backend", NamespaceId=namespace_id)
client.register_instance(
ServiceId=service["Service"]["Id"],
InstanceId="i-0123456789",
Attributes={"AWS_INSTANCE_IPV4": "10.0.0.10"},
)
found = client.discover_instances(NamespaceName="floci-demo", ServiceName="backend")
print(found["Instances"])
```
## Related Docs
- [Services overview](index.md)
- [Route53](route53.md)
- [AWS CLI & SDK setup](../getting-started/aws-setup.md)
- [Environment variables](../configuration/environment-variables.md)
## Out of Scope
- Route 53 hosted zone / record set creation.
- SRV, AAAA and CNAME records: `AWS_INSTANCE_PORT` is stored and returned, but only A records are served.
- Route 53 health checks backing `HealthCheckConfig` (custom health status is stored, not actively probed).
- `AWS_INIT_HEALTH_STATUS` and `AWS_EC2_INSTANCE_ID` on `RegisterInstance`. Every instance registers `HEALTHY`, which is AWS's own initial status when `AWS_INIT_HEALTH_STATUS` is absent, so only a caller that explicitly asks for `UNHEALTHY` sees a difference.
- Cross-region namespace and service discovery. A DNS query carries no region, so a name resolves through whichever namespace copy holds instances.