Skip to content

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

feat(apigateway): dispatch v1 AWS integrations to SQS targets (#2047)

Lisandro Pintosfc426e06d4f2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 07156794..aa726ff5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).
### Added
+- **API Gateway v1 — `AWS` integrations targeting SQS** — non-proxy integrations whose URI names `sqs:path/{account}/{queue}` now reach the queue: the matching `requestTemplates` entry is rendered with the `$input.body`, `$input.json('<json-path>')`, `$input.params('<name>')` and `$util.urlEncode(...)` subset (a request with no matching template passes its body through as the form-encoded Query call), the rendered `Action` dispatch runs against the queue's own account and region, and the backend reply or error flows through `integrationResponses` — `selectionPattern` matched against the backend status code or error body, else the default response — including `responseTemplates` and `method.response.header.*` parameters. Other `AWS` service URIs still take the Lambda path. Closes #2045. Contributed by @pingedbrain.
+
- **IoT — domain configurations** — `CreateDomainConfiguration`, `DescribeDomainConfiguration`, `ListDomainConfigurations`, `UpdateDomainConfiguration` and `DeleteDomainConfiguration`, and the CloudFormation type `AWS::IoT::DomainConfiguration`, were missing. The list now shows the `iot:Data-ATS` and `iot:CredentialProvider` configurations IoT owns (their `domainName` is the `DescribeEndpoint` address). A configuration without a server certificate is AWS-managed with a generated `-ats` host, one with an ACM certificate is customer-managed, and requests are validated with AWS's messages (TLS security policy, authentication type and protocol combinations, certificates, the IoT-owned entries). As on AWS, only a DISABLED configuration can be deleted, and an AWS-managed one only seven days after it was disabled; CloudFormation leaves a configuration DISABLED unless the template enables it, and disables it before deleting it, so the stack delete of an AWS-managed one fails with `DELETE_FAILED`. The broker does not serve a configured domain. Contributed by @iot-rocket.
- **CloudFormation — enum properties are validated before provisioning** — a template value outside an enum of the resource schema was only refused by the resource's own create, after the resources before it existed. Such a value, literal or from a parameter, now fails the create before anything is provisioned, as AWS does: one stack `CREATE_FAILED` event "Validation failed with N error(s). ..." with `DetailedStatus` `VALIDATION_FAILED`, then `ROLLBACK_IN_PROGRESS` and `ROLLBACK_COMPLETE`, with no resource event. `DescribeStackEvents` returns `DetailedStatus`. `AWS::Signer::SigningProfile` `PlatformId` is the first property checked. Contributed by @iot-rocket.
- **Signer — CloudFormation `AWS::Signer::SigningProfile` and `AWS::Signer::ProfilePermission`, profile permissions and `CancelSigningProfile`** — a template with either type was refused as an unrecognized resource type, and the profile permission API and `CancelSigningProfile` had no route. Both types now provision as AWS does: the profile's `Ref` is its ARN with `Arn`, `ProfileName`, `ProfileVersion` and `ProfileVersionArn` attributes, a generated name is `<LogicalId>_` plus 12 letters and digits, `Tags` (with the stack and `aws:cloudformation:` tags) update in place, a `PlatformId` or `SignatureValidityPeriod` change replaces the profile, a delete cancels it, and a `PlatformId` outside the schema's two values fails the resource with AWS's enum message; the permission's `Ref` is `<StatementId>|<ProfileName>`. `AddProfilePermission`, `ListProfilePermissions` and `RemoveProfilePermission` follow the revision id rules, error codes, 2000-byte policy limit and `policySizeBytes` measured on AWS, a canceled profile keeps its name and its permissions, `PutSigningProfile` refuses a name that is already taken, and `GetSigningProfile` reports the 135-month default validity period. Contributed by @iot-rocket.
diff --git a/README.md b/README.md
index e0909d0f..eb8d1933 100644
--- a/README.md
+++ b/README.md
@@ -439,7 +439,7 @@ subnet = ec2.create_subnet(
| **WAF v2** | CreateWebACL, GetWebACL, UpdateWebACL, DeleteWebACL, ListWebACLs, AssociateWebACL, DisassociateWebACL, GetWebACLForResource, ListResourcesForWebACL, CreateIPSet, GetIPSet, UpdateIPSet, DeleteIPSet, ListIPSets, CreateRuleGroup, GetRuleGroup, UpdateRuleGroup, DeleteRuleGroup, ListRuleGroups, TagResource, UntagResource, ListTagsForResource, CheckCapacity, DescribeManagedRuleGroup | LockToken enforced on Update/Delete; resource associations tracked |
| **Step Functions** | CreateStateMachine, DeleteStateMachine, DescribeStateMachine, UpdateStateMachine, ListStateMachines, StartExecution, StartSyncExecution, StopExecution, DescribeExecution, DescribeStateMachineForExecution, ListExecutions, GetExecutionHistory, SendTaskSuccess, SendTaskFailure, SendTaskHeartbeat, CreateActivity, DeleteActivity, DescribeActivity, ListActivities, GetActivityTask, TestState, TagResource, UntagResource, ListTagsForResource | Full ASL interpreter; Retry/Catch; waitForTaskToken; Activities (worker pattern); Pass/Task/Choice/Wait/Succeed/Fail/Map/Parallel; TestState API with mock and inspectionLevel support; SFN_MOCK_CONFIG for AWS SFN Local compatible mock testing; intrinsic functions (States.StringToJson, States.JsonToString, States.JsonMerge, States.Format); nested startExecution.sync |
| **API Gateway v2** | CreateApi, GetApi, GetApis, UpdateApi, DeleteApi, CreateRoute, GetRoute, GetRoutes, UpdateRoute, DeleteRoute, CreateIntegration, GetIntegration, GetIntegrations, UpdateIntegration, DeleteIntegration, CreateRouteResponse, GetRouteResponse, GetRouteResponses, UpdateRouteResponse, DeleteRouteResponse, CreateIntegrationResponse, GetIntegrationResponse, GetIntegrationResponses, UpdateIntegrationResponse, DeleteIntegrationResponse, CreateStage, GetStage, GetStages, UpdateStage, DeleteStage, CreateDeployment, GetDeployment, GetDeployments, DeleteDeployment, CreateAuthorizer, GetAuthorizer, GetAuthorizers, UpdateAuthorizer, DeleteAuthorizer, CreateDomainName, GetDomainName, GetDomainNames, UpdateDomainName, DeleteDomainName, CreateApiMapping, GetApiMapping, GetApiMappings, UpdateApiMapping, DeleteApiMapping, TagResource, UntagResource, GetTags, PostToConnection, GetConnection, DeleteConnection | **HTTP API** and **WebSocket API** (`protocolType=WEBSOCKET`); Lambda proxy (`AWS_PROXY`), HTTP proxy (`HTTP_PROXY`), and MOCK integrations; HTTP data plane via `{apiId}.execute-api.localhost` Host header or path-based `/_aws/execute-api/{apiId}/{stage}/{path}` (no DNS/Host override needed — works from browsers on macOS and strict clients); `$default` stage served from the URL root (no stage segment in the path); per-API `corsConfiguration` applied to preflights + dispatched responses; request parameter mapping for HTTP_PROXY (`append/overwrite/remove` for headers/querystring plus `overwrite:path`) with context variables including `$context.authorizer.jwt.claims`; JWT data-plane authorization for HTTP routes (issuer/audience/time/scope checks) and claim propagation to integrations; qualified-alias integration URIs (`arn:...:function:<name>:<alias>`) resolve to the alias's target version; WebSocket data plane on the same two URL forms, with `$connect` / `$disconnect` / `$default` / custom-action routing, `$request.body.*` RouteSelectionExpression, `@connections` management API (PostToConnection / GetConnection / DeleteConnection), per-connection outbox for server-side push; `{param}` / `{proxy+}` matching; custom domain names and API mappings shared with API Gateway v1, routed by `Host`; JWT/Lambda authorizer CRUD; pin `apiId` across runs with the `ms-custom-id` tag |
-| **API Gateway v1** | CreateRestApi, GetRestApi, GetRestApis, UpdateRestApi, DeleteRestApi, CreateResource, GetResource, GetResources, UpdateResource, DeleteResource, PutMethod, GetMethod, DeleteMethod, UpdateMethod, PutMethodResponse, GetMethodResponse, DeleteMethodResponse, PutIntegration, GetIntegration, DeleteIntegration, UpdateIntegration, PutIntegrationResponse, GetIntegrationResponse, DeleteIntegrationResponse, CreateDeployment, GetDeployment, GetDeployments, UpdateDeployment, DeleteDeployment, CreateStage, GetStage, GetStages, UpdateStage, DeleteStage, CreateAuthorizer, GetAuthorizer, GetAuthorizers, UpdateAuthorizer, DeleteAuthorizer, CreateModel, GetModel, GetModels, DeleteModel, CreateDocumentationPart, GetDocumentationPart, GetDocumentationParts, UpdateDocumentationPart, DeleteDocumentationPart, CreateApiKey, GetApiKey, GetApiKeys, UpdateApiKey, DeleteApiKey, CreateUsagePlan, GetUsagePlan, GetUsagePlans, UpdateUsagePlan, DeleteUsagePlan, CreateUsagePlanKey, GetUsagePlanKeys, DeleteUsagePlanKey, CreateDomainName, GetDomainName, GetDomainNames, DeleteDomainName, CreateBasePathMapping, GetBasePathMapping, GetBasePathMappings, DeleteBasePathMapping, TagResource, UntagResource, GetTags, CreateRequestValidator, GetRequestValidators, GetRequestValidator, UpdateRequestValidator, DeleteRequestValidator | REST API (v1) protocol; Lambda proxy format 1.0 (AWS_PROXY), non-proxy Lambda (AWS — the handler's output is the response body, mapping templates not modeled), HTTP proxy (HTTP_PROXY), MOCK integration; data plane via `{apiId}.execute-api.localhost` Host header, path-based `/_aws/execute-api/{apiId}/{stage}/{path}`, or legacy `/restapis/{apiId}/{stage}/_user_request_/{path}`; qualified-alias integration URIs (`arn:...:function:<name>:<alias>`) resolve to the alias's target version; resource tree with `{param}` and `{proxy+}` path matching; routing is on the resource+method pair, so a request the matched resource does not serve falls through to a `{proxy+}` elsewhere in the tree (a methodless intermediate, a CORS-preflight-only node, or any undeclared verb), as it does on AWS; TOKEN and REQUEST Lambda authorizers on the data plane, with the returned IAM policy evaluated against each request's own method ARN and `authorizerResultTtlInSeconds` caching the policy per stage and identity source; JSON Patch for all PATCH operations; state persistence; pin `id` across runs with the `ms-custom-id` tag |
+| **API Gateway v1** | CreateRestApi, GetRestApi, GetRestApis, UpdateRestApi, DeleteRestApi, CreateResource, GetResource, GetResources, UpdateResource, DeleteResource, PutMethod, GetMethod, DeleteMethod, UpdateMethod, PutMethodResponse, GetMethodResponse, DeleteMethodResponse, PutIntegration, GetIntegration, DeleteIntegration, UpdateIntegration, PutIntegrationResponse, GetIntegrationResponse, DeleteIntegrationResponse, CreateDeployment, GetDeployment, GetDeployments, UpdateDeployment, DeleteDeployment, CreateStage, GetStage, GetStages, UpdateStage, DeleteStage, CreateAuthorizer, GetAuthorizer, GetAuthorizers, UpdateAuthorizer, DeleteAuthorizer, CreateModel, GetModel, GetModels, DeleteModel, CreateDocumentationPart, GetDocumentationPart, GetDocumentationParts, UpdateDocumentationPart, DeleteDocumentationPart, CreateApiKey, GetApiKey, GetApiKeys, UpdateApiKey, DeleteApiKey, CreateUsagePlan, GetUsagePlan, GetUsagePlans, UpdateUsagePlan, DeleteUsagePlan, CreateUsagePlanKey, GetUsagePlanKeys, DeleteUsagePlanKey, CreateDomainName, GetDomainName, GetDomainNames, DeleteDomainName, CreateBasePathMapping, GetBasePathMapping, GetBasePathMappings, DeleteBasePathMapping, TagResource, UntagResource, GetTags, CreateRequestValidator, GetRequestValidators, GetRequestValidator, UpdateRequestValidator, DeleteRequestValidator | REST API (v1) protocol; Lambda proxy format 1.0 (AWS_PROXY), non-proxy Lambda (AWS — the handler's output is the response body, Lambda request mapping templates not modeled) and non-proxy `AWS` integrations whose URI targets SQS (`arn:aws:apigateway:{region}:sqs:path/{account}/{queue}`: the request template subset `$input.body` / `$input.json('…')` / `$input.params('…')` / `$util.urlEncode(…)` renders the form-encoded SQS Query call, run in the URI's account and region, and the backend result or error is mapped through `integrationResponses`), HTTP proxy (HTTP_PROXY), MOCK integration; data plane via `{apiId}.execute-api.localhost` Host header, path-based `/_aws/execute-api/{apiId}/{stage}/{path}`, or legacy `/restapis/{apiId}/{stage}/_user_request_/{path}`; qualified-alias integration URIs (`arn:...:function:<name>:<alias>`) resolve to the alias's target version; resource tree with `{param}` and `{proxy+}` path matching; routing is on the resource+method pair, so a request the matched resource does not serve falls through to a `{proxy+}` elsewhere in the tree (a methodless intermediate, a CORS-preflight-only node, or any undeclared verb), as it does on AWS; TOKEN and REQUEST Lambda authorizers on the data plane, with the returned IAM policy evaluated against each request's own method ARN and `authorizerResultTtlInSeconds` caching the policy per stage and identity source; JSON Patch for all PATCH operations; state persistence; pin `id` across runs with the `ms-custom-id` tag |
| **ELBv2 / ALB** | CreateLoadBalancer, DescribeLoadBalancers, DeleteLoadBalancer, DescribeLoadBalancerAttributes, ModifyLoadBalancerAttributes, CreateTargetGroup, DescribeTargetGroups, ModifyTargetGroup, DeleteTargetGroup, DescribeTargetGroupAttributes, ModifyTargetGroupAttributes, CreateListener, DescribeListeners, ModifyListener, DeleteListener, CreateRule, DescribeRules, ModifyRule, DeleteRule, SetRulePriorities, SetSubnets, SetIpAddressType, SetSecurityGroups, RegisterTargets, DeregisterTargets, DescribeTargetHealth, AddTags, RemoveTags, DescribeTags | Control plane + data plane; ALB→Lambda live traffic routing; `path-pattern`, `host-header`, `http-method`, `query-string`, `http-header` rule conditions; `forward`, `redirect`, `fixed-response`, `authenticate-oidc` actions; data plane via `{lb-name}.alb.localhost` Host header or `/_alb/{lb-name}/` path prefix |
| **KMS** | CreateKey, ReplicateKey, ListKeys, DescribeKey, GetPublicKey, Sign, Verify, Encrypt, Decrypt, GenerateMac, VerifyMac, GenerateDataKey, GenerateDataKeyWithoutPlaintext, GenerateDataKeyPair, GenerateDataKeyPairWithoutPlaintext, GenerateRandom, CreateAlias, DeleteAlias, ListAliases, UpdateAlias, UpdateKeyDescription, EnableKeyRotation, DisableKeyRotation, GetKeyRotationStatus, GetKeyPolicy, PutKeyPolicy, ListKeyPolicies, EnableKey, DisableKey, ScheduleKeyDeletion, CancelKeyDeletion, TagResource, UntagResource, ListResourceTags, GetParametersForImport, ImportKeyMaterial, DeleteImportedKeyMaterial, CreateGrant, ListGrants, RevokeGrant, RetireGrant | 41 actions; multi-Region keys: `CreateKey` with `MultiRegion` mints an `mrk-` key and `ReplicateKey` creates a same-id replica in another region sharing the key material (ciphertext from the primary decrypts against the replica; a primary with live replicas waits in `PendingReplicaDeletion`); CMK specs: RSA (2048/3072/4096), ECC (SECG_P256K1, NIST P-256/384/521), Edwards25519 (`ECC_NIST_EDWARDS25519` — pure Ed25519 via `ED25519_SHA_512` with `MessageType=RAW`; `ED25519_PH_SHA_512` advertised in `SigningAlgorithms` but returns `UnsupportedOperationException` until Ed25519ph lands), HMAC (`HMAC_224`/`HMAC_256`/`HMAC_384`/`HMAC_512` with `KeyUsage=GENERATE_VERIFY_MAC`), and symmetric keys; PKCS1v15, PSS, and ECDSA signing; real RFC 2104 HMAC via `GenerateMac`/`VerifyMac` (4096-byte message cap, `KMSInvalidMacException` on mismatch, `DryRun` supported); envelope encryption, incl. asymmetric data key pairs (`KeyPairSpec` adds RSA_3072); alias resolution; key rotation; key policies; tags; enable/disable/schedule deletion; grants (stored and listed, not evaluated for authorization); full Terraform `aws_kms_key` compatible; `cryptography` package included in Docker image |
| **CloudFront** | CreateDistribution, GetDistribution, GetDistributionConfig, ListDistributions, UpdateDistribution, DeleteDistribution, CreateInvalidation, ListInvalidations, GetInvalidation, CreateOriginAccessControl, GetOriginAccessControl, GetOriginAccessControlConfig, ListOriginAccessControls, UpdateOriginAccessControl, DeleteOriginAccessControl, CreateMonitoringSubscription, GetMonitoringSubscription, DeleteMonitoringSubscription, CreateFunction, DeleteFunction, DescribeFunction, GetFunction, ListFunctions, PublishFunction, UpdateFunction, **KeyValueStore (mgmt)**: CreateKeyValueStore, DescribeKeyValueStore, ListKeyValueStores, UpdateKeyValueStore, DeleteKeyValueStore, **Policies**: CreateCachePolicy, GetCachePolicy, GetCachePolicyConfig, UpdateCachePolicy, DeleteCachePolicy, ListDistributionsByCachePolicyId, CreateOriginRequestPolicy, GetOriginRequestPolicy, GetOriginRequestPolicyConfig, UpdateOriginRequestPolicy, DeleteOriginRequestPolicy, ListDistributionsByOriginRequestPolicyId, CreateResponseHeadersPolicy, GetResponseHeadersPolicy, GetResponseHeadersPolicyConfig, UpdateResponseHeadersPolicy, DeleteResponseHeadersPolicy, ListDistributionsByResponseHeadersPolicyId, **Public keys / key groups**: CreatePublicKey, GetPublicKey, GetPublicKeyConfig, UpdatePublicKey, DeletePublicKey, ListPublicKeys, CreateKeyGroup, GetKeyGroup, GetKeyGroupConfig, UpdateKeyGroup, DeleteKeyGroup, ListKeyGroups, **SaaS Manager**: CreateConnectionGroup, GetConnectionGroup, GetConnectionGroupByRoutingEndpoint, UpdateConnectionGroup, DeleteConnectionGroup, ListConnectionGroups, CreateDistributionTenant, GetDistributionTenant, GetDistributionTenantByDomain, UpdateDistributionTenant, DeleteDistributionTenant, ListDistributionTenants, ListDistributionTenantsByCustomization, AssociateDistributionTenantWebACL, DisassociateDistributionTenantWebACL, CreateInvalidationForDistributionTenant, GetInvalidationForDistributionTenant, ListInvalidationsForDistributionTenant, VerifyDnsConfiguration, GetManagedCertificateDetails, ListDomainConflicts, UpdateDomainAssociation, ListDistributionsByConnectionMode, TagResource, UntagResource, ListTagsForResource | REST/XML API; ETag-based optimistic concurrency; Origin config round-trip; cache / origin request / response headers policies round-trip their full config with `ETag` / `If-Match` optimistic concurrency; Origin Access Control (OAC) with SigV4 signing for S3, MediaStore, Lambda, MediaPackageV2 origins; the AWS-managed cache (7), origin request (8) and response headers (5) policies are listed and immutable; `CreateMonitoringSubscription` upserts; functions keep DEVELOPMENT/LIVE stages and `KeyValueStoreAssociations`, and `UpdateFunction` clears LIVE until `PublishFunction` runs again; **data plane**: a request to a distribution's `DomainName` (`d` + 13 characters, or `<label>.cloudfront.<MINISTACK_HOST>`) is served through its cache behaviors, policies, `viewer-request` / `viewer-response` CloudFront Functions (on Node) and an S3 or custom origin (read only as the bucket policy allows: OAC, OAI or public), with custom error responses for origin errors; not modelled: caching, WAF, logging, geo restrictions, signed URLs, Lambda@Edge, CORS preflight, and origins at AWS hostnames (502); **SaaS Manager** multi-tenant distributions: `DistributionConfig` round-trips `ConnectionMode` / `TenantConfig`, tenants require a `tenant-only` distribution, CNAMEs are unique across tenants and distribution aliases, a tenant without an explicit connection group lazily gets the account default group, and MiniStack collapses async workflows (tenants deploy, domains activate, DNS verifies, managed certificates issue immediately); key groups validate `Items` against real public keys, and deleting either while still referenced (`PublicKeyInUse` / `ResourceInUse`) is rejected |
diff --git a/ministack/services/apigateway_v1.py b/ministack/services/apigateway_v1.py
index a3616c53..49450e33 100644
--- a/ministack/services/apigateway_v1.py
+++ b/ministack/services/apigateway_v1.py
@@ -114,6 +114,7 @@ from ministack.core.responses import (
get_account_id,
get_region,
new_uuid,
+ request_scope,
)
from ministack.services.apigateway import (
_b64url_decode,
@@ -2571,13 +2572,21 @@ async def _execute_in_scope(
if int_type in ("AWS_PROXY", "AWS"):
# AWS_PROXY hands the function a response envelope to interpret; the
- # non-proxy `AWS` (custom / "lambda") integration returns the handler's
- # output as the body verbatim. Same event in, different response
- # contract out.
+ # non-proxy `AWS` integration dispatches on the target service in the
+ # URI: `sqs:path/...` targets reach the SQS data plane, everything else
+ # takes the Lambda path (custom/"lambda" integrations return the
+ # handler's output as the body verbatim).
caller_identity = None
if (method_obj.get("authorizationType") or "").upper() == "AWS_IAM":
caller_identity = _iam_caller_identity(headers, query_params)
- invoke = _invoke_lambda_proxy_v1 if int_type == "AWS_PROXY" else _invoke_lambda_custom_v1
+ if int_type == "AWS_PROXY":
+ invoke = _invoke_lambda_proxy_v1
+ else:
+ spec = _parse_service_integration_uri(integration.get("uri", ""))
+ if spec and spec[1] == "sqs" and spec[2] == "path":
+ invoke = _invoke_sqs_v1
+ else:
+ invoke = _invoke_lambda_custom_v1
return await invoke(
integration, api_id, stage_name, stage, resource, path, method,
headers, body, query_params, path_params,
@@ -2890,6 +2899,294 @@ async def _invoke_lambda_custom_v1(
return _default_integration_response_status_v1(integration), resp_headers, resp_body
+def _parse_service_integration_uri(uri: str):
+ """Split an ``arn:aws:apigateway:{region}:{service}:{verb}/{rest}`` URI.
+
+ Returns ``(region, service, verb, rest)`` — e.g. for
+ ``arn:aws:apigateway:us-east-1:sqs:path/123456789012/my-queue`` it returns
+ ``("us-east-1", "sqs", "path", "123456789012/my-queue")``. Returns ``None``
+ for URIs that are not service-integration ARNs (plain Lambda references,
+ HTTP endpoints); those keep their existing dispatch.
+ """
+ if not isinstance(uri, str):
+ return None
+ parts = uri.split(":", 5)
+ if len(parts) != 6 or parts[0] != "arn" or parts[2] != "apigateway":
+ return None
+ region, service, remainder = parts[3], parts[4], parts[5]
+ if "/" not in remainder:
+ return None
+ verb, rest = remainder.split("/", 1)
+ return region, service, verb, rest
+
+
+# ── Minimal request-template (VTL) subset ────────────────────────────────
+#
+# Non-proxy AWS service integrations shape the backend call through a
+# `requestTemplates` mapping template. Only the subset real SQS templates use
+# is modeled: `$input.body`, `$input.json('<json-path>')` / `$input.path(...)`,
+# `$input.params('name')` (merged header < querystring < path params, the
+# precedence AWS documents), and `$util.urlEncode(...)` — e.g.
+# `Action=SendMessage&MessageBody=$util.urlEncode($input.body)`.
+# Anything the subset does not cover renders as an empty string.
+
+def _vtl_json_path(body_str: str, path: str):
+ """Evaluate the small ``$input.json('<path>')`` subset: ``$``, ``$.a.b``,
+ numeric indices. Returns "" for unparseable bodies or missing nodes."""
+ try:
+ node = json.loads(body_str)
+ except (json.JSONDecodeError, TypeError, ValueError):
+ return ""
+ path = path.strip()
+ if path in ("", "$", "'$'", '"$"'):
+ return node
+ for seg in path.lstrip("$").lstrip(".").split("."):
+ if not seg:
+ continue
+ if isinstance(node, dict):
+ node = node.get(seg)
+ elif isinstance(node, list) and seg.isdigit() and int(seg) < len(node):
+ node = node[int(seg)]
+ else:
+ return ""
+ if node is None:
+ return ""
+ return node
+
+
+def _vtl_eval(expr: str, body_str: str, params_map: dict):
+ """Evaluate one template reference; unrecognised references render ""."""
+ expr = expr.strip()
+ if expr.startswith("$util.urlEncode(") and expr.endswith(")"):
+ inner = _vtl_eval(expr[len("$util.urlEncode("):-1], body_str, params_map)
+ return urllib.parse.quote(str(inner), safe="")
+ if expr == "$input.body":
+ return body_str
+ m = re.fullmatch(r"\$input\.(?:json|path)\(\s*(['\"])(.*?)\1\s*\)",
+ expr, re.DOTALL)
+ if m:
+ value = _vtl_json_path(body_str, m.group(2))
+ if isinstance(value, (dict, list)):
+ return json.dumps(value, separators=(",", ":"), ensure_ascii=False)
+ return "" if value is None else str(value)
+ m = re.fullmatch(r"\$input\.params\(\s*(['\"])(.*?)\1\s*\)",
+ expr, re.DOTALL)
+ if m:
+ return str(params_map.get(m.group(2), ""))
+ if len(expr) >= 2 and expr[0] == expr[-1] and expr[0] in ("'", '"'):
+ return expr[1:-1]
+ return ""
+
+
+def _vtl_expr_end(template: str, start: int) -> int:
+ """Index just past the ``$...`` expression at ``start`` in ``template``.
+
+ Bare references end at the first non-identifier/dot character; parenthesised
+ calls end at the closing paren, skipping over quoted strings.
+ """
+ i = start + 1
+ depth = 0
+ while i < len(template):
+ ch = template[i]
+ if ch in ("'", '"'):
+ quote = ch
+ i += 1
+ while i < len(template) and template[i] != quote:
+ i += 1
+ elif ch == "(":
+ depth += 1
+ elif ch == ")":
+ if depth == 0:
+ return i
+ depth -= 1
+ if depth == 0:
+ return i + 1
+ elif depth == 0 and not (ch.isalnum() or ch in "._"):
+ return i
+ i += 1
+ return i
+
+
+def _render_request_template_v1(template: str, body_str: str,
+ params_map: dict) -> str:
+ """Render the VTL subset; bare text passes through unchanged."""
+ out = []
+ i = 0
+ while i < len(template):
+ if template[i] != "$" or not template[i + 1:].startswith(("input", "util")):
+ out.append(template[i])
+ i += 1
+ continue
+ end = _vtl_expr_end(template, i)
+ out.append(str(_vtl_eval(template[i:end], body_str, params_map)))
+ i = end
+ return "".join(out)
+
+
+def _select_service_integration_response(integration, backend_status: int,
+ backend_text: str):
+ """Pick the integration response for a non-proxy service call.
+
+ ``selectionPattern`` entries match on the backend HTTP status
+ (``fullmatch`` on the numeric code) or on the backend body (``search``);
+ the entry with no pattern is the default and takes whatever remains.
+ """
+ responses = integration.get("integrationResponses") or {}
+ for resp in responses.values():
+ pattern = (resp or {}).get("selectionPattern")
+ if not pattern:
+ continue
+ try:
+ if (re.fullmatch(pattern, str(backend_status))
+ or re.search(pattern, backend_text)):
+ return resp
+ except re.error:
+ continue
+ for resp in responses.values():
+ if not (resp or {}).get("selectionPattern"):
+ return resp
+ return None
+
+
+def _service_integration_response(integration, backend_status: int,
+ backend_ct: str, backend_text: str,
+ accept: str):
+ """Map a backend service result through the integration responses."""
+ responses = integration.get("integrationResponses") or {}
+ if not responses:
+ # No integration responses configured: pass the backend reply through.
+ return backend_status, {"Content-Type": backend_ct}, backend_text.encode()
+ selected = _select_service_integration_response(
+ integration, backend_status, backend_text)
+ if selected is None:
+ return (500, {"Content-Type": "application/json"},
+ json.dumps({"message": "Internal server error"}).encode())
+ try:
+ status = int(selected.get("statusCode") or backend_status)
+ except (TypeError, ValueError):
+ status = backend_status
+ resp_headers = {"Content-Type": "application/json"}
+ for dest, src in (selected.get("responseParameters") or {}).items():
+ if not dest.startswith("method.response.header."):
+ continue
+ header = dest[len("method.response.header."):]
+ if isinstance(src, str) and len(src) >= 2 and src[0] == src[-1] == "'":
+ resp_headers[header] = src[1:-1]
+ else:
+ resp_headers[header] = src
+ templates = selected.get("responseTemplates") or {}
+ template = templates.get(accept) or templates.get("application/json")
+ if template is None and templates:
+ template = next(iter(templates.values()))
+ if template is not None:
+ body_out = _render_request_template_v1(
+ template, backend_text,
+ {k: v for k, v in resp_headers.items()})
+ return status, resp_headers, body_out.encode()
+ return status, resp_headers, backend_text.encode()
+
+
+async def _invoke_sqs_v1(
+ integration,
+ api_id,
+ stage_name,
+ stage,
+ resource,
+ request_path,
+ method,
+ headers,
+ body,
+ query_params,
+ path_params,
+ *,
+ owner_account_id=None,
+ owner_region=None,
+ binary_media_types=None,
+ authorizer_context=None,
+ caller_identity=None,
+):
+ """Dispatch a non-proxy ``AWS`` integration whose URI targets SQS.
+
+ ``arn:aws:apigateway:{region}:sqs:path/{account}/{queue}`` renders the
+ request template for the request's Content-Type (the raw body passes
+ through when none matches, per WHEN_NO_MATCH/WHEN_NO_TEMPLATES), parses the
+ result as a form-encoded SQS Query call, and runs it in the scope the URI
+ names — the queue's own account and region, like the integration's
+ ``credentials`` role would on AWS. Backend errors flow through the
+ integration responses like successes do; nothing is a 504 here because the
+ queue lookup itself is part of the modeled call.
+ """
+ from ministack.services import sqs as _sqs_svc
+
+ spec = _parse_service_integration_uri(integration.get("uri", ""))
+ region = (spec[0] if spec else "") or owner_region or get_region()
+ account, queue_name = None, None
+ if spec and spec[1] == "sqs" and spec[2] == "path":
+ tail = spec[3].strip("/").split("/")
+ if len(tail) == 2:
+ account, queue_name = tail
+ elif len(tail) == 1:
+ account, queue_name = (owner_account_id or get_account_id(),
+ tail[0])
+ if not queue_name:
+ # A malformed sqs:path URI means the integration has no reachable
+ # backend — same failure class as an unresolvable Lambda reference.
+ return (504, {"Content-Type": "application/json"},
+ json.dumps({"message": "Internal server error"}).encode())
+
+ body_str = body.decode("utf-8", errors="replace") if body else ""
+ params_map = {}
+ for k, v in (headers or {}).items():
+ if isinstance(v, str):
+ params_map[k] = v
+ for k, v in (query_params or {}).items():
+ params_map[k] = v[0] if isinstance(v, (list, tuple)) else v
+ params_map.update(path_params or {})
+
+ content_type = (_header_ci(headers, "content-type") or "application/json")
+ content_type = content_type.split(";", 1)[0].strip().lower()
+ template = None
+ for key, value in (integration.get("requestTemplates") or {}).items():
+ if key.split(";", 1)[0].strip().lower() == content_type:
+ template = value
+ break
+ rendered = (_render_request_template_v1(template, body_str, params_map)
+ if template is not None else body_str)
+ form = dict(urllib.parse.parse_qsl(rendered, keep_blank_values=True))
+ action = form.get("Action", "")
+
+ accept = (_header_ci(headers, "accept") or "application/json")
+ accept = accept.split(",", 1)[0].split(";", 1)[0].strip().lower()
+
+ if not action:
+ backend_status, _h, xbody = _sqs_svc._xml_err_resp(
+ "MissingAction", "Missing Action parameter", 400)
+ backend_text = xbody.decode("utf-8", errors="replace")
+ return _service_integration_response(
+ integration, backend_status, "application/xml", backend_text,
+ accept)
+
+ qurl = (_sqs_svc._queue_name_to_url.get_scoped(account, region, queue_name)
+ or _sqs_svc._queue_url_for_account(account, queue_name))
+ data = _sqs_svc._normalise(action, form)
+ data["QueueUrl"] = qurl
+ try:
+ with request_scope(account, region):
+ result = await _sqs_svc._dispatch(action, data, qurl)
+ backend_status, _h, xbody = _sqs_svc._to_xml(action, result)
+ backend_ct = "application/xml"
+ except _sqs_svc._Err as e:
+ backend_status, _h, xbody = _sqs_svc._xml_err_resp(
+ e.code, e.message, e.status)
+ backend_ct = "application/xml"
+ except Exception:
+ return (504, {"Content-Type": "application/json"},
+ json.dumps({"message": "Internal server error"}).encode())
+ return _service_integration_response(
+ integration, backend_status, backend_ct,
+ xbody.decode("utf-8", errors="replace"), accept)
+
+
async def _invoke_http_proxy_v1(integration, path, method, headers, body, query_params, path_params=None):
"""Forward a request to an HTTP backend."""
uri = integration.get("uri", "")
diff --git a/tests/test_apigatewayv1.py b/tests/test_apigatewayv1.py
index 0b9a1c8a..64980603 100644
--- a/tests/test_apigatewayv1.py
+++ b/tests/test_apigatewayv1.py
@@ -5331,3 +5331,149 @@ def test_apigwv1_request_validator_crud(apigw_v1):
assert exc.value.response["Error"]["Code"] == "NotFoundException"
finally:
apigw_v1.delete_rest_api(restApiId=api_id)
+
+
+def _sqs_api(apigw_v1, queue_name, *, path_part="send", account="000000000000",
+ region="us-east-1", request_templates=None,
+ integration_responses=None, passthrough=None):
+ """REST API with POST /<path_part> backed by a non-proxy AWS integration
+ pointing at ``sqs:path/{account}/{queue_name}`` (#2045)."""
+ api_id, root = _gw_api(apigw_v1, f"gwsqs-{_uuid_mod.uuid4().hex[:8]}")
+ resource = apigw_v1.create_resource(
+ restApiId=api_id, parentId=root, pathPart=path_part)["id"]
+ apigw_v1.put_method(restApiId=api_id, resourceId=resource,
+ httpMethod="POST", authorizationType="NONE")
+ apigw_v1.put_method_response(restApiId=api_id, resourceId=resource,
+ httpMethod="POST", statusCode="200")
+ kwargs = {}
+ if passthrough:
+ kwargs["passthroughBehavior"] = passthrough
+ apigw_v1.put_integration(
+ restApiId=api_id, resourceId=resource, httpMethod="POST", type="AWS",
+ integrationHttpMethod="POST",
+ uri=f"arn:aws:apigateway:{region}:sqs:path/{account}/{queue_name}",
+ credentials=f"arn:aws:iam::{account}:role/apigw-sqs",
+ requestParameters={
+ "integration.request.header.Content-Type":
+ "'application/x-www-form-urlencoded'"},
+ requestTemplates=request_templates or {},
+ **kwargs)
+ for status_code, extra in (integration_responses or {}).items():
+ apigw_v1.put_integration_response(
+ restApiId=api_id, resourceId=resource, httpMethod="POST",
+ statusCode=status_code, **extra)
+ apigw_v1.create_deployment(restApiId=api_id, stageName="p")
+ return api_id
+
+
+def test_apigwv1_execute_sqs_integration_delivers_message(apigw_v1, sqs):
+ """#2045 repro: AWS -> sqs:path renders the request template, sends the
+ message, and flows the reply through the integration response."""
+ queue_name = f"apigw-q-{_uuid_mod.uuid4().hex[:8]}"
+ qurl = sqs.create_queue(QueueName=queue_name)["QueueUrl"]
+ api_id = _sqs_api(
+ apigw_v1, queue_name,
+ request_templates={
+ "application/json":
+ "Action=SendMessage&MessageBody=$util.urlEncode($input.body)"},
+ integration_responses={"200": {
+ "responseTemplates": {"application/json": '{"message":"accepted"}'}}})
+ try:
+ status, text = _stage_call(
+ api_id, "/send", method="POST", body='{"hello":"world"}',
+ headers=[("Content-Type", "application/json")])
+ assert status == 200
+ assert json.loads(text) == {"message": "accepted"}
+ msgs = sqs.receive_message(QueueUrl=qurl, WaitTimeSeconds=1).get(
+ "Messages", [])
+ assert [m["Body"] for m in msgs] == ['{"hello":"world"}']
+ finally:
+ apigw_v1.delete_rest_api(restApiId=api_id)
+ sqs.delete_queue(QueueUrl=qurl)
+
+
+def test_apigwv1_execute_sqs_integration_vtl_subset(apigw_v1, sqs):
+ """$input.json / $input.params render inside the request template."""
+ queue_name = f"apigw-q-{_uuid_mod.uuid4().hex[:8]}"
+ qurl = sqs.create_queue(QueueName=queue_name)["QueueUrl"]
+ api_id = _sqs_api(
+ apigw_v1, queue_name,
+ request_templates={
+ "application/json":
+ "Action=SendMessage"
+ "&MessageBody=$util.urlEncode($input.json('$.msg'))"
+ "-$input.params('tenant')"},
+ integration_responses={"200": {}})
+ try:
+ status, _ = _stage_call(
+ api_id, "/send?tenant=blue", method="POST",
+ body='{"msg":"hi there"}',
+ headers=[("Content-Type", "application/json")])
+ assert status == 200
+ msgs = sqs.receive_message(QueueUrl=qurl, WaitTimeSeconds=1).get(
+ "Messages", [])
+ assert [m["Body"] for m in msgs] == ["hi there-blue"]
+ finally:
+ apigw_v1.delete_rest_api(restApiId=api_id)
+ sqs.delete_queue(QueueUrl=qurl)
+
+
+def test_apigwv1_execute_sqs_integration_passthrough(apigw_v1, sqs):
+ """No matching request template: the body passes through as the Query
+ call itself (WHEN_NO_MATCH default)."""
+ queue_name = f"apigw-q-{_uuid_mod.uuid4().hex[:8]}"
+ qurl = sqs.create_queue(QueueName=queue_name)["QueueUrl"]
+ api_id = _sqs_api(
+ apigw_v1, queue_name,
+ request_templates={"application/json": "Action=SendMessage&MessageBody=x"},
+ integration_responses={"200": {}})
+ try:
+ status, _ = _stage_call(
+ api_id, "/send", method="POST",
+ body="Action=SendMessage&MessageBody=raw-form-body",
+ headers=[("Content-Type", "application/x-www-form-urlencoded")])
+ assert status == 200
+ msgs = sqs.receive_message(QueueUrl=qurl, WaitTimeSeconds=1).get(
+ "Messages", [])
+ assert [m["Body"] for m in msgs] == ["raw-form-body"]
+ finally:
+ apigw_v1.delete_rest_api(restApiId=api_id)
+ sqs.delete_queue(QueueUrl=qurl)
+
+
+def test_apigwv1_execute_sqs_integration_backend_errors_map(apigw_v1, sqs):
+ """An SQS-side failure selects on selectionPattern against the backend
+ error body; unmatched errors land on the default (patternless) response."""
+ missing = f"missing-q-{_uuid_mod.uuid4().hex[:8]}"
+ api_id = _sqs_api(
+ apigw_v1, missing,
+ request_templates={
+ "application/json":
+ "Action=SendMessage&MessageBody=$util.urlEncode($input.body)"},
+ integration_responses={
+ "200": {"responseTemplates": {"application/json": '{"ok":true}'}},
+ "502": {"selectionPattern": ".*NonExistentQueue.*",
+ "responseTemplates": {"application/json": '{"ok":false}'}}})
+ try:
+ status, text = _stage_call(
+ api_id, "/send", method="POST", body='{"x":1}',
+ headers=[("Content-Type", "application/json")])
+ assert status == 502
+ assert json.loads(text) == {"ok": False}
+ finally:
+ apigw_v1.delete_rest_api(restApiId=api_id)
+
+ # No pattern matches a MissingAction: the default response absorbs it.
+ api_id = _sqs_api(
+ apigw_v1, missing,
+ request_templates={"application/json": "Action=&MessageBody=x"},
+ integration_responses={
+ "200": {"responseTemplates": {"application/json": '{"ok":true}'}}})
+ try:
+ status, text = _stage_call(
+ api_id, "/send", method="POST", body='{"x":1}',
+ headers=[("Content-Type", "application/json")])
+ assert status == 200
+ assert json.loads(text) == {"ok": True}
+ finally:
+ apigw_v1.delete_rest_api(restApiId=api_id)