{"record":{"id":"fd2635a7d9fff9a0","repo":"floci-io/floci","slug":"namealreadyexistsexception","errorCode":"NameAlreadyExistsException","errorMessage":"StackSet already exists: ${name}","messagePattern":"StackSet already exists: (.+?)","errorType":"error_code","errorClass":"AwsException","httpStatus":409,"severity":"error","filePath":"src/main/java/io/github/hectorvent/floci/services/cloudformation/StackSetService.java","lineNumber":64,"sourceCode":"    public StackSetService(CloudFormationService cfnService, StorageFactory storageFactory) {\n        this.cfnService = cfnService;\n        this.stackSets = storageFactory.create(\"cloudformation\", \"cloudformation-stacksets.json\",\n                new TypeReference<Map<String, StackSet>>() {});\n        this.instances = storageFactory.create(\"cloudformation\", \"cloudformation-stackset-instances.json\",\n                new TypeReference<Map<String, StackInstance>>() {});\n        this.operations = storageFactory.create(\"cloudformation\", \"cloudformation-stackset-operations.json\",\n                new TypeReference<Map<String, StackSetOperation>>() {});\n    }\n\n    // ── StackSet lifecycle ─────────────────────────────────────────────────────\n\n    public StackSet createStackSet(String name, String templateBody, Map<String, String> parameters,\n                                   List<String> capabilities, Map<String, String> tags, String description) {\n        if (name == null || name.isBlank()) {\n            throw new AwsException(\"ValidationError\", \"StackSetName must not be empty\", 400);\n        }\n        if (stackSets.get(name).isPresent()) {\n            throw new AwsException(\"NameAlreadyExistsException\",\n                    \"StackSet already exists: \" + name, 409);\n        }\n        // AWS rejects CreateStackSet with no template; the handler resolves TemplateBody/TemplateURL\n        // to null when neither is supplied. Without this guard a later CreateStackInstances would\n        // deploy empty (\"{}\") stacks into every target account.\n        if (templateBody == null || templateBody.isBlank()) {\n            throw new AwsException(\"ValidationError\",\n                    \"Either TemplateBody or TemplateURL must be specified\", 400);\n        }\n        StackSet ss = new StackSet();\n        ss.setStackSetName(name);\n        ss.setStackSetId(name + \":\" + UUID.randomUUID());\n        ss.setTemplateBody(templateBody);\n        ss.setDescription(description);\n        if (parameters != null) {\n            ss.setParameters(new LinkedHashMap<>(parameters));\n        }\n        if (capabilities != null) {","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/floci-io/floci/blob/62ff490619e7bd3554597c28c704081b4c15add5/src/main/java/io/github/hectorvent/floci/services/cloudformation/StackSetService.java#L46-L82","documentation":"NameAlreadyExistsException (HTTP 409) from createStackSet when a StackSet with the same name is already registered in the emulator's stackset storage. StackSet names are unique per account.","triggerScenarios":"Calling CreateStackSet twice with the same name without deleting the first, or re-running a setup script against an emulator whose persistent storage still holds the previous StackSet.","commonSituations":"Idempotent provisioning scripts that create-on-every-run, emulator state persisting between test sessions, or a previous deletion skipped because stack instances remained (DeleteStackSet refuses non-empty sets).","solutions":["Check first: aws --endpoint-url http://localhost:4566 cloudformation describe-stack-set --stack-set-name <name>; only create when absent.","Delete the existing set before recreating — delete stack instances first (DeleteStackInstances), then DeleteStackSet.","For ephemeral test runs, use unique names per session (suffix with a run id) or reset the emulator's persistent storage."],"exampleFix":"# before\naws ... cloudformation create-stack-set --stack-set-name shared --template-body file://t.yml\n\n# after\naws ... cloudformation delete-stack-instances --stack-set-name shared \\\n  --accounts 111122223333 --regions us-east-1 --no-retain-stacks || true\naws ... cloudformation delete-stack-set --stack-set-name shared || true\naws ... cloudformation create-stack-set --stack-set-name shared --template-body file://t.yml","handlingStrategy":"try-catch","validationCode":"boolean exists = false;\ntry {\n    cfn.describeStackSet(req -> req.stackSetName(name));\n    exists = true;\n} catch (CloudFormationException e) { /* absent */ }","typeGuard":null,"tryCatchPattern":"catch CloudFormationException with statusCode 409 / code NameAlreadyExistsException: either delete-and-recreate (instances first, then the set) or adopt the existing set; do not blindly retry create.","preventionTips":["Make provisioning scripts idempotent: describe-then-create","Use unique per-run names in ephemeral test environments","Complete deletion (instances, then set) before recreating the same name"],"tags":["cloudformation","stacksets","already-exists","conflict"],"backgroundTag":null,"analyzedSha":"62ff490619e7bd3554597c28c704081b4c15add5","analyzedAt":"2026-08-14T14:25:23.764Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}