aws-samples/php-examples-for-aws-lambda

0.7 container image demo app doesn't work correctly once deployed

bawbgale opened this issue · 3 comments

I followed all the steps to the letter and tested along the way.

It passes the first local test:

> curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"queryStringParameters": {"name":"Ben"}}'
{"statusCode":200,"headers":{"Content-Type":"application\/json","Access-Control-Allow-Origin":"*","Access-Control-Allow-Headers":"Content-Type","Access-Control-Allow-Methods":"OPTIONS,POST"},"body":"Hello, Ben"}%

Still works after sam build

> echo '{"queryStringParameters": {"name":"Ben"}}' | sam local invoke --event  - "myPHPLambdaFuncton"
Reading invoke payload from stdin (you can also pass it from file with --event)
Invoking Container created from myphplambdafuncton:phpoci
Building image.................
Skip pulling image and use local one: myphplambdafuncton:rapid-1.37.0-x86_64.

START RequestId: dc7a8354-9167-4f15-837b-467715a9efdc Version: $LATEST


 END RequestId: dc7a8354-9167-4f15-837b-467715a9efdc
REPORT RequestId: dc7a8354-9167-4f15-837b-467715a9efdc  Init Duration: 0.93 ms  Duration: 48.44 ms      Billed Duration: 49 ms  Memory Size: 128 MB     Max Memory Used: 128 MB
{"statusCode":200,"headers":{"Content-Type":"application\/json","Access-Control-Allow-Origin":"*","Access-Control-Allow-Headers":"Content-Type","Access-Control-Allow-Methods":"OPTIONS,POST"},"body":"Hello, Ben"}%                                                                                                                                                      

But after I run sam deploy, the resulting Lambda does not return the correct output:

> curl "https://jvtu5i3mgl.execute-api.us-east-1.amazonaws.com" -d '{"queryStringParameters": {"name":"Ben"}}'         
Hello, %

One odd thing that happened along the way: sam deploy -g did NOT prompt for image repository, as the README said. When I looked in samconfig.toml, I saw this value:

image_repositories = ["myPHPLambdaFuncton=592584503830.dkr.ecr.us-east-1.amazonaws.com/myphplambdacontainerdemob95cc0b8/myphplambdafuncton976009f2repo"]

And I noticed that a second ECR repository with this name was created. So I tried manually changing it to the repository URI from the previous step:

image_repositories = ["myPHPLambdaFuncton=592584503830.dkr.ecr.us-east-1.amazonaws.com/php-lambda-functon"]

and reran sam deploy. But I got the same result.

Update: I found that the function does return a correct response when testing with the Lambda console:
Screen Shot 2022-02-22 at 11 58 39 AM

But CURLing the API Gateway still gives me an incorrect response:

> curl "https://jvtu5i3mgl.execute-api.us-east-1.amazonaws.com" -d '{"queryStringParameters": {"name":"Ben"}}'         
Hello, %

The issue is due to the fact that API GW will base 64 encode the incoming body, as well as wrap the payload in a bunch of other headers. You can see this if you add print statements to the index.php and then re-deploy. I'm going to submit a PR against this repo to fix, but if you change the src/index.php index function to return APIResponse("Hello, ". json_decode(base64_decode($data['body']), true)['queryStringParameters']['name']); then you will get the proper output in from the deployed lambda, however it will not work with the provided payload locally, it needs to be base64 encoded
curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"body": "eyJxdWVyeVN0cmluZ1BhcmFtZXRlcnMiOiB7Im5hbWUiOiJCZW4ifX0="}' like so

Thanks for the PR, this has now been merged.