SQS send message error using #aws-sdk node js at AWS lambda - aws-lambda

I am trying to send message from my AWS Lambda to AWS SQS but it isn't quiet working and throwing me the error.
2022-12-26T14:58:31.651Z 282ada00-ea4a-45b6-afe4-e3a7f16e8c5a INFO MissingParameter: The request must contain the parameter Label.
at throwDefaultError (/var/task/node_modules/#aws-sdk/smithy-client/dist-cjs/default-error-handler.js:8:22)
at deserializeAws_queryAddPermissionCommandError (/var/task/node_modules/#aws-sdk/client-sqs/dist-cjs/protocols/Aws_query.js:292:51)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async /var/task/node_modules/#aws-sdk/middleware-serde/dist-cjs/deserializerMiddleware.js:7:24
at async /var/task/node_modules/#aws-sdk/middleware-signing/dist-cjs/middleware.js:14:20
at async /var/task/node_modules/#aws-sdk/middleware-retry/dist-cjs/retryMiddleware.js:27:46
at async /var/task/node_modules/#aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:5:22
at async sendToSQS (/var/task/sendToSqs.js:28:20)
at async exports.handler (/var/task/index.js:19:22) {
'$fault': 'client',
'$metadata': {
httpStatusCode: 400,
requestId: 'bf137e9a-24bc-52bd-9416-22b99c6b82f5',
extendedRequestId: undefined,
cfId: undefined,
attempts: 1,
totalRetryDelay: 0
},
Type: 'Sender',
Code: 'MissingParameter',
Detail: ''
}
I am not sure what parameters and in which way I need to include to make it work.
This is my code for sending message, where from my main module I send a simple data value as part of my message to be sent to SQS.
const { SQSClient, AddPermissionCommand } = require("#aws-sdk/client-sqs");
const sendToSQS=async(data)=>{
const client = new SQSClient({ region: "eu-west-1" });
var params = {
DelaySeconds: 0,
MessageAttributes: {
"Author": {
DataType: "String",
StringValue: "event params"
},
},
MessageGroupId:`${data}`,
MessageBody:JSON.stringify(data),
QueueUrl:"https://sqs.eu-west-1.amazonaws.com/000011110000/Salesforce-cqd-orders-delayer-retry"
};
try{
const command = new AddPermissionCommand(params);
let queueRes = await client.send(command);
console.info("[LAMBDA/#sqs] retry mechanism has succeeded. Data sent to SQS successfully")
console.log(queueRes)
const response = {
statusCode: 200,
body: "Data sent from lambda to sqs successfully.",
};
return response
}catch(error){
console.error("[LAMBDA/#s] retry mechanism has failed. Data wasn't sent to SQS")
console.log(error)
const response = {
statusCode: 200,
body: "Lambda to SQS error",
};
return response;
}
}
module.exports={sendToSQS}

Your message has delaySeconds which is not required and MessageGroupId which is only required for FIFO queue.
You can check sendMessage code reference from here AWS Wiki
Also, check this API reference for Send Message

Related

NodeJS: AWS SDK V3: Not receiving any response data from lambda function

I'm trying to use the v3 javascript sdk to invoke a AWS Lambda function, and I'm having problems getting any meaningful response.
My code looks like so...
const { Lambda } = require("#aws-sdk/client-lambda");
const client = new Lambda();
const params = {
FunctionName: "MyLamdaFuncton",
Payload: JSON.stringify({ "action": "do_something" }),
InvocationType: "Event"
};
client.invoke(params)
.then((response) => {
console.log(JSON.stringify(response,null,4));
})
.catch((err) => {
console.error(err);
})
I can confirm from checking the CloudWatch logs that the lambda function works as exepcted. However this is the response I get in my NodeJS code...
{
"$metadata": {
"httpStatusCode": 202,
"requestId": "d6ba189d-9156-4f01-bd51-efe34a66fe34",
"attempts": 1,
"totalRetryDelay": 0
},
"Payload": {}
}
How do I get the actual response and status from the Lambda function?
If I change the payload above to intentionally throw an exception in my Lambda, the response in the console is still exactly the same.
update:
The Lambda function is written in Ruby. The response is returned like so...
{ statusCode: 200, body: JSON.generate(response.success?) }
where "response" is from another service it calls internally.
I've figured out what I was doing wrong. The issue was the "InvocationType". I got it working by changing to...
InvocationType: "RequestResponse"
Then I had to extract the response data like so...
const response_data = JSON.parse(new TextDecoder("utf-8").decode(response.Payload))

AWS lambda api gateway error “Malformed Lambda proxy response”

My AWS ApiGateway invokes this lambda function :
const handler = (event, context) => {
const theResponse = {
"statusCode": 200,
"isBase64Encoded": false,
"headers": {
"Content-Type": "application/json"
},
"body": "hello, world"
};
switch (event.httpMethod) {
case 'GET':
theResponse["body"] = `method ${event.httpMethod} detected`;
break;
default:
theResponse["statusCode"] = 404;
theResponse["body"] = `Unsupported method ${event.httpMethod} detected`;
break;
}
return theResponse;
};
module.exports = {
handler,
};
Any thoughts to why this errors with :
{"message": "Internal server error"}
Received response. Status: 200, Integration latency: 344 ms
Endpoint response body before transformations: null
Execution failed due to configuration error: Malformed Lambda proxy response
Method completed with status: 502
I tried replacing return (theResponse); with return JSON.stringify(theResponse); but this also returns the same error.
However if I replace return theResponse; with return {"statusCode":200, "body":"hello, world"}; then the API executes without errors.
Looks to me like a lamda_proxy integration issue but I can't see why. thanks.
Bearrider!
I guess it is related to the way you are building your response object:
Try stringify only the body attribute:
case 'GET':
theResponse["body"] = JSON.stringify(`method ${event.httpMethod} detected`);
break;
default:
theResponse["statusCode"] = 404;
theResponse["body"] = JSON.stringify(`Unsupported method ${event.httpMethod} detected`);
break;
}

"[Network] undefined" when trying to use subscriber - URQL

I am trying to set up a subscriber to log some output on the creation of a new Message.
Currently using Urql, with ApolloServerExpress on the backend.
I am receiving an error from the useSubscription method which I am logging to the console :
message: "[Network] undefined"
name: "CombinedError"
I know for sure my backend is working as I can subscribe using the Graphiql playground just fine.
As far as front end goes, I have followed almost exactly as the example in the Urql docs.
WS Client:
const wsClient = createWSClient({
url: "ws://localhost:4000/graphql",
});
Subscriber Exchange:
subscriptionExchange({
forwardSubscription(operation) {
return {
subscribe: (sink) => {
const dispose = wsClient.subscribe(operation, sink);
return {
unsubscribe: dispose,
};
},
};
},
}),
MessageList Component:
const newMessages = `
subscription Messages {
newMessage {
content
status
sender {
id
email
}
recipient {
id
email
}
}
}
`;
...
const handleSub = (messages: any, newMessage: any) => {
console.log("Messages: ", messages);
console.log("newMessages: ", newMessage);
};
const [res] = useSubscription({ query: newMessages }, handleSub);
console.log("Res: ", res);
I was getting the same error when using subscriptions with urql. In my case, I was able to do console.log(error.networkError); which gave a much more helpful error message than [Network] undefined.
You can read more about errors in urql here.
The error I got from error.networkError was:
Event {
"code": 4400,
"isTrusted": false,
"reason": "{\"server_error_msg\":\"4400: Connection initialization failed: Missing 'Authorization' or 'Cookie' header in JWT authenticati",
}
I was able to fix it by adding authentication to my subscription exchange setup. Here's the code I'm using now:
const wsClient = createWSClient({
url: "wss://your-api-url/graphql",
connectionParams: async () => {
// Change this line to however you get your auth token
const token = await getTokenFromStorage();
return {
headers: {
Authorization: `Bearer ${token}`,
},
};
},
});
Ended up chalking graphql-ws and switched over to subscriptions-transport-ws.
Fixed my issues.

Returning custom http status codes on AWS Lambda error

I'm working with an AWS Lambda with serverless framework and I want to return a custom http status code when an error occurs but I'm always getting a 502 status code when I call my endpoint with axios.
module.exports.handler = async (event, context, callback) => {
try {
// some stuff
} catch (err) {
// error here
let myErrorObj = {
errorType : "InternalServerError",
httpStatus : 500,
requestId : context.awsRequestId,
trace : {
"function": "abc()",
"line": 123,
"file": "abc.js"
},
body: err
}
callback(JSON.stringify(myErrorObj));
}
}
But the object I'm getting back contains the property status: 502 and data.message: "Internal server error"
Any ideas of what is going on here?
status code 502 indicates the lambda's response to API Gateway is in an incorrect format.
The correct response for an async function (if not integration method stated in serverless YAML file, it would use Lambda Proxy Integration):
export const dummyFunction = async (event, context, callback) =>
{
// ... logic
return {
statusCode: 500,
body: JSON.stringify({...data}),
}
};
Callback is only for non-async function only. Please refer to full documentation.
Are you using API Gateway with your Lambda? If so, what you are returning in the callback is incorrect. The object you return must match this format: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format
So you can either just callback(err) and let AWS generate a 500 or if you want the added error context something like:
let myErrorObj = {
statusCode : 500,
body: JSON.stringify({
requestId : context.awsRequestId,
trace : {
"function": "abc()",
"line": 123,
"file": "abc.js"
}
error: err
}
})
}
callback(null, myErrorObj);
If you want requestId and trace attributes you would need to add them into the body.

