-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Support Lambda Function Urls in local start-api #4299
Comments
Thanks for raising this feature request! I'll bring this up with the team to discuss about next steps |
This is a pretty big blocker for me. Not being able to develop locally without deploying test functions is very annoying. Great to hear it's being looked into. |
This could be a really nice feature to implement, I tried to use a local template file with a dummy api gateway event, this may work for some but the main problem is the Payload format version, in the case of a regular lambda with lambda url function, the version is 2.0 and when executing sam local start-api the only version available is 1.0, there should not be many changes requierd to make this work, adding support for reading the lambda url details form the template file and using payload v2.0 when this is detected, I'll be happy to help with this. |
@JP-tech-sh thanks for offering to contribute! Would you be willing to write up a design proposal in this issue? Once we have clarity on design you can open a PR. |
I think one difference between Function URLs and API Gateway is that functions URLs are all served directly at the root What would be a good experience for you when you have multiple functions with URLs on the same template? Options I can think of:
|
As a user I would prefer to have a different port per Lambda function URL, and APIGW. Having different prefixes in the path would work if all you do is call it once, but when pointing to the API from another application (e.g. a UI) where the paths are assuming to be at the Also, the user should be able to specify by resource name/id the lambda function to start so that you only start that one, which I think is a behavior that already exists for the single invocation functionality. |
@JP-tech-sh It's a really valid point that you have raised here. This is the major issue that impacts usability, hopefully the payload version is an easy fix, this would make it bearable for the time being and ultimately hope this Feature Request goes through. |
I would also like this feature! As a workaround I use
No idea why Sources: |
Thanks a lot for that workaround! This solution looks good enough for me to run on my local. Do you know how to get methods other than POST to work? |
FWIW the date |
Extending @AxelJunker's method, if you used the I found this worked quite well to invoke the lambda directly (my lambda returns HTML): $ curl -XPOST "http://127.0.0.1:3001/2015-03-31/functions/MyFunction/invocations" -d @events/event.json
{"statusCode": 200, "headers": {"Content-Type": "text/html"}, "body": "..."} However, I couldn't execute that in the browser. So I threw together a quick Flask app to forward requests so I could try it locally. Essentially this is answering @sayandcode's question as well. requirements.txt flask
requests app.py import os
import requests
from flask import Flask, request, Response
LAMBDA_HOST = os.getenv('LAMBDA_HOST', '127.0.0.1:3001')
TARGET_URL = 'http://{}/2015-03-31/functions/{}/invocations'
app = Flask(__name__)
@app.route('/<function>', methods=['GET'])
def forward_request(function):
url = TARGET_URL.format(LAMBDA_HOST, function)
payload = {
'httpMethod': 'GET',
'queryStringParameters': request.args.to_dict(),
}
response = requests.post(url, json=payload)
response.raise_for_status()
lambda_response = response.json()
return Response(lambda_response['body'], status=lambda_response['statusCode'], headers=lambda_response['headers']) There's way more you can do with the payload etc. but this was all I really needed. I just started everything then went in to the browser and hit http://127.0.0.1:5000/MyFunction?a=b as a test. Forwarder: $ flask run
...
127.0.0.1 - - [19/Jun/2023 21:42:10] "GET /MyFunction?a=b HTTP/1.1" 200 - Lambda: $ sam local start-lambda --force-image-build
...
START RequestId: d88b7596-5bd8-4be0-9703-3249c6645e4d Version: $LATEST
{'httpMethod': 'GET', 'queryStringParameters': {'a': 'b'}}
END RequestId: d88b7596-5bd8-4be0-9703-3249c6645e4d
REPORT RequestId: d88b7596-5bd8-4be0-9703-3249c6645e4d Init Duration: 0.22 ms Duration: 182.33 ms Billed Duration: 183 ms Memory Size: 128 MB Max Memory Used: 128 MB
2023-06-19 21:41:31 127.0.0.1 - - [19/Jun/2023 21:41:31] "POST /2015-03-31/functions/MyFunction/invocations HTTP/1.1" 200 - HTH It'd definitely be much nicer to have this built in somehow, however. I'll have a look at the code at some point and see if there's any obvious way of integrating it. |
I ran into this right now, using Lambda Function integrated with REST API GW. Will share a template that's causing this as I get time. The issue is, when I deploy Lambda to AWS using sam template the payload format version is 2.0 while on local debugging setup the payload format version is 2.0, this is causing some nightmares in local debugging. For instance, pathParameters and rawQueryString are different in both format versions |
I am also running into this now. I tried the Python Flask app and it works for making a request (with query parameters) directly from the browser (and curl), but I am getting a cors error when my local dev setup calls the API |
I managed to fix the CORS issue by rewriting the last line in return Response(lambda_response['body'], status=lambda_response['statusCode'], headers=lambda_response['headers'] | {'Access-Control-Allow-Origin': '*'}) |
This would be super helpful, for some bizarre reason our API Gateway integration just stopped working and we would love to use anything else. |
Describe your idea/feature/enhancement
With the addition of Lambda Function URLs, we don't need API Gateway for full APIs. However, the
sam local start-api
command does not support that yet, and fails with the error that no API exists.An example of such SAM template can be found in https://github.com/lambrospetrou/aws-playground/blob/master/aws-lambda-url-and-fly-golang-proxy/aws-iac/sam-template.yml. Relevant excerpt below:
Proposal
Ideally, the cli should be able to add the Function URLs defined as part of the expose API, or even expose them in different ports if we want them separate from a potential API Gateway definition.
Things to consider:
The text was updated successfully, but these errors were encountered: