AWS Lambda with API Gateway and with "Raw JSON Payload" - amazon-web-services

I have a Lambda written in C# that is called via API Gateway. The Lambda's Handler() function has the following signature:
public async Task<APIGatewayProxyResponse> Handler(APIGatewayProxyRequest request, ILambdaContext context)
This Lambda works fine when called from an HTTP client.
I have another Lambda with this signature that also works just fine:
public async Task<int> Handler(JObject jasonRequest, ILambdaContext context)
I have only ever called this Lambda from the AWS Console though.
What I need is for the same Lambda to be able to be called either way. I need to be able to identify an incoming API Gateway request (so that I can retrieve the request.Path and request.Body and return a response.StatusCode) but I also need to be able to call the Lambda by passing it a "raw JSON payload" (not sure of the terminology here), as I do when testing the Lambda in the AWS Console.
So, is there a way to set up my Lambda's signature and return type so that I can respond to both API Gateway requests as well raw JSON payloads? I am not asking this just to be able to test via the AWS Console. Another team at our company will call the Lambda via EventBridge and I think it will send a raw JSON payload.

Related

Do not send raw body to AWS Lambda via API Gateway

I have an aws lambda function written in node. I have a HTTP API in API Gateway which calls this lambda function.
The "issue" that I'm having is that my request payload which is a JSON, is sent wrapped in a raw body along with a bunch of other fields which I don't really need in my lambda.
How can I have the HTTP API to only send my payload?
If I use a REST API, at least by default, it works the way I expect.

Event object is empty when received by the lambda function

I am calling a lambda function from my API Gateway with Lambda proxy integration enabled. But when I am receive the event object in the function it is an empty object i.e. '{}'. I have tested the function with a test input available on AWS Console with dummy request object and it works fine. I am using AWS Cognito Pool as authorizer for the API Gateway Method.
In the function I have made an object that returns me dummy data and event object itself. When I hit the API from Postman or my frontend application it returns me with the dummy data and the event object is empty.
I have looked at the following Stackoverflow questions but to no avail.
Event Object is empty in AWS Lambda nodejs function
Passing event from API gateway to Lambda
Event object is empty in api gateway call to lambda
Api Gateway sends empty parameters to AWS lambda
Lambda event returns empty object
This is a small thing and is really hindering my progress. Would really appreciate any help.
Just made whole another Lambda and API Gateway setup and it worked. Must have been bad API Gateway configuration

How does AWS lambda get triggered by an api request?

Im assuming lambda is like the missing piece of the puzzle for a complete api request. So you create the apigateway and then write the lambda function which bridges the gap between taking a request and returning the output of the lambda function as the http response.
I've successfully followed guides on how to set up an API gateway that triggers AWS lambda to do something, but I still don't really understand what is being done.
How is the function def handler(event, context): being called by the aws apigateway? How does it get triggered and how is the output of handler sent back?
You do not need a Lambda "in the middle". Using Lambda Proxy integration in the API Gateway you can receive the full information about the request (endpoint URL, query parameters, etc) in your targeted Lambda event.
Have a look at the following Tutorial how to setup Lambda Proxy integration with API Gateway.
The tricky thing you should care about is the structure of the response that you will return from your lambda_handler. See the requirements here.
Answering the question of "how this happens"... In short, when an HTTP request comes to your API endpoint it is automatically routed to the mapped Lambda function. Behind the scenes a new container for the Function is spawned and your request comes to the event of the lambda_handler. API Gateway by default also creates a CloudFront distribution in front of itself to serve your requests more efficiently. Once your Lambda returns the response, API Gateway parses it and constructs the HTTP response out of it. The nice thing is that all of this is managed by AWS.

How to determine in lambda where request is coming from?

I have a AWS setup containing an api gateway resource and a lambda function.
I need to determine in lambda function if request is coming from direct invocation or through api gateway invocation.
How would this be possible?
For now I tried to find something suitable on google, but without success unfortunatly.
I'm sure you've noticed that a new Lambda function is declared as follows:
exports.handler = (event, context, callback) => {
// Your code goes here
}
The event object here contains information regarding the invocation of your Lambda Function. For example, if your lambda is triggered by an upload to S3 this will contain information about the object being uploaded for example or in your case, it will contain API Gateway information.
See more documentation on AWS Lambda's integration with other services here.

Can I send static response via aws api gate way?

I am creating an AWS ApiGateway & it invoking a lambda function asynchronously. Once apigateway sent the message lambda it'll send a 200 response with empty response. I need to send some static information with in the response message.
{
"code":"SUCCESS",
"description":"Successfully sent to lambda"
}
I tried to set up a model. But there I'm unable to set static values. Please help on this ?
You need to configure AWS API Gateway Integration Response Data Mapping return the response message from your Lambda function. If you use Lambda proxy, you can directly send the entire response including headers.
Note: If its only a static response, you don't need Lambda, you can use a Mock Endpoint with static message response.