This is the aws lambda function which will invoke an api:
'use strict';
var request = require("request")
exports.handler = function (event, context,callback) {
let url = "https://3sawt0jvzf.execute-api.us-east-1.amazonaws.com/prod/test"
request({
url: url,
method: "POST",
json: event,
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
callback(null, { "isBase64Encoded": true|false,
"statusCode": "200",
"headers": { "headerName": "headerValue"},
"body": body});
}
else {
console.log("error: " + error)
console.log("response.statusCode: " + response.statusCode)
console.log("response.statusText: " + response.statusText)
}
})
};
This is the api written as an aws lambda function:
'use strict';
exports.handler = function(event, context, callback) {
console.log(event.name);
callback(null, { "isBase64Encoded": true|false,
"statusCode": "200",
"headers": { "headerName": "headerValue"},
"body": `Hello World ${event.name}`}); // SUCCESS with message
};
When I try to call the api from the lambda function it just returns "Hello World undefined". It is not appending the name at the end and returning the correct response.
Like this, we can call any REST API in our Lambda Function and perform any serverless operations on the response. To know more about Lambda functions and get a hand over the complete working code, please visit our Github Repository here.
Looks like an ability to directly call Lambdas over the Internet without an API Gateway was just added to the SDK. Function URLs are available using the Lambda API and are supported in CloudFormation, AWS SAM and AWS CDK.
You can invoke Lambda functions directly using the Lambda console, a function URL HTTP(S) endpoint, the Lambda API, an AWS SDK, the AWS Command Line Interface (AWS CLI), and AWS toolkits.
To stop it from retrying, you should make sure that any error is handled and you tell Lambda that your invocation finished successfully by returning a non-error (or in Node, calling callback(null, <any>) . To do this you can enclose the entire body of your handler function with a try-catch.
Assumptions:
You're misunderstanding what event
is. This is NOT the JSON payload that you sent through your HTTP request.
An HTTP request through the API Gateway gets transformed into an event
object similar to this:
{
"resource": "Resource path",
"path": "Path parameter",
"httpMethod": "Incoming request's method name"
"headers": {Incoming request headers}
"queryStringParameters": {query string parameters }
"pathParameters": {path parameters}
"stageVariables": {Applicable stage variables}
"requestContext": {Request context, including authorizer-returned key-value pairs}
"body": "A JSON string of the request payload."
"isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"
}
As you can see, the JSON payload is accessible in a stringified form in event.body
.
If you want to send the pass the same payload to the second Lambda, you have to parse it first.
const body = JSON.parse(event.body)
Then, send body
instead of event
.
Then, in your second Lambda, you parse the stringified JSON in event.body
and then you get your original payload back.
If you sent name
in that original payload, you can get it from JSON.parse(event.body).name
.
Reference: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-simple-proxy-for-lambda-input-format
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With