Skip to content
This repository was archived by the owner on Sep 8, 2021. It is now read-only.

Adding ALB Support Out of the Box. #5

Merged
merged 2 commits into from
Dec 4, 2018
Merged

Conversation

atrope
Copy link
Contributor

@atrope atrope commented Dec 2, 2018

Adding Support for PHP to output directly to an ALB

Checked Here:
https://aws.amazon.com/pt/blogs/networking-and-content-delivery/lambda-functions-as-targets-for-application-load-balancers/

We need to add isBase64Encoded= false and the statusDescription to the Response in order to ALB to accepts it as a valid response and not give us 502.

The problem is that if we always add, Api Gateway complains and throws us an error :)

With this little code, everything works seamlessly(with multiValueHeaders enabled in the ELB Target group) without the user have the need to worry. It supports Api Gateway + ALB (If someone want to do some endpoints in ALB directly to save some Api Gateway Requests)

Hope you enjoy

Copy link
Member

@txase txase left a comment

Choose a reason for hiding this comment

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

Let's figure out what happens for unusual status codes. Then this looks good to merge!

🙏

bootstrap Outdated
$isALB = array_key_exists("elb", $event['requestContext']);
if ($isALB) { // Add Headers For ALB
$status = $response["statusCode"];
$response["statusDescription"] = "$status ". $http_codes[$status];
Copy link
Member

Choose a reason for hiding this comment

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

What happens if the status code isn't in http_codes? I think PHP would throw an error.

Maybe only append the http_codes message if the status exists?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @txase We could put it inside a try statement

example:


try {
    $response["statusDescription"] = "$status ". $http_codes[$status];
} catch (Exception $e) {
    $response["statusDescription"] = "$status Unknown"
}

Something like that what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

What about this:

if (array_key_exists($status, $http_codes)) {
    $response["statusDescription"] = "$status ". $http_codes[$status];
} else {
    $response["statusDescription"] = "$status Unknown";
}

I think it's better to check for conditions explicitly rather than capture all errors (including those you didn't mean to capture).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems great to me 👍

Copy link
Member

Choose a reason for hiding this comment

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

Cool, can you update the PR and test it out? 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated and tested. Everything worked great.
I Couldnt find any HTTP code to test the unknown status code, but in theory it should work withou an issue.

Copy link
Member

@txase txase left a comment

Choose a reason for hiding this comment

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

Looks good! 🤘

@txase txase merged commit 75eeb3f into stackery:master Dec 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants