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(rds): return DBInstanceAlreadyExists wire code for duplicate DB instances (#1297)
kiran01bm4694d01630f5
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 319b5a64..91f034b5 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).## [Unreleased]+### Fixed+- **RDS — duplicate DB instance wire code matches AWS** — a duplicate `CreateDBInstance`, `CreateDBInstanceReadReplica`, or `RestoreDBInstanceFromDBSnapshot` target returned wire code `DBInstanceAlreadyExistsFault`, but real AWS omits the `Fault` suffix for instance-level error codes (cluster-level codes such as `DBClusterAlreadyExistsFault` keep it). SDKs match the exact string to produce their typed error — aws-sdk-go-v2, for example, deserialized the response as a generic API error instead of the typed `DBInstanceAlreadyExistsFault`. The wire code is now `DBInstanceAlreadyExists`. This deliberately reverts the v1.1.18 change, which mistook the SDK exception *shape name* for the *wire code* — the model's `error.code` field (`DBInstanceAlreadyExists`) is what SDKs match, and exact-string tests now pin the correct direction. Contributed by @kiran01bm.### Added- **CodeBuild - builds can really run (`MINISTACK_CODEBUILD_EXECUTE=1`)** - `StartBuild` returned a build that was already `SUCCEEDED`, so a pipeline rehearsed against MiniStack reported a pass without a single phase having run, and a buildspec that fails on AWS still looked green locally. With the flag set, `StartBuild` returns `IN_PROGRESS` and the project's inline buildspec is handed to the official AWS CodeBuild local agent (`public.ecr.aws/codebuild/local-builds`), which runs the phases in the project's `environment.image` the way CodeBuild does - the phase semantics come from AWS's own agent instead of a reimplemented executor. `BatchGetBuilds` reflects progress while the build runs: the agent's `Phase complete: <PHASE> State: <STATUS>` lines become `phases` entries, and the container's exit status maps to `SUCCEEDED` / `FAILED`, with `FAULT` when Docker is unreachable. `environment.environmentVariables`, `privilegedMode`, and the `CODEBUILD_BUILD_ID` / `_ARN` / `_NUMBER` / `_INITIATOR` variables reach the build. The build reaches back into MiniStack: `AWS_ENDPOINT_URL` (plus placeholder credentials) is injected unless the project declares its own, so `aws s3 cp` inside a build hits this emulator rather than real AWS. `timeoutInMinutes` is enforced (the build is stopped and its phase reported `TIMED_OUT`), and a restored build that was in flight when MiniStack stopped is reported `FAULT` rather than polling as running forever. `StopBuild` is honoured deterministically: it records the intent before removing the container, so the worker reports the requested `STOPPED` instead of racing it to `FAULT`. Build output also reaches CloudWatch Logs: the record already advertised `logs.groupName` / `logs.streamName`, and an executed build now writes the agent output there, so `aws logs get-log-events` and `aws logs tail` read back a real build log instead of an empty stream. Default behaviour is unchanged - without the flag builds stay metadata-only. Requires the Docker socket; the agent image and workspace path are fixed internals, not configurable.diff --git a/ministack/services/rds.py b/ministack/services/rds.pyindex 0ec71776..56bcf35f 100644--- a/ministack/services/rds.py+++ b/ministack/services/rds.py@@ -2376,7 +2376,7 @@ def _create_db_instance(p):if not db_id:return _error("MissingParameter", "DBInstanceIdentifier is required", 400)if db_id in _instances:- return _error("DBInstanceAlreadyExistsFault", f"DB instance {db_id} already exists", 400)+ return _error("DBInstanceAlreadyExists", f"DB instance {db_id} already exists", 400)engine = _p(p, "Engine") or "postgres"explicit_engine_version = _p(p, "EngineVersion")@@ -3173,7 +3173,7 @@ def _create_read_replica(p):return _error("DBInstanceNotFound", f"DBInstance {source_id} not found.", 404)source_id = source["DBInstanceIdentifier"]if replica_id in _instances:- return _error("DBInstanceAlreadyExistsFault", f"DBInstance {replica_id} already exists.", 400)+ return _error("DBInstanceAlreadyExists", f"DBInstance {replica_id} already exists.", 400)arn = f"arn:aws:rds:{get_region()}:{get_account_id()}:db:{replica_id}"replica = dict(source)@@ -3214,7 +3214,7 @@ def _restore_from_snapshot(p):snap_id = _p(p, "DBSnapshotIdentifier")if db_id in _instances:- return _error("DBInstanceAlreadyExistsFault", f"DBInstance {db_id} already exists.", 400)+ return _error("DBInstanceAlreadyExists", f"DBInstance {db_id} already exists.", 400)snap = _snapshots.get(snap_id)if not snap:diff --git a/tests/test_rds.py b/tests/test_rds.pyindex a955c806..4c99300d 100644--- a/tests/test_rds.py+++ b/tests/test_rds.py@@ -71,6 +71,35 @@ def test_rds_create(rds):assert instances[0]["Engine"] == "postgres"assert "Address" in instances[0]["Endpoint"]+def test_rds_create_duplicate_instance(rds):+ """Duplicate CreateDBInstance returns wire code DBInstanceAlreadyExists.++ Real AWS omits the ``Fault`` suffix for instance-level codes (unlike the+ cluster-level ``DBClusterAlreadyExistsFault``); SDKs match the exact string+ to produce their typed error, so the suffix breaks typed error handling.+ """+ rds.create_db_instance(+ DBInstanceIdentifier="dup-create-db",+ DBInstanceClass="db.t3.micro",+ Engine="postgres",+ MasterUsername="admin",+ MasterUserPassword="password123",+ AllocatedStorage=20,+ )+ try:+ with pytest.raises(ClientError) as exc:+ rds.create_db_instance(+ DBInstanceIdentifier="dup-create-db",+ DBInstanceClass="db.t3.micro",+ Engine="postgres",+ MasterUsername="admin",+ MasterUserPassword="password123",+ AllocatedStorage=20,+ )+ assert exc.value.response["Error"]["Code"] == "DBInstanceAlreadyExists"+ finally:+ rds.delete_db_instance(DBInstanceIdentifier="dup-create-db", SkipFinalSnapshot=True)+def test_rds_create_rejects_unknown_parameter_group(rds):"""CreateDBInstance / CreateDBCluster reject a reference to a parameter groupthat doesn't exist; a created group (or a default.* group) is accepted (#1278)."""@@ -2826,7 +2855,7 @@ def test_rds_create_read_replica(rds):DBInstanceIdentifier="rr-replica",SourceDBInstanceIdentifier="rr-source",)- assert exc.value.response["Error"]["Code"] == "DBInstanceAlreadyExistsFault"+ assert exc.value.response["Error"]["Code"] == "DBInstanceAlreadyExists"finally:rds.delete_db_instance(DBInstanceIdentifier="rr-replica", SkipFinalSnapshot=True)rds.delete_db_instance(DBInstanceIdentifier="rr-source", SkipFinalSnapshot=True)@@ -2905,7 +2934,7 @@ def test_rds_restore_from_snapshot(rds):DBInstanceIdentifier="restored-db",DBSnapshotIdentifier="restore-snap",)- assert exc.value.response["Error"]["Code"] == "DBInstanceAlreadyExistsFault"+ assert exc.value.response["Error"]["Code"] == "DBInstanceAlreadyExists"finally:rds.delete_db_instance(DBInstanceIdentifier="restored-db", SkipFinalSnapshot=True)rds.delete_db_snapshot(DBSnapshotIdentifier="restore-snap")