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

The incoming event is not a valid request from Amazon API Gateway or an Application Load Balancer #245

Closed
balihb opened this issue Mar 4, 2019 · 16 comments

Comments

@balihb
Copy link

balihb commented Mar 4, 2019

  • Framework version: latest
  • Implementations: Spring Boot

Scenario

I'm trying to create two Spring Boot function. One supposed to call the other, I can call both from outside (Api Gateway), but if I try to do it directly with lambda invoke I get this:
The incoming event is not a valid request from Amazon API Gateway or an Application Load Balancer
either from code or from the lambda console.
What am I missing?

Expected behavior

Works the same way with invoker as through api gateway.

Actual behavior

Only works with api gateway.

Steps to reproduce

The code is here:
https://github.com/balihb/fifi

./gradlew :aws:buildFibZip :aws:buildFibtimerZip
cd aws
./deploy.sh

Full log output

93) [LambdaSandboxJava-1.0.jar:na]
	at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64) [LambdaSandboxJava-1.0.jar:na]
	at java.lang.Class.forName0(Native Method) [na:1.8.0_181]
	at java.lang.Class.forName(Class.java:348) [na:1.8.0_181]
	at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:104) [LambdaJavaRTEntry-1.0.jar:na]

2019-03-04 15:43:35.920 ERROR 1 --- [           main] c.a.s.proxy.AwsProxyExceptionHandler     : Called exception handler for:

com.amazonaws.serverless.exceptions.InvalidRequestEventException: The incoming event is not a valid request from Amazon API Gateway or an Application Load Balancer
	at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:41) ~[aws-serverless-java-container-core-1.3.1.jar:na]
	at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:28) ~[aws-serverless-java-container-core-1.3.1.jar:na]
	at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxy(LambdaContainerHandler.java:174) [aws-serverless-java-container-core-1.3.1.jar:na]
	at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxyStream(LambdaContainerHandler.java:209) [aws-serverless-java-container-core-1.3.1.jar:na]
	at balihb.fifi.fib.aws.StreamLambdaHandler.handleRequest(StreamLambdaHandler.java:33) [task/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
	at lambdainternal.EventHandlerLoader$StreamMethodRequestHandler.handleRequest(EventHandlerLoader.java:350) [LambdaSandboxJava-1.0.jar:na]
	at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888) [LambdaSandboxJava-1.0.jar:na]
	at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:293) [LambdaSandboxJava-1.0.jar:na]
	at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64) [LambdaSandboxJava-1.0.jar:na]
	at java.lang.Class.forName0(Native Method) [na:1.8.0_181]
	at java.lang.Class.forName(Class.java:348) [na:1.8.0_181]
	at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:104) [LambdaJavaRTEntry-1.0.jar:na]

com.amazonaws.serverless.exceptions.InvalidRequestEventException: The incoming event is not a valid request from Amazon API Gateway or an Application Load Balancer
	at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:41)
	at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:28)
	at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxy(LambdaContainerHandler.java:174)
	at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxyStream(LambdaContainerHandler.java:209)
	at balihb.fifi.fib.aws.StreamLambdaHandler.handleRequest(StreamLambdaHandler.java:33)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at lambdainternal.EventHandlerLoader$StreamMethodRequestHandler.handleRequest(EventHandlerLoader.java:350)
	at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888)
	at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:293)
	at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:348)
	at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:104)
END RequestId: 4aea4f27-70f1-458a-90fb-f5358a5e8f0f
REPORT RequestId: 4aea4f27-70f1-458a-90fb-f5358a5e8f0f	Duration: 5.94 ms	Billed Duration: 100 ms 	Memory Size: 1512 MB	Max Memory Used: 193 MB
@sapessi
Copy link
Collaborator

sapessi commented Mar 4, 2019

Hey @balihb - the framework expects an event that is shaped like an API Gateway or ALB proxy event. That doesn't mean that the call must come from API Gateway or ALB, just that the event should have the same model. If you wanted to, you could use the AwsProxyRequest object from the model package to generate the event.

If these two Lambda functions are meant to be separate services - meaning they have different scaling factors and will eventually be managed by separate teams - I'd recommend putting an actual API in-between them.

@sapessi
Copy link
Collaborator

sapessi commented Mar 12, 2019

Can I resolve this @balihb?

@balihb
Copy link
Author

balihb commented Mar 13, 2019

I've tried to emulate an api gateway request with AwsProxyRequest, but I haven't been able to figure out a way to access the data from the current request (so I can maybe forward it to the next request).

@sapessi
Copy link
Collaborator

sapessi commented Mar 13, 2019

If you simply want to forward the current request to the other Lambda function you can access the incoming event from the HttpServletRequest as an attribute. See the RequestReader code here.

@sapessi
Copy link
Collaborator

sapessi commented Jun 26, 2019

Assuming this issues is solved. Closing.

@sapessi sapessi closed this as completed Jun 26, 2019
@thanh-nguyen-van
Copy link

thanh-nguyen-van commented Jul 12, 2019

I got the same issue when Added cloudwatch event to lambda function

@sapessi
Copy link
Collaborator

sapessi commented Jul 15, 2019

Hi @Thanh3d, what event are you trying to handle? Is it sent by an AWS service or are you calling the Lambda function directly? In general, you have two options:

  1. Use the AwsProxyRequest object as the event for the second Lambda function.
  2. Create an alternative implementation of the RequestReader as described in the wiki guide.