AWS lambda function failing in Amazon Connect

I have written an AWS lambda function in node.js to send an email, which is invoked in an Amazon Connect contact flow. In the error branch it plays a prompt saying "lambda function failed." I verified the IAM role has permission to send email with SES, the sender/receiver emails are verified in SES, and also the lambda function has permissions for Amazon Connect.
The email does actually get sent out, but oddly I still hear the prompt "lambda function failed." Here is the code:
"use strict";
const aws = require('aws-sdk');
const sender = "Sender Name <sender#email.com>";
const recipient = "recipient#email.com";
const subject = "ALERT: no agents are logged in";
const body_text = "There are no agents logged in";
const body_html =
`<html>
<head></head>
<body>
<h1>ALERT</h1>
<p>Ther are no agents logged in to take calls in the queue.</p>
</body>
</html>`;
const charset = "UTF-8";
let params = {
Source: sender,
Destination: {
ToAddresses: [
recipient
],
},
Message: {
Subject: {
Data: subject,
Charset: charset
},
Body: {
Text: {
Data: body_text,
Charset: charset
},
Html: {
Data: body_html,
Charset: charset
}
}
},
};
const ses = new aws.SES({apiVersion: '2010-12-01'});
exports.handler = function(event, context, callback) {
ses.sendEmail(params, function(err, data) {
if(err) {
console.log("fail");
callback(err, err.message);
}
else {
console.log("success");
callback(null);
}
});
};
I checked the cloudwatch logs and don't see any error:
00:17:24
START RequestId: 17f1e239-990e-11e8-96bb-a1980f44db91 Version: $LATEST
00:17:24
2018-08-06T00:17:24.723Z 17f1e239-990e-11e8-96bb-a1980f44db91 success
00:17:24
END RequestId: 17f1e239-990e-11e8-96bb-a1980f44db91
00:17:24
REPORT RequestId: 17f1e239-990e-11e8-96bb-a1980f44db91 Duration: 226.51 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 32 MB
How do I troubleshoot this?
EDIT:
I enabled contact flow logs. In CloudWatch I noticed this:
{
"Parameters": { "FunctionArn": "arn:aws:lambda:us-west-2:769182588423:function:noAgentEmail", "TimeLimit": "8000" },
"Timestamp": "2018-08-06T06:36:31.786Z",
"ContactFlowModuleType": "InvokeExternalResource",
"Results": "The Lambda Function Returned An Error.",
"ExternalResults": { "forceClose": "false" },
"ContactId": "458027b0-d895-439e-bc06-114500dce64a",
"ContactFlowId": "arn:aws:connect:us-west-2:769182588423:instance/1e2ddedd-8335-42fe-89de-1e986fc016ef/contact-flow/2329af39-682c-4dc8-b3a2-5e7fe64de5d2"
}
What's confusing is it indicates the lambda function returned something:
"ExternalResults": { "forceClose": "false" }
But this is clearly not the case given the code. What's going on?
There's a couple of things to ensure your working Lambda function can be used for Connect.
As mentioned in the above answer, the return value should be a flat JSON object. As the guide says:
Nested and complex objects are not supported.
So, it should look something like this when you execute the lambda directly:
Response:
{
"statusPop": "success",
"id": "1",
"name": "me",
}
The property names and values need to be
alphanumeric, dash, and underscore characters.
Oh, and here's what tripped me up:
Amazon Connect sends paramters as part of a hierarchical structure, so any parameters you're expecting need to be referenced as
event['Details']['Parameters']['statusPop'];
instead of
event['statusPop'];
And don't forget to add permissions using:
aws lambda add-permission --function-name function:my-lambda-function --statement-id 1 \
--principal connect.amazonaws.com --action lambda:InvokeFunction --source-account 123456789012 \
--source-arn arn:aws:connect:us-east-1:123456789012:instance/def1a4fc-ac9d-11e6-b582-06a0be38cccf
More info and in depth details of what I mentioned here: https://docs.aws.amazon.com/connect/latest/adminguide/connect-lambda-functions.html
Your lambda needs to return a flat filed JSON object. It looks like on success your lambda callbacks with null.
Update your exports to this:
exports.handler = function(event, context, callback) {
ses.sendEmail(params, function(err, data) {
if(err) {
console.log("fail");
//callback(err, err.message);
callback(null, {"status":"error"});
}
else {
console.log("success");
callback(null, {"status":"success"});
}
});
});
More information on lambda/connect requirements here: https://docs.aws.amazon.com/connect/latest/adminguide/connect-lambda-functions.html
Note:
The output returned from the function must be a flat object of key/value pairs, with values that include only alphanumeric, dash, and underscore characters. Nested and complex objects are not supported. The size of the returned data must be less than 32 Kb of UTF-8 data.
you must return a valid JSON object such as below example
export const handler = async(event) => {
console.log("event" + JSON.stringify(event));
let amount = event['Details']['Parameters']['amount'];
console.log("amount:" + amount);
let resultMap = {
"amountValid": "valid"
};
console.log("resultMap" + JSON.stringify(resultMap));
return resultMap;
};

Resources