Skip to content

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

fix(cloudformation): provision an API Gateway method's response mappings (#1689)

ppettitau3632cce8425f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9eec2308..1c30aef3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **CloudFormation — an `AWS::ApiGateway::Method`'s `MethodResponses` and `IntegrationResponses` are provisioned** — the provisioner read the method and integration properties and discarded both response lists, so every REST API deployed from a template lost its mapped response headers. The visible casualty was the CDK's `defaultCorsPreflightOptions`, whose generated `OPTIONS` method returns the `Access-Control-Allow-*` headers through a MOCK integration's `responseParameters`: the preflight answered without a single CORS header and the browser blocked the request. That became load-bearing once `COGNITO_USER_POOLS` authorizers were enforced in 1.5.10, because the preflight can no longer fall through to a proxy integration and be answered by the application's own CORS middleware. Both lists are now provisioned onto the method, where the existing request-time mapping picks them up, and a stack update reprovisions them from the template. Contributed by @ppettitau.
+
- **Transcribe — `StartTranscriptionJob` returns a job already `IN_PROGRESS`** — the start response reported `QUEUED` with no `StartTime`, and the job reached `IN_PROGRESS` only from the background worker, after the response had been sent. A caller that reads the status off the start response and then waits for the terminal `Transcribe Job State Change` event, rather than polling, therefore never saw an in-progress state at all: the job went straight from queued to complete. Jobs now start `IN_PROGRESS` with `StartTime` set, matching the response the AWS CLI walkthrough documents, and `TRANSCRIBE_JOB_RUN_SECONDS` covers the whole run rather than half of it. `QUEUED`, which AWS reaches only for a request that opted into job queueing through `JobExecutionSettings.AllowDeferredExecution` while the account is at its concurrent job limit, stays reachable through the new `TRANSCRIBE_JOB_QUEUE_SECONDS` (default `0`); both knobs are settable at runtime through `/_ministack/config`. Contributed by @ppettitau.
## [1.5.10] — 2026-09-10
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index 8135a997..374304a5 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -3703,6 +3703,20 @@ def _apigw_method_create(logical_id, props, stack_name):
}
_apigw_v1._put_method(api_id, resource_id, http_method, data)
+ # apigateway_v1 stores these in dicts keyed by the status code as a string,
+ # and a template may legitimately carry StatusCode as an integer.
+ for method_response in props.get("MethodResponses", []) or []:
+ _apigw_v1._put_method_response(
+ api_id,
+ resource_id,
+ http_method,
+ str(method_response.get("StatusCode", "200")),
+ {
+ "responseParameters": method_response.get("ResponseParameters", {}),
+ "responseModels": method_response.get("ResponseModels", {}),
+ },
+ )
+
# Also set Integration if provided
integration = props.get("Integration")
if integration:
@@ -3720,6 +3734,20 @@ def _apigw_method_create(logical_id, props, stack_name):
}
_apigw_v1._put_integration(api_id, resource_id, http_method, int_data)
+ for integration_response in integration.get("IntegrationResponses", []) or []:
+ _apigw_v1._put_integration_response(
+ api_id,
+ resource_id,
+ http_method,
+ str(integration_response.get("StatusCode", "200")),
+ {
+ "selectionPattern": integration_response.get("SelectionPattern", ""),
+ "responseParameters": integration_response.get("ResponseParameters", {}),
+ "responseTemplates": integration_response.get("ResponseTemplates", {}),
+ "contentHandling": integration_response.get("ContentHandling"),
+ },
+ )
+
pid = f"{api_id}-{resource_id}-{http_method}"
return pid, {}
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index f411cb57..d1236ff8 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -10245,6 +10245,117 @@ def test_cfn_apigateway_rest_api_tracks_stack_region(cfn, apigw_v1):
_wait_stack(west_cfn, stack_name)
+def test_cfn_apigateway_method_responses_survive_provisioning(cfn, apigw_v1):
+ """MethodResponses and Integration.IntegrationResponses declared in a template
+ reach the deployed method, so a CDK-style defaultCorsPreflightOptions OPTIONS
+ method actually answers with the Access-Control-Allow-* headers it maps."""
+ endpoint = os.environ.get("MINISTACK_ENDPOINT", "http://localhost:4566")
+ port = urlparse(endpoint).port or 4566
+ suffix = _uuid_mod.uuid4().hex[:8]
+ stack_name = f"intg-cfn-apigw-cors-{suffix}"
+ template = {
+ "Resources": {
+ "Api": {
+ "Type": "AWS::ApiGateway::RestApi",
+ "Properties": {"Name": f"cors-cfn-{suffix}"},
+ },
+ "Items": {
+ "Type": "AWS::ApiGateway::Resource",
+ "Properties": {
+ "RestApiId": {"Ref": "Api"},
+ "ParentId": {"Fn::GetAtt": ["Api", "RootResourceId"]},
+ "PathPart": "items",
+ },
+ },
+ "OptionsMethod": {
+ "Type": "AWS::ApiGateway::Method",
+ "Properties": {
+ "RestApiId": {"Ref": "Api"},
+ "ResourceId": {"Ref": "Items"},
+ "HttpMethod": "OPTIONS",
+ "AuthorizationType": "NONE",
+ "Integration": {
+ "Type": "MOCK",
+ "RequestTemplates": {"application/json": '{ "statusCode": 200 }'},
+ "IntegrationResponses": [
+ {
+ "StatusCode": "204",
+ "ResponseParameters": {
+ "method.response.header.Access-Control-Allow-Origin": "'*'",
+ "method.response.header.Access-Control-Allow-Headers": (
+ "'Content-Type,Authorization'"
+ ),
+ "method.response.header.Access-Control-Allow-Methods": "'OPTIONS,GET'",
+ },
+ },
+ ],
+ },
+ "MethodResponses": [
+ {
+ "StatusCode": "204",
+ "ResponseParameters": {
+ "method.response.header.Access-Control-Allow-Origin": True,
+ "method.response.header.Access-Control-Allow-Headers": True,
+ "method.response.header.Access-Control-Allow-Methods": True,
+ },
+ },
+ ],
+ },
+ },
+ "Deployment": {
+ "Type": "AWS::ApiGateway::Deployment",
+ "DependsOn": "OptionsMethod",
+ "Properties": {"RestApiId": {"Ref": "Api"}, "StageName": "prod"},
+ },
+ },
+ "Outputs": {
+ "ApiId": {"Value": {"Ref": "Api"}},
+ "ResourceId": {"Value": {"Ref": "Items"}},
+ },
+ }
+
+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(template))
+ try:
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+ outputs = {item["OutputKey"]: item["OutputValue"] for item in stack["Outputs"]}
+
+ method = apigw_v1.get_method(
+ restApiId=outputs["ApiId"],
+ resourceId=outputs["ResourceId"],
+ httpMethod="OPTIONS",
+ )
+ assert method["methodResponses"], "MethodResponses were dropped during provisioning"
+ assert "204" in method["methodResponses"]
+ integration_responses = method["methodIntegration"]["integrationResponses"]
+ assert integration_responses, "IntegrationResponses were dropped during provisioning"
+ # Quotes are stripped at request time, not at storage time, so the
+ # stored mapping must keep them verbatim.
+ assert integration_responses["204"]["responseParameters"] == {
+ "method.response.header.Access-Control-Allow-Origin": "'*'",
+ "method.response.header.Access-Control-Allow-Headers": "'Content-Type,Authorization'",
+ "method.response.header.Access-Control-Allow-Methods": "'OPTIONS,GET'",
+ }
+
+ req = urllib.request.Request(
+ f"http://127.0.0.1:{port}/prod/items",
+ method="OPTIONS",
+ headers={
+ "Host": f"{outputs['ApiId']}.execute-api.localhost:{port}",
+ "Origin": "http://localhost:3000",
+ "Access-Control-Request-Method": "GET",
+ "Access-Control-Request-Headers": "authorization",
+ },
+ )
+ with urllib.request.urlopen(req) as resp:
+ assert resp.status == 204
+ assert resp.headers.get("Access-Control-Allow-Origin") == "*"
+ assert resp.headers.get("Access-Control-Allow-Methods") == "OPTIONS,GET"
+ finally:
+ cfn.delete_stack(StackName=stack_name)
+ _wait_stack(cfn, stack_name)
+
+
def test_cfn_apigateway_domain_name_lifecycle(cfn, apigw_v1):
"""CloudFormation provisions CDK-style regional and edge custom domains."""
suffix = _uuid_mod.uuid4().hex[:8]