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

Lambda-http: vary type of response based on request origin #269

Merged
merged 6 commits into from
Feb 7, 2021

Conversation

l3ku
Copy link
Contributor

@l3ku l3ku commented Nov 24, 2020

Issue #, if available:
#267

Description of changes:

ApiGatewayV2, ApiGateway and Alb all expect different types of responses to be returned from the invoked lambda function. Thus, it makes sense to pass the request origin to the creation of the response, so that the correct type of LambdaResponse is returned from the function.

This PR also adds support for the "cookies" attribute which can be used for returning multiple Set-cookie headers from a lambda invoked via ApiGatewayV2, since ApiGatewayV2 no longer seems to recognize the "multiValueHeaders" attribute.

On lines 129-132 of response.rs the SET_COOKIE headers are removed from the headers returned to API Gateway v2, because otherwise API Gateway v2 would return three set-cookie headers when just returning two cookies in cookies, because one of the cookies would be duplicated in the headers. I tried using the http::header::OccupiedEntry::remove_entry_mult function for removing the SET_COOKIE headers from the headers, but I got some panicking from the http crate as described in hyperium/http#446. So I went with the solution where I first fetch the SET_COOKIE headers returned with headers.get_all and insert them cloned into the cookies, and then remove them with headers.remove(SET_COOKIE). However, any ideas and suggestions as to improve this are warmly welcome!

By submitting this pull request

  • I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • I confirm that I've made a best effort attempt to update all relevant documentation.

ApiGatewayV2, ApiGateway and Alb all expect different types of responses to
be returned from the invoked lambda function. Thus, it makes sense to pass
the request origin to the creation of the response, so that the correct
type of LambdaResponse is returned from the function.

This commit also adds support for the "cookies" attribute which can be used
for returning multiple Set-cookie headers from a lambda invoked via
ApiGatewayV2, since ApiGatewayV2 no longer seems to recognize the
"multiValueHeaders" attribute.

Closes: awslabs#267.
Copy link
Contributor

@softprops softprops left a comment

Choose a reason for hiding this comment

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

This is excellent.

At first there was only rest apis ... and then albs.. and then http apis.

When we got to supporting three types the current impl didn't feel right but this feels a lot cleaner and provides an way forward when the inevitable 4th http like trigger comes along

@@ -149,7 +152,7 @@ where

#[doc(hidden)]
pub struct TransformResponse<R, E> {
is_alb: bool,
request_origin: RequestOrigin,
Copy link
Contributor

Choose a reason for hiding this comment

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

Much cleaner

Copy link
Contributor

@calavera calavera left a comment

Choose a reason for hiding this comment

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

It'd be awesome if this got merged into master @davidbarsky.

@miere
Copy link

miere commented Dec 21, 2020

An honest question: why is this runtime maintaining its own implementation of the AWS Events where one could get them from the aws_lambda_event crate?

@bahildebrand bahildebrand merged commit 5556ab2 into awslabs:master Feb 7, 2021
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.

5 participants