LambdaInvalidResponse nodejs - node.js

I have the following code running as an AWS Lambda function:
const getDb = require('./db');
const Service = require('./service');
exports.main = async function (event, context) {
context.callbackWaitsForEmptyEventLoop = false;
const endpoint = event.path.split('/')[1].toLowerCase();
const service = new Service(
await getDb()
);
const result = await service[endpoint](); // result might be number or string, i.e. 123 or '123'
return {
isBase64Encoded: false,
statusCode: 200,
statusDescription: '200 OK',
headers: {
'Set-cookie': 'cookies',
'Content-Type': 'application/json',
},
body: result,
};
};
When I invoke it with aws-cli or the testing function in AWS console it works, but when I put it behind an ALB (application load balancer) it returns 502 Bad Gateway.
I have viewed the Lambda output CloudWatch logs. The endpoint is correctly invoked by the load balancer, but the load balancer access logs have the following entry:
2021-11-08T16:28:24.033000Z "forward" "-" "LambdaInvalidResponse" "-" "-" "-" "-"
Any ideas why I'm experiencing this issue?

Not sure which of these things fixed it, but I did fix it:
Wrap all properties in response object in quotes, i.e. { "body": result } instead of { body: result }
serialized result, i.e. { "body": JSON.stringify({ result }) } instead of { "body": result }
removed property statusDescription from response object

Related

Why is event.requestContext undefined for my AWS Lambda function?

Why was I unable to obtain the remote IP address of the user?
When I clicked on the test button or the function URL, it seems that event.requestContext was undefined. Why?
const res = {
statusCode: 200,
isBase64Encoded: false,
headers: {
"Access-Control-Allow-Origin":"*",
"Content-Type": "text/plain"
},
multiValueHeader: {}
};
export const handler = async(event) => {
const ip = event.requestContext.identity.sourceIp;
res.body="hi "+ip;
return res;
}
It seems that the event format is incorrect. This works:
const ip = event.requestContext.http.sourceIp;
What I used was the format for Lambda version 1 but I am now using version 2.

Unable to run AWS StepFunctions inside an AWS Lambda

I'm trying to start the execution of an AWS StepFunction from inside an AWS Lambda, but I receive null as result, with no error message. The StepFunctions here is an Express State Machine, so I use the method startSyncExecution(params = {}, callback), as pointed in the docs.
Here is the code of the Lambda:
const AWS = require('aws-sdk');
exports.handler = async(event, context, callback) => {
var params = {
stateMachineArn: "arn:aws:states:us-east-1:[AccountID]:stateMachine:BookLectureStateMachine",
input: JSON.stringify(event),
name: "test-from-lambda"
}
var stepfunctions = new AWS.StepFunctions();
console.log("Everything okay") //This one is logged
stepfunctions.startSyncExecution(params, function(err, data) {
console.log("This log isn't shown"); //This one isn't logged
if (err) {
callback(null, {
statusCode: 400,
body: err,
headers: {
'Access-Control-Allow-Origin': '*'
}
})
} else {
callback(null, {
statusCode: 200,
body: 'Lecture booked',
headers: {
'Access-Control-Allow-Origin': '*'
}
})
}
});
};
The response is null, nothing else.
I've checked the permissions, and the Lambda has Full Access to the Step Functions.
Any idea on how to solve it?
UPDATE
I think the StepFunction is not executed, since the logs are empty.
I increased the Lambda timeout to 1min in order to avoid timeout scenarios. The billed duration is about half a second
I believe it may have something to do with the mix of callbacks and async. Since you aren't using await in your handler anywhere, I would try removing async from the handler.
Either that or you could try changing the code to:
var data = await stepfunctions.startSyncExecution(params).promise()

PUT doesn't work on POSTMAN but okay when called from front-end

I have the following lambda function that works when called from the front-end React app:
// Update a contact
module.exports.updateContact = async (event, _context) => {
const id = event.pathParameters.id;
const body = JSON.parse(event.body);
const paramName = body.paramName;
const paramValue = body.paramValue;
const params = {
Key: {
id: id
},
TableName: contactsTable,
ConditionExpression: 'attribute_exists(id)',
UpdateExpression: 'set ' + paramName + ' = :v',
ExpressionAttributeValues: {
':v': paramValue,
},
ReturnValues: 'ALL_NEW'
};
try {
const res = await db.update(params).promise();
}
catch (err){
console.log(err);
return err;
}
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify({
status: 'updated!',
[paramName]: paramValue,
}),
};
return response;
};
But it fails with status code 502 & 'InternalServerErrorException' message when called from Postman.
I tried to solve it with one of the suggestions found on stackoverflow with method override:
It doesn't work and I'm now getting status code 403 and 'MissingAuthenticationTokenException'.
I'm not sure what I'm doing wrong, seeking some guidance. Thank you.
It seems the API endpoint has an Authorizer configured on the API Gateway and the front end is sending the correct credentials, while the Postman configuration is not.
I would suggest to use the browser dev tools selecting the PUT request and copy it as cUrl:
then you can paste the copied cUrl command into the Postman request. It will prefill all the parameters: method, body and the headers.
hope this help

Returning HTML response without API gateway in AWS Lambda for Node template

I am trying to experiment with AWS Lambda and I am using Serverless CLI for my deployment. I am using aws-nodejs template to generate my project folder.
This is the handler.js code:
'use strict';
module.exports.hello = async (event, context) => {
return {
statusCode: 200,
body: {
"message":
'Hello World! Today is '+new Date().toDateString()
}
};
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};
I am getting a successful response in JSON format. I am trying to tweak it to return HTML response. Should I change the content-type for that? If so how?
I have gone through the below questions:
Get aws lambda response as an HTML page
Return HTML from AWS API Gateway
and a few others as well. But all of them are using the web console and the API gateway which I am not using.
You just need to add the content headers for html
return {
statusCode: 200,
headers: {
'Content-Type': 'text/html',
},
body: '<p>Look ma, its sending html now</p>',
}
Also, this is in one of the serverless examples in their github repo.
This works, try it. I have tested it with Lambda Function URL or with Lambda as Target for Application Load balancer
export const handler = async(event) => {
// TODO implement
const response = {
statusCode: 200,
body: '<h1>HTML from Lambda without API GW</h1>',
headers: {
'Content-Type': 'text/html',
}
};
return response;
};

AWS lambda api gateway error "Malformed Lambda proxy response"

I am trying to set up a hello world example with AWS lambda and serving it through api gateway. I clicked the "Create a Lambda Function", which set up the api gatway and selected the Blank Function option. I added the lambda function found on AWS gateway getting started guide:
exports.handler = function(event, context, callback) {
callback(null, {"Hello":"World"}); // SUCCESS with message
};
The issue is that when I make a GET request to it, it's returning back a 502 response { "message": "Internal server error" }. And the logs say "Execution failed due to configuration error: Malformed Lambda proxy response".
Usually, when you see Malformed Lambda proxy response, it means your response from your Lambda function doesn't match the format API Gateway is expecting, like this
{
"isBase64Encoded": true|false,
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"body": "..."
}
If you are not using Lambda proxy integration, you can login to API Gateway console and uncheck the Lambda proxy integration checkbox.
Also, if you are seeing intermittent Malformed Lambda proxy response, it might mean the request to your Lambda function has been throttled by Lambda, and you need to request a concurrent execution limit increase on the Lambda function.
If lambda is used as a proxy then the response format should be
{
"isBase64Encoded": true|false,
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"body": "..."
}
Note : The body should be stringified
Yeah so I think this is because you're not actually returning a proper http response there which is why you're getting the error.
personally I use a set of functions like so:
module.exports = {
success: (result) => {
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
"Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS
},
body: JSON.stringify(result),
}
},
internalServerError: (msg) => {
return {
statusCode: 500,
headers: {
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
"Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS
},
body: JSON.stringify({
statusCode: 500,
error: 'Internal Server Error',
internalError: JSON.stringify(msg),
}),
}
}
} // add more responses here.
Then you simply do:
var responder = require('responder')
// some code
callback(null, responder.success({ message: 'hello world'}))
For Python3:
import json
def lambda_handler(event, context):
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': json.dumps({
'success': True
}),
"isBase64Encoded": False
}
Note the body isn't required to be set, it can just be empty:
'body': ''
I had this issue, which originated from an invalid handler code which looks completely fine:
exports.handler = (event, context) => {
return {
isBase64Encoded: false,
body: JSON.stringify({ foo: "bar" }),
headers: {
'Access-Control-Allow-Origin': '*',
},
statusCode: 200,
};
}
I got the hint from examining the somewhat confusing API Gateway response logs:
> Endpoint response body before transformations: null
The way to fix it would be to either
Add the async keyword (async function implicitly returns a Promise):
exports.handler = async (event, context) => {
return {
isBase64Encoded: false,
body: JSON.stringify({ foo: "bar" }),
headers: {
'Access-Control-Allow-Origin': '*',
},
statusCode: 200,
};
}
Return a Promise:
exports.handler = (event, context) => {
return new Promise((resolve) => resolve({
isBase64Encoded: false,
body: JSON.stringify({ foo: "bar" }),
headers: {
'Access-Control-Allow-Origin': '*',
},
statusCode: 200,
}));
}
Use the callback:
exports.handler = (event, context, callback) => {
callback({
isBase64Encoded: false,
body: JSON.stringify({ foo: "bar" }),
headers: {
'Access-Control-Allow-Origin': '*',
},
statusCode: 200,
});
}
My handler was previously declared async without ever using await, so I removed the async keyword to reduce complexity of the code, without realizing that Lambda expects either using async/await/Promise or callback return method.
From the AWS docs
In a Lambda function in Node.js, To return a successful response, call
callback(null, {"statusCode": 200, "body": "results"}). To throw an
exception, call callback(new Error('internal server error')). For a
client-side error, e.g., a required parameter is missing, you can call
callback(null, {"statusCode": 400, "body": "Missing parameters of
..."}) to return the error without throwing an exception.
Just a piece of code for .net core and C# :
using Amazon.Lambda.APIGatewayEvents;
...
var response = new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.OK,
Body = JsonConvert.SerializeObject(new { msg = "Welcome to Belarus! :)" }),
Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
};
return response;
Response from lambda will be :
{"statusCode":200,"headers":{"Content-Type":"application/json"},"multiValueHeaders":null,"body":"{\"msg\":\"Welcome to Belarus! :)\"}","isBase64Encoded":false}
Response from api gateway will be :
{"msg":"Welcome to Belarus! :)"}
I've tried all of above suggestion but it doesn't work while body value is not String
return {
statusCode: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify({
success: true
}),
isBase64Encoded: false
};
A very very special case, if you pass the headers directly there is a chance you have this header:
"set-cookie": [ "........" ]
But Amazon needs this:
"set-cookie": "[ \\"........\\" ]"
For anyone else who struggles when the response appears valid. This does not work:
callback(null,JSON.stringify( {
isBase64Encoded: false,
statusCode: 200,
headers: { 'headerName': 'headerValue' },
body: 'hello world'
})
but this does:
callback(null,JSON.stringify( {
'isBase64Encoded': false,
'statusCode': 200,
'headers': { 'headerName': 'headerValue' },
'body': 'hello world'
})
Also, it appears that no extra keys are allowed to be present on the response object.
If you're using Go with https://github.com/aws/aws-lambda-go, you have to use events.APIGatewayProxyResponse.
func hello(ctx context.Context, event ImageEditorEvent) (events.APIGatewayProxyResponse, error) {
return events.APIGatewayProxyResponse{
IsBase64Encoded: false,
StatusCode: 200,
Headers: headers,
Body: body,
}, nil
}
I had this error because I accidentally removed the variable ServerlessExpressLambdaFunctionName from the CloudFormation AWS::Serverless::Api resource. The context here is https://github.com/awslabs/aws-serverless-express "Run serverless applications and REST APIs using your existing Node.js application framework, on top of AWS Lambda and Amazon API Gateway"
Most likely your returning body is in JSON format, but only STRING format is allowed for Lambda proxy integration with API Gateway.
So wrap your old response body with JSON.stringify().
In case the above doesn't work for anyone, I ran into this error despite setting the response variable correctly.
I was making a call to an RDS database in my function. It turned out that what was causing the problem was the security group rules (inbound) on that database.
You'll probably want to restrict the IP addresses that can access the API, but if you want to get it working quick / dirty to test out if that change fixes it you can set it to accept all like so (you can also set the range on the ports to accept all ports too, but I didn't do that in this example):
A common cause of the "Malformed Lambda proxy response" error is headers that are not {String: String, ...} key/values pairs.
Since set-cookie headers can and do appear in multiples, they are represented
in http.request.callback.response as the set-cookie key having an Array of
Strings value instead of a single String. While this works for developers, AWS
API Gateway doesn't understand it and throws a "Malformed Lambda proxy response"
error.
My solution is to do something like this:
function createHeaders(headers) {
const singleValueHeaders = {}
const multiValueHeaders = {}
Object.entries(headers).forEach(([key, value]) => {
const targetHeaders = Array.isArray(value) ? multiValueHeaders : singleValueHeaders
Object.assign(targetHeaders, { [key]: value })
})
return {
headers: singleValueHeaders,
multiValueHeaders,
}
}
var output = {
...{
"statusCode": response.statusCode,
"body": responseString
},
...createHeaders(response.headers)
}
Note that the ... above does not mean Yada Yada Yada. It's the ES6 spread operator.
Here's another approach. Configure the mapping template in your API gateway integration request and response. Go to IntegrationRequest -> MappingTemplate -> select "When there are no templates defined" -> type application/json for content-type. Then you don't have to explicitly send a json. Even the response you get at your client can be a plain string.
The format of your function response is the source of this error. For API Gateway to handle a Lambda function's response, the response must be JSON in this format:
{
"isBase64Encoded": true|false,
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"body": "..."
}
Here's an example function in Node.js with the response correctly formatted:
exports.handler = (event, context, callback) => {
var responseBody = {
"key3": "value3",
"key2": "value2",
"key1": "value1"
};
var response = {
"statusCode": 200,
"headers": {
"my_header": "my_value"
},
"body": JSON.stringify(responseBody),
"isBase64Encoded": false
};
callback(null, response);
};
Ref: https://aws.amazon.com/premiumsupport/knowledge-center/malformed-502-api-gateway/
Python 3.7
Before
{
"isBase64Encoded": False,
"statusCode": response.status_code,
"headers": {
"Content-Type": "application/json",
},
"body": response.json()
}
After
{
"isBase64Encoded": False,
"statusCode": response.status_code,
"headers": {
"Content-Type": "application/json",
},
"body": str(response.json()) //body must be of string type
}
If you're just new to AWS and just want your URL working,
If you haven't created a trigger for your Lambda Function, navigate to the function in Lambda Functions app and create trigger choosing API Gateway.
Navigate to API Gateway App -> Choose your Particular Lambda's API Gateway (Method execution) -> Click on INTEGRATION Request -> Uncheck "Use Lambda Proxy integration" (check box).
Then click on "<-Method Execution" & click on Test Client section. Provide the options and click test button. You should see a success response.
If you are still unable to get a success response, create an alias for the correct version (if you have multiple versions in the Lambda Function)
Pick the URL from the logs and use your POST/GET Tool (Postman) and choose authentication as AWS Signature - provide your authentication keys(AccessKey & SecretKey) in the postman request with AWS Region & Service Name as lambda.
P.S : This may only help beginners and may be irrelevant to others.

Resources