{"record":{"id":"8ededacf4f7f80e7","repo":"floci-io/floci","slug":"namespacealreadyexists","errorCode":"NamespaceAlreadyExists","errorMessage":"A namespace named \\\"{}\\\" already exists.","messagePattern":"A namespace named \\\\\"(.+?)\\\\\" already exists\\.","errorType":"exception","errorClass":"AwsException","httpStatus":400,"severity":"error","filePath":"src/main/java/io/github/hectorvent/floci/services/cloudmap/CloudMapService.java","lineNumber":111,"sourceCode":"        if (vpc == null || vpc.isBlank()) {\n            throw new AwsException(\"InvalidInput\", \"Vpc is required for a private DNS namespace.\", 400);\n        }\n        Namespace ns = newNamespace(name, \"DNS_PRIVATE\", description, creatorRequestId, tags, region);\n        ns.setVpc(vpc);\n        ns.setHostedZoneId(generateHostedZoneId());\n        namespaceStore.put(ns.getId(), ns);\n        return submitOperation(\"CREATE_NAMESPACE\", \"NAMESPACE\", ns.getId(), region);\n    }\n\n    private Namespace newNamespace(String name, String type, String description,\n                                   String creatorRequestId, Map<String, String> tags, String region) {\n        if (name == null || name.isBlank()) {\n            throw new AwsException(\"InvalidInput\", \"Namespace name is required.\", 400);\n        }\n        boolean exists = scan(namespaceStore).stream()\n                .anyMatch(n -> region.equals(n.getRegion()) && name.equals(n.getName()));\n        if (exists) {\n            throw new AwsException(\"NamespaceAlreadyExists\",\n                    \"A namespace named \\\"\" + name + \"\\\" already exists.\", 400);\n        }\n        Namespace ns = new Namespace();\n        ns.setId(\"ns-\" + randomId(20));\n        ns.setName(name);\n        ns.setType(type);\n        ns.setDescription(description);\n        ns.setCreatorRequestId(creatorRequestId != null ? creatorRequestId : UUID.randomUUID().toString());\n        ns.setCreateDate(Instant.now());\n        ns.setRegion(region);\n        ns.setServiceCount(0);\n        if (tags != null) {\n            ns.setTags(tags);\n        }\n        ns.setArn(regionResolver.buildArn(\"servicediscovery\", region, \"namespace/\" + ns.getId()));\n        return ns;\n    }\n","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/floci-io/floci/blob/62ff490619e7bd3554597c28c704081b4c15add5/src/main/java/io/github/hectorvent/floci/services/cloudmap/CloudMapService.java#L93-L129","documentation":"Cloud Map namespace names must be unique within a region. Before creating a namespace, Floci scans existing namespaces for the same region + name pair and throws NamespaceAlreadyExists (HTTP 400) with a message naming the duplicate. This fires across all namespace types (DNS_PUBLIC, DNS_PRIVATE, HTTP) because the check lives in the shared newNamespace helper.","triggerScenarios":"Running CreatePublicDnsNamespace/CreatePrivateDnsNamespace/CreateHttpNamespace twice with the same Name in the same region without a distinct CreatorRequestId; re-running a bootstrap script that is not idempotent.","commonSituations":"Provisioning scripts run twice; CI pipelines that create namespaces per run with a fixed name; two services claiming the same domain name. Note AWS treats identical CreatorRequestId as idempotent, but Floci's duplicate check fires on the name alone.","solutions":["Check ListNamespaces for the name in that region first and reuse the existing namespace if present","Catch NamespaceAlreadyExists and treat it as success in idempotent bootstrap code","If duplicate names are legitimate, disambiguate the name per environment (e.g. app.prod.internal vs app.dev.internal)"],"exampleFix":"// before\ncloudMapClient.createPrivateDnsNamespace(r -> r\n    .name(\"app.internal\")\n    .vpc(vpcId)); // throws on second run\n\n// after\nboolean exists = cloudMapClient.listNamespaces().namespaces().stream()\n    .anyMatch(n -> \"app.internal\".equals(n.name()));\nif (!exists) {\n    cloudMapClient.createPrivateDnsNamespace(r -> r\n        .name(\"app.internal\")\n        .vpc(vpcId));\n}","handlingStrategy":"try-catch","validationCode":"boolean exists = cloudMapClient.listNamespaces().namespaces().stream()\n    .anyMatch(n -> name.equals(n.name()));\nif (exists) {\n    // reuse the existing namespace instead of creating a new one\n}","typeGuard":null,"tryCatchPattern":"try {\n    cloudMapClient.createPrivateDnsNamespace(r -> r.name(name).vpc(vpcId));\n} catch (ServiceDiscoveryException e) {\n    if (\"NamespaceAlreadyExists\".equals(e.awsErrorDetails().errorCode())) {\n        // idempotent bootstrap: find and reuse the existing namespace\n    } else { throw e; }\n}","preventionTips":["Make bootstrap scripts idempotent: list first, create only if missing","Include environment in namespace names when multiple environments share an emulator"],"tags":["cloudmap","namespace","duplicate","idempotency"],"backgroundTag":null,"analyzedSha":"62ff490619e7bd3554597c28c704081b4c15add5","analyzedAt":"2026-08-14T14:25:23.764Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}