Skip to content
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

Add aws lambda support #359

Merged
merged 1 commit into from
Jan 20, 2024
Merged

Conversation

blair55
Copy link

@blair55 blair55 commented Jan 12, 2024

Adds a Fable.Remoting.AwsLambda project that allows Fable.Remoting to work on AWS Lambda.

Fable.Remoting.AwsLambda/FableLambdaAdapter.fs is just a clone of Fable.Remoting.AzureFunctions.Worker/FableAzureFunctionsAdapter.fs with adaptations and dependencies required to work with the APIGatewayHttpApiV2ProxyRequest & APIGatewayHttpApiV2ProxyResponse types from the Amazon.Lambda.APIGatewayEvents package.

This code runs in a project I have on lambda, so while there are no tests I have evidence of it working :-)

I have also omitted any changes to the build project to publish a nuget package - I wanted your feedback before going any further 🙏

@blair55 blair55 force-pushed the add-aws-lambda-support branch from 5d67bdb to 473f59b Compare January 12, 2024 20:50
@blair55 blair55 force-pushed the add-aws-lambda-support branch from 473f59b to f01d363 Compare January 12, 2024 20:51
@blair55 blair55 marked this pull request as ready for review January 12, 2024 21:07
Comment on lines +13 to +14
type HttpRequestData = APIGatewayHttpApiV2ProxyRequest
type HttpResponseData = APIGatewayHttpApiV2ProxyResponse
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've aliased the AWS types for parity with the Azure Functions implementation to make comparison easier, but they don't need to remain.

@blair55
Copy link
Author

blair55 commented Jan 18, 2024

hi @Zaid-Ajaj 👋

I can't assign reviewers.. so just a heads up!

@Zaid-Ajaj
Copy link
Owner

Hi there @blair55 thanks a lot for the contribution! This looks like a very nice addition to the remoting packages. I will try to merge and publish it as soon as possible

@Zaid-Ajaj Zaid-Ajaj merged commit 630b0b0 into Zaid-Ajaj:master Jan 20, 2024
1 check failed
@Zaid-Ajaj
Copy link
Owner

Merged and published an initial version of Fable.Remoting.AwsLambda v1.0.0 🚀

I am curious though, it targets net5.0 now and fails to build when changing to net6.0, did you see a similar problem? 🤔

@blair55
Copy link
Author

blair55 commented Jan 22, 2024

I am targeting net6.0 in the repo that hosts the code with no build problems, and have only the 6.0.4 runtime installed.

So while the Fable.Remoting.AwsLambda project in this repo builds locally, I wasn't building faithfully with net5.0 😬

@victoradan
Copy link

@blair55 , thank you for adding this!
I'm interested in using Fable.Remoting with AWS Lambda. However, this implementation is specifically and exclusively targeting the request/response types of AWS's HTTP API v2.
However, AWS has a different pair or request/response types for its REST API, which I'm currently using.
It would be great to add support for this.

@Zaid-Ajaj , @blair55 , what is your recommended approach to adding this support? A new module under the same Fable.Remoting.AwsLambda project seems reasonable to me, but would like to get your thoughts and recommendations to add this. Thanks.

Please refer to the following AWS docs:

@blair55
Copy link
Author

blair55 commented Feb 27, 2024

Hi @victoradan!

First thought is that there is a lot of overlap between the old & new types They are not identical, but they do share a lot of root properties.

The types are just DTO classes used to bind the event json to. So you can expect many of the APIGatewayHttpApiV2ProxyRequest properties to be populated just as they would be on the APIGatewayProxyRequest type because the json payload is similar across both HTTP API or REST API events.

So you might get pretty far using the package as-is, and might not need the v1 types at all? Worth a try! 🤞

v1 APIGatewayProxyRequest

{
    "Body": null,
    "Headers": null,
    "HttpMethod": null,
    "IsBase64Encoded": false,
    "MultiValueHeaders": null,
    "MultiValueQueryStringParameters": null,
    "Path": null,
    "PathParameters": null,
    "QueryStringParameters": null,
    "RequestContext": null,
    "Resource": null,
    "StageVariables": null
}

v2 APIGatewayHttpApiV2ProxyRequest

{
    "Body": null,
    "Cookies": null,
    "Headers": null,
    "IsBase64Encoded": false,
    "PathParameters": null,
    "QueryStringParameters": null,
    "RawPath": null,
    "RawQueryString": null,
    "RequestContext": null,
    "RouteKey": null,
    "StageVariables": null,
    "Version": null
}

@blair55 blair55 deleted the add-aws-lambda-support branch February 27, 2024 11:59
@victoradan
Copy link

victoradan commented Feb 29, 2024 via email

@blair55
Copy link
Author

blair55 commented Mar 27, 2024

To make a start (if this is still of interest!)... install the Fable.Remoting package but use custom code for the adapter. That's how I developed and tested the changes in this PR. e.g.

[<LambdaSerializer(typeof<Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer>)>]
let Handler (request: APIGatewayProxyRequest) =
  Remoting.createApi ()
  |> Remoting.withErrorHandler errorHandler
  |> Remoting.fromValue (api request)
  |> Remoting.buildRequestHandler
  |> HttpResponseData.fromRequestHandler request

Where HttpResponseData.fromRequestHandler is a function that you own in your own project. Just copy the contents of Fable.Remoting.AwsLambda/FableLambdaAdapter.fs. Then switch the V2 types for the V1 types: that's just two lines to change given how they are aliased. Then adapt the function to work with the different property names etc.

If this is successful then perhaps it could be introduced as a new project.

@victoradan
Copy link

victoradan commented Mar 29, 2024 via email

@blair55
Copy link
Author

blair55 commented Mar 29, 2024

Yep, I agree with the approach. No need for another project, just a separate file in the existing lambda project. Good stuff! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants