Reputation: 957
I am trying to invoke a lambda function from another lambda using python SDK. Both the lambda functions belong to the same VPC. The trigger lambda only contains a python script that invokes the second lambda (loader_development). The loader_development lambda's APIGateway is private and it has a resource policy configured with it which denies access to all IP addresses which don't belong to that particular VPC.
My Python script in the trigger lambda is:
from __future__ import print_function
import json
import logging
import os
from urllib2 import urlopen,Request,HTTPError
import boto3
logger = logging.getLogger()
logger.setLevel(logging.INFO)
region = os.environ['AWS_REGION']
def lambda_handler(event, context):
invokeLambda = boto3.client('lambda', region_name = 'us-east-1')
request = {'resource':'/bucketstatus/latest','path':'/bucketstatus/latest','httpMethod':'GET'}
invoke_response = invokeLambda.invoke(FunctionName='loader_development',
InvocationType='RequestResponse',
Payload=json.dumps(request))
print(invoke_response['Payload'].read())
logger.info('Process Complete')
So /bucketstatus/latest
is a GET
request and this endpoint resides in the loader_development lambda's APIGateway (which is private). The loader_development lambda is a spring boot application whereas the trigger lambda is a standalone lambda that only has a python script to invoke an endpoint of the loader_development lambda to get the response.
While testing this script, it gives a status of 500 and an Internal Server Error. The error:
2019-10-09 10:09:09.279 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
10:09:09 at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:41)
10:09:09 at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:28)
10:09:09 at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxy(LambdaContainerHandler.java:174)
10:09:09 at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxyStream(LambdaContainerHandler.java:209)
10:09:09 at com.trimble.roadrunner.StreamLambdaHandler.handleRequest(StreamLambdaHandler.java:49)
10:09:09 at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:888)
10:09:09 at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:293)
10:09:09 at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:64)
10:09:09 at java.lang.Class.forName0(Native Method)
10:09:09 at java.lang.Class.forName(Class.java:348)
10:09:09 at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:114)
The weird part is that when I try to invoke some other lambda (microservice), the request gets processed and I get a status of 200. The example lambda is also inside a VPC and has a private APIGateway.
I am really not sure what I am missing. Any help would be greatly appreciated!
Upvotes: 1
Views: 906
Reputation: 957
W.R.T. GitHub solution, I was receiving this exception since I didn't include the requestContext
property on the request object. So I modified my request object to:
request = {'resource': '/bucketstatus/latest', 'path': '/bucketstatus/latest', 'httpMethod': 'GET',
'requestContext':{'accountId':'821665253511',
'resourceId': '',
'stage': 'development',
'requestId': context.aws_request_id,
'identity': {
'cognitoIdentityPoolId': None,
'accountId': None,
'cognitoIdentityId': None,
'caller': None,
'apiKey': None,
'sourceIp': '',
'cognitoAuthenticationType': None,
'cognitoAuthenticationProvider': None,
"userArn":context.invoked_function_arn
}
},
"apiId": "No need"
}
I was able to get the correct response with a status code of 200 after this change.
Upvotes: 1