Skip to content

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

feat(bedrock): ListFoundationModelAgreementOffers and CreateFoundationModelAgreement (#2031)

Nahuel Nucerae12e9b725978
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0710740c..8a2cb903 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
### Fixed
+- **Bedrock — foundation model agreement offers** — `ListFoundationModelAgreementOffers` always returned an empty `offers` list, so there was no `offerToken` to pass to `CreateFoundationModelAgreement`. It now returns one offer for third-party models (Amazon models have none), and `CreateFoundationModelAgreement` requires `offerToken` and answers `ResourceNotFoundException` for an unknown model. `GetFoundationModelAvailability` reports a third-party model's `agreementAvailability` as `AVAILABLE` only while an agreement exists, so the Terraform/OpenTofu `aws_bedrock_foundation_model_agreement` resource can be destroyed. Reported by @wparad.
- **RDS — MySQL instances apply their DB parameter group** — a group's parameters were stored but never reached the server. A MySQL or MariaDB instance now starts with its group's set parameters as server options; `ModifyDBParameterGroup` with `ApplyMethod=immediate` applies dynamic parameters to running instances (`SET GLOBAL`), and `ResetDBParameterGroup` returns them to the engine default; an `immediate` change to a static parameter is refused with `InvalidParameterCombination`, as AWS refuses it, when a running instance uses the group (MiniStack has no parameter list, so with none it is stored); a `pending-reboot` change reports `ParameterApplyStatus: pending-reboot` until the instance next starts. Names and values are not validated against the engine's parameter list, and formula values (`{DBInstanceClassMemory*3/4}`) are not evaluated. Contributed by @skialpine.
- **API Gateway — `GetApiKeys` filters by `nameQuery`** — `GetApiKeys` ignored `nameQuery` and returned all the API keys, so a lookup by name could get the wrong key. It now returns only the keys whose names start with `nameQuery`.
- **RDS — creating an existing parameter group is refused** — `CreateDBParameterGroup` and `CreateDBClusterParameterGroup` with a name already in use replaced the group with an empty one, dropping its parameters; they now return `DBParameterGroupAlreadyExists`, as AWS does. Contributed by @skialpine.
diff --git a/ministack/services/bedrock.py b/ministack/services/bedrock.py
index c7a0868b..df8683e9 100644
--- a/ministack/services/bedrock.py
+++ b/ministack/services/bedrock.py
@@ -273,6 +273,7 @@ _prompt_routers = AccountRegionScopedDict()
_invocation_logging_config = AccountRegionScopedDict() # 'config' -> dict (singleton)
_tags = AccountRegionScopedDict() # arn -> {key: value}
_USE_CASE = AccountRegionScopedDict() # 'usecase' -> dict (model-access use case)
+_model_agreements = AccountRegionScopedDict() # model id -> True (CreateFoundationModelAgreement)
def reset():
@@ -282,7 +283,8 @@ def reset():
_model_import_jobs, _model_copy_jobs,
_model_invocation_jobs, _evaluation_jobs,
_marketplace_endpoints, _prompt_routers,
- _invocation_logging_config, _tags, _USE_CASE):
+ _invocation_logging_config, _tags, _USE_CASE,
+ _model_agreements):
store.clear()
@@ -304,6 +306,7 @@ def get_state():
"invocation_logging_config": _invocation_logging_config,
"tags": _tags,
"use_case": _USE_CASE,
+ "model_agreements": _model_agreements,
})
@@ -330,6 +333,7 @@ def _restore_state(data):
_invocation_logging_config.update(data.get("invocation_logging_config", {}))
_tags.update(data.get("tags", {}))
_USE_CASE.update(data.get("use_case", {}))
+ _model_agreements.update(data.get("model_agreements", {}))
@@ -426,11 +430,13 @@ def _get_foundation_model(identifier: str) -> tuple:
def _get_foundation_model_availability(model_id: str) -> tuple:
- if _find_model(model_id) is None:
+ entry = _find_model(model_id)
+ if entry is None:
return _not_found(f"Could not find model {model_id}.")
+ agreed = entry[2] == "Amazon" or entry[0] in _model_agreements
return _json({
"ModelId": model_id,
- "AgreementAvailability": {"Status": "AVAILABLE"},
+ "AgreementAvailability": {"Status": "AVAILABLE" if agreed else "NOT_AVAILABLE"},
"AuthorizationStatus": "AUTHORIZED",
"EntitlementAvailability": "AVAILABLE",
"RegionAvailability": "AVAILABLE",
@@ -438,9 +444,21 @@ def _get_foundation_model_availability(model_id: str) -> tuple:
def _list_foundation_model_agreement_offers(model_id: str) -> tuple:
- if _find_model(model_id) is None:
+ entry = _find_model(model_id)
+ if entry is None:
return _not_found(f"Could not find model {model_id}.")
- return _json({"ModelId": model_id, "Offers": []})
+ if entry[2] == "Amazon":
+ return _json({"ModelId": model_id, "Offers": []})
+ offer_id = str(uuid.uuid5(uuid.NAMESPACE_URL, f"bedrock-offer:{model_id}"))
+ return _json({"ModelId": model_id, "Offers": [{
+ "OfferId": offer_id,
+ "OfferToken": offer_id,
+ "TermDetails": {
+ "UsageBasedPricingTerm": {"RateCard": []},
+ "LegalTerm": {},
+ "SupportTerm": {},
+ },
+ }]})
def _create_foundation_model_agreement(body) -> tuple:
@@ -449,6 +467,12 @@ def _create_foundation_model_agreement(body) -> tuple:
return err
if not body_obj.get("modelId"):
return _validation("modelId is required.")
+ if not body_obj.get("offerToken"):
+ return _validation("offerToken is required.")
+ entry = _find_model(body_obj["modelId"])
+ if entry is None:
+ return _not_found(f"Could not find model {body_obj['modelId']}.")
+ _model_agreements[entry[0]] = True
return _json({"ModelId": body_obj["modelId"]}, status=202)
@@ -458,6 +482,9 @@ def _delete_foundation_model_agreement(body) -> tuple:
return err
if not body_obj.get("modelId"):
return _validation("modelId is required.")
+ entry = _find_model(body_obj["modelId"])
+ if entry is not None:
+ _model_agreements.pop(entry[0], None)
return _empty(status=202)
diff --git a/tests/test_bedrock.py b/tests/test_bedrock.py
index 4441d9cd..814ab80e 100644
--- a/tests/test_bedrock.py
+++ b/tests/test_bedrock.py
@@ -1707,6 +1707,45 @@ def test_bedrock_list_foundation_model_agreement_offers():
assert "offers" in resp
+def test_bedrock_agreement_offer_token_creates_an_agreement():
+ model_id = "anthropic.claude-3-haiku-20240307-v1:0"
+ offers = _bedrock().list_foundation_model_agreement_offers(modelId=model_id)["offers"]
+ assert len(offers) == 1
+ assert set(offers[0]["termDetails"]) == {"usageBasedPricingTerm", "legalTerm", "supportTerm"}
+ resp = _bedrock().create_foundation_model_agreement(
+ offerToken=offers[0]["offerToken"], modelId=model_id,
+ )
+ assert resp["ResponseMetadata"]["HTTPStatusCode"] == 202
+ assert resp["modelId"] == model_id
+
+
+def test_bedrock_agreement_lifecycle_drives_availability():
+ """The Terraform/OpenTofu agreement resource waits on agreementAvailability
+ reaching AVAILABLE after create and NOT_AVAILABLE after delete."""
+ model_id = "anthropic.claude-3-haiku-20240307-v1:0"
+ client = _bedrock()
+ token = client.list_foundation_model_agreement_offers(modelId=model_id)["offers"][0]["offerToken"]
+ client.create_foundation_model_agreement(offerToken=token, modelId=model_id)
+ status = client.get_foundation_model_availability(modelId=model_id)["agreementAvailability"]["status"]
+ assert status == "AVAILABLE"
+ client.delete_foundation_model_agreement(modelId=model_id)
+ status = client.get_foundation_model_availability(modelId=model_id)["agreementAvailability"]["status"]
+ assert status == "NOT_AVAILABLE"
+ amazon = client.get_foundation_model_availability(modelId="amazon.titan-text-express-v1")
+ assert amazon["agreementAvailability"]["status"] == "AVAILABLE"
+
+
+def test_bedrock_amazon_models_have_no_agreement_offers():
+ resp = _bedrock().list_foundation_model_agreement_offers(modelId="amazon.titan-text-express-v1")
+ assert resp["offers"] == []
+
+
+def test_bedrock_create_foundation_model_agreement_unknown_model():
+ with pytest.raises(botocore.exceptions.ClientError) as exc:
+ _bedrock().create_foundation_model_agreement(offerToken="t", modelId="nope.model-v1:0")
+ assert exc.value.response["Error"]["Code"] == "ResourceNotFoundException"
+
+
# ---------------------------------------------------------------------------
# Tagging
# ---------------------------------------------------------------------------