@mohit-saw
Copy link

mohit-saw commented Aug 17, 2019

I am trying to deploy the spring boot project generated by Maven archetype for Spring Boot 2.x on AWS lambda and calling its /ping endpoint with the help of AWS API gateway but I am getting the same error reported here i.e The incoming event is not a valid request from Amazon API Gateway or an Application Load Balancer

Here is my code:

https://my-lambda-code-bucket.s3.us-east-2.amazonaws.com/my-service-1.0-SNAPSHOT-lambda-package.zip

Here is my api endpoint:
https://gpvvq6g3i1.execute-api.us-east-2.amazonaws.com/Prod/ping

@sapessi
Copy link
Collaborator

sapessi commented Aug 17, 2019

Hi @mohit-saw, make sure that you have configured API Gateway as a proxy integration. See screenshot here. If this is not the root cause of the issue, could you share how you deployed and configured the function + API Gateway? Did you use the SAM file?

@petrucristescu
Copy link

petrucristescu commented Jan 16, 2020

I'm getting the same error:

The incoming event is not a valid request from Amazon API Gateway or an Application Load Balancer

I have the following configuration:

  SomeEndpoint:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: SomeCodeUri
      Handler: LambdaHandler::handleRequest
        Warmup:
          Type: Schedule
          Properties:
            Schedule: cron(0/5 8-17 ? * MON-FRI *)

I think a http method is expected on the request judging by what I see here:

if (request.getHttpMethod() == null || request.getHttpMethod().equals("") || request.getRequestContext() == null) {
            throw new InvalidRequestEventException(INVALID_REQUEST_ERROR);
        }

@DogukanZengin
Copy link

DogukanZengin commented Oct 27, 2020

I'm getting the same error:

The incoming event is not a valid request from Amazon API Gateway or an Application Load Balancer

I have the following configuration:

  SomeEndpoint:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: SomeCodeUri
      Handler: LambdaHandler::handleRequest
        Warmup:
          Type: Schedule
          Properties:
            Schedule: cron(0/5 8-17 ? * MON-FRI *)

I think a http method is expected on the request judging by what I see here:

if (request.getHttpMethod() == null || request.getHttpMethod().equals("") || request.getRequestContext() == null) {
            throw new InvalidRequestEventException(INVALID_REQUEST_ERROR);
        }

Do we have a sample handler class for this error? This is happening because the warmup plugin sends an event but it is not a gateway event. We should have a condition to return directly if the request is coming from warmup plugin.

@sapessi
Copy link
Collaborator

sapessi commented Oct 29, 2020

Have you considered using provisioned concurrency instead? Much more reliable

@petrucristescu
Copy link

I'm getting the same error:
The incoming event is not a valid request from Amazon API Gateway or an Application Load Balancer
I have the following configuration:

  SomeEndpoint:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: SomeCodeUri
      Handler: LambdaHandler::handleRequest
        Warmup:
          Type: Schedule
          Properties:
            Schedule: cron(0/5 8-17 ? * MON-FRI *)

I think a http method is expected on the request judging by what I see here:

if (request.getHttpMethod() == null || request.getHttpMethod().equals("") || request.getRequestContext() == null) {
            throw new InvalidRequestEventException(INVALID_REQUEST_ERROR);
        }

Do we have a sample handler class for this error? This is happening because the warmup plugin sends an event but it is not a gateway event. We should have a condition to return directly if the request is coming from warmup plugin.

It's been a while since I've played with this and I don't have the sample to provide it. Meanwhile we've dropped the warmup part.

@DogukanZengin
Copy link

I'm getting the same error:
The incoming event is not a valid request from Amazon API Gateway or an Application Load Balancer
I have the following configuration:

  SomeEndpoint:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: SomeCodeUri
      Handler: LambdaHandler::handleRequest
        Warmup:
          Type: Schedule
          Properties:
            Schedule: cron(0/5 8-17 ? * MON-FRI *)

I think a http method is expected on the request judging by what I see here:

if (request.getHttpMethod() == null || request.getHttpMethod().equals("") || request.getRequestContext() == null) {
            throw new InvalidRequestEventException(INVALID_REQUEST_ERROR);
        }

Do we have a sample handler class for this error? This is happening because the warmup plugin sends an event but it is not a gateway event. We should have a condition to return directly if the request is coming from warmup plugin.

It's been a while since I've played with this and I don't have the sample to provide it. Meanwhile we've dropped the warmup part.

Thanks for the answer, We have also decided to switch to provisioned concurrency

@sramaven
Copy link

Hi @mohit-saw, make sure that you have configured API Gateway as a proxy integration. See screenshot here. If this is not the root cause of the issue, could you share how you deployed and configured the function + API Gateway? Did you use the SAM file?

Hi, I am trying to invoke function through sam local invoke then I am facing same error, I am trying to run this through sam.yaml file

@sramaven
Copy link

Hi @mohit-saw, make sure that you have configured API Gateway as a proxy integration. See screenshot here. If this is not the root cause of the issue, could you share how you deployed and configured the function + API Gateway? Did you use the SAM file?

Hi, I am trying to invoke function through sam local invoke then I am facing same error, I am trying to run this through sam.yaml file

May I get any solution

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

No branches or pull requests

7 participants