localstack icon indicating copy to clipboard operation
localstack copied to clipboard

bug: Stage variables not evaluated for HTTP integration URI

Open agershman opened this issue 3 years ago • 4 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current Behavior

AWS API Gateway allowed for use of stage variables in an HTTP integration URI (ref: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-stages.html). E.g. http://${stageVariables.foo}. I've observed that localstack will not evaluate stage variable interpolation in this context, which in turn causes the upstream HTTP request to fail due to what is a nonsensical host.

requests.exceptions.ConnectionError: MyHTTPConnectionPool(host='$%7bstagevariables.foo%7d', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x4046249120>: Failed to establish a new connection: [Errno -2] Name or service not known'))

Expected Behavior

I'd like to see localstack's emulation of AWS API Gateway follow the documented contract for stage variables in an HTTP integration URI. See the API Gateway stage variables reference / HTTP integration URIs section of https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-stages.html for documented behavior.

How are you starting LocalStack?

With the localstack script

Steps To Reproduce

How are you starting localstack (e.g., bin/localstack command, arguments, or docker-compose.yml)

localstack start

Client commands (e.g., AWS SDK code snippet, or sequence of "awslocal" commands)

Place the following JSON in a file named api.json

{
   "openapi":"3.0.1",
   "info":{
      "title":"example",
      "version":"1.0"
   },
   "paths":{
      "/path1":{
         "get":{
            "x-amazon-apigateway-integration":{
               "httpMethod":"GET",
               "payloadFormatVersion":"1.0",
               "type":"HTTP_PROXY",
               "uri":"http://${stageVariables.foo}"
            }
         }
      }
   }
}

Create a REST API which has an HTTP integration URI referencing a stage variable

awslocal apigateway import-rest-api --body 'fileb:///path/to/api.json'

Create a deployment for our REST API

awslocal apigateway create-deployment --rest-api-id <rest-api-id>

Create a dummy stage and deployment for our REST API. Note the stage variables

awslocal apigateway create-stage --rest-api-id <rest-api-id> --stage-name v1 --variables foo=docker.host.internal:5000 --deployment-id <deployment-id>

Start up a dummy HTTP server to back our integration

python -m http.server 5000

Make a request against the API gateway

curl -ik https://<rest-api-id>.execute-api.localhost.localstack.cloud:4566/v1/path1

Environment

- OS: macOS Ventura (13.4)
- LocalStack: 2.1.0

Anything else?

No response

agershman avatar Jun 25 '23 15:06 agershman

Welcome to LocalStack! Thanks for reporting your first issue and our team will be working towards fixing the issue for you or reach out for more background information. We recommend joining our Slack Community for real-time help and drop a message to LocalStack Pro Support if you are a Pro user! If you are willing to contribute towards fixing this issue, please have a look at our contributing guidelines and our contributing guide.

localstack-bot avatar Jun 25 '23 15:06 localstack-bot

Hi Local stack team,

I am facing the exact sam issue with localstack latest comunity image.

api gateway swagger yaml integration endpoint definition: x-amazon-apigateway-integration: httpMethod: ANY uri: 'https://${stageVariables.HttpIntegrationUri}${stageVariables.CommServicePort}/grid-comm-service/api/v2.2/auth/{proxy}' responses: default: statusCode: '200'

When I hit my end point.

        `{"Type": "User", "message": "Error invoking integration for API Gateway ID '37u5it8m8t': Invalid URL 'https:///xyz-service/api/v2.2/auth/login': No host supplied", "__type": "InvalidRequest"}`
        
        instead of stage variable if I place static url It works. I also confirm my api stage contain these stage vars & values correclty.

Local stack logs: [et.reactor-1] localstack.request.aws : AWS apigateway.GetRestApi => 200 2024-07-09 00:54:28 2024-07-08T19:54:28.208 ERROR --- [et.reactor-1] l.s.apigateway.invocations : Error invoking integration for API Gateway ID '37u5it8m8t': Invalid URL 'https:///xyz-service/api/v2.2/auth/login': No host supplied 2024-07-09 00:54:28 Traceback (most recent call last): 2024-07-09 00:54:28 File "/opt/code/localstack/localstack-core/localstack/services/apigateway/invocations.py", line 316, in invoke_rest_api_integration 2024-07-09 00:54:28 response = invoke_rest_api_integration_backend(invocation_context) 2024-07-09 00:54:28 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-07-09 00:54:28 File "/opt/code/localstack/localstack-core/localstack/services/apigateway/invocations.py", line 383, in invoke_rest_api_integration_backend 2024-07-09 00:54:28 return HTTPIntegration().invoke(invocation_context) 2024-07-09 00:54:28 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-07-09 00:54:28 File "/opt/code/localstack/localstack-core/localstack/services/apigateway/integration.py", line 816, in invoke 2024-07-09 00:54:28 result = requests.request(method=method, url=uri, data=payload, headers=headers) 2024-07-09 00:54:28 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-07-09 00:54:28 File "/opt/code/localstack/.venv/lib/python3.11/site-packages/requests/api.py", line 59, in request 2024-07-09 00:54:28 return session.request(method=method, url=url, **kwargs) 2024-07-09 00:54:28 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-07-09 00:54:28 File "/opt/code/localstack/.venv/lib/python3.11/site-packages/requests/sessions.py", line 575, in request 2024-07-09 00:54:28 prep = self.prepare_request(req) 2024-07-09 00:54:28 ^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-07-09 00:54:28 File "/opt/code/localstack/.venv/lib/python3.11/site-packages/requests/sessions.py", line 484, in prepare_request 2024-07-09 00:54:28 p.prepare( 2024-07-09 00:54:28 File "/opt/code/localstack/.venv/lib/python3.11/site-packages/requests/models.py", line 367, in prepare 2024-07-09 00:54:28 self.prepare_url(url, params) 2024-07-09 00:54:28 File "/opt/code/localstack/.venv/lib/python3.11/site-packages/requests/models.py", line 444, in prepare_url 2024-07-09 00:54:28 raise InvalidURL(f"Invalid URL {url!r}: No host supplied") 2024-07-09 00:54:28 requests.exceptions.InvalidURL: Invalid URL 'https:///grid-comm-service/api/v2.2/auth/login': No host supplied

I would like to know if this issue got fix & working OR need diferent cofig otherwise.

ZaheerUdDeen avatar Jul 08 '24 20:07 ZaheerUdDeen

@alexrashed

ZaheerUdDeen avatar Jul 09 '24 18:07 ZaheerUdDeen

@ZaheerUdDeen this is a closed issue, please create a new issue with detailed steps on how to reproduce the error you are facing.

alexrashed avatar Jul 09 '24 18:07 alexrashed