Skip to content

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

fix(apigw-v2): honour ms-custom-id tag in CloudFormation provisioner (#946)

Michael Smith103855943dea
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index cfa17732..52b55a26 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -3524,7 +3524,7 @@ def _cw_metric_alarm_delete(physical_id, props):
# ---------------------------------------------------------------------------
def _apigw_v2_api_create(logical_id, props, stack_name):
- api_id = new_uuid()[:8]
+ api_id = _apigw_v2._resolve_custom_api_id(props.get("Tags", {}), _apigw_v2._apis) or new_uuid()[:8]
name = props.get("Name") or _physical_name(stack_name, logical_id, max_len=128)
protocol = props.get("ProtocolType", "HTTP")
api = {
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index 8e419053..aad8ec7c 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -2707,6 +2707,38 @@ def test_cfn_apigwv2_integration_basic(cfn, apigw):
assert apigw.get_integrations(ApiId=api_id)["Items"] == []
+def test_cfn_apigwv2_ms_custom_id(cfn, apigw):
+ """CloudFormation ms-custom-id tag pins the ApiGatewayV2 API id (issue #400)."""
+ template = {
+ "AWSTemplateFormatVersion": "2010-09-09",
+ "Resources": {
+ "HttpApi": {
+ "Type": "AWS::ApiGatewayV2::Api",
+ "Properties": {
+ "Name": "cfn-apigwv2-custom-id-t01",
+ "ProtocolType": "HTTP",
+ "Tags": {"ms-custom-id": "cfn-pinned-api"},
+ },
+ },
+ },
+ }
+ stack_name = "cfn-apigwv2-custom-id-t01"
+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(template))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE"
+
+ resources = cfn.describe_stack_resources(StackName=stack_name)["StackResources"]
+ api_res = [r for r in resources if r["ResourceType"] == "AWS::ApiGatewayV2::Api"][0]
+ assert api_res["PhysicalResourceId"] == "cfn-pinned-api"
+
+ api = apigw.get_api(ApiId="cfn-pinned-api")
+ assert api["ApiId"] == "cfn-pinned-api"
+ assert api["Name"] == "cfn-apigwv2-custom-id-t01"
+
+ cfn.delete_stack(StackName=stack_name)
+ _wait_stack(cfn, stack_name)
+
+
def test_cfn_apigwv2_route_basic(cfn, apigw):
"""CFN stack with ApiGatewayV2 Api + Integration + Route deploys successfully."""
template = {