Im working on a project with AWS, and I've created an endpoint that I've connected to and SQS queue that then triggers a Lambda function. The Lambda should write data to a database but for some reason it doesn't. When I try to trigger it manually with a request like this
{
"Records": [
{
"messageId": "11d6ee51-4cc7-4302-9e22-7cd8afdaadf5",
"receiptHandle": "AQEBBX8nesZEXmkhsmZeyIE8iQAMig7qw...",
"body": {
"duration": "1230",
"player_1": "UR-da336be50ba9b8e53b8",
"player_2": "UR-a67322a021284404128",
"status_1": 1,
"status_2": 0
},
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1573251510774",
"SequenceNumber": "18849496460467696128",
"MessageGroupId": "1",
"SenderId": "AIDAIO23YVJENQZJOL4VO",
"MessageDeduplicationId": "1",
"ApproximateFirstReceiveTimestamp": "1573251510774"
},
"messageAttributes": {},
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:fifo.fifo",
"awsRegion": "us-east-2"
},
{
"messageId": "11d6ee51-4cc7-4302-9e22-7cd8afdaadf5",
"receiptHandle": "AQEBBX8nesZEXmkhsmZeyIE8iQAMig7qw...",
"body": {
"duration": "5510",
"player_1": "UR-da336be50ba9b8e53b8",
"player_2": "UR-a67322a021284404128",
"status_1": 1,
"status_2": 0
},
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1573251510774",
"SequenceNumber": "18849496460467696128",
"MessageGroupId": "1",
"SenderId": "AIDAIO23YVJENQZJOL4VO",
"MessageDeduplicationId": "1",
"ApproximateFirstReceiveTimestamp": "1573251510774"
},
"messageAttributes": {},
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:fifo.fifo",
"awsRegion": "us-east-2"
}
]
}
It works fine, but when it get called from SQS nothing happens and it deletes the message from SQS.
The code for my Lambda:
const { Client } = require("pg");
const client = new Client({
user: process.env.POSTGRES_USER,
host: process.env.POSTGRES_HOST,
database: process.env.POSTGRES_DATABASE,
password: process.env.POSTGRES_PASSWORD,
port: parseInt(process.env.POSTGRES_PORT),
});
client.connect();
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
exports.handler = async (event) => {
try {
await asyncForEach(event.Records, async (record) => {
//Writes the game info to the DB
const result = await client.query(
`INSERT INTO game_data (duration) VALUES (
${record.body.duration}
) RETURNING game_id`
);
const res = await Promise.all([
client.query(
`INSERT INTO player_game_data (user_id,game_id,player_game_status) VALUES (
'${record.body.player_1}',
'${result.rows[0].game_id}',
${record.body.status_1}
)`
),
client.query(
`INSERT INTO player_game_data (user_id,game_id,player_game_status) VALUES (
'${record.body.player_2}',
'${result.rows[0].game_id}',
${record.body.status_2}
)`
),
]);
}
);
return{
statusCode: 200}
} catch (error) {
return {
statusCode: 400,
error: error.message,
};
}
};
I've tested the queue and it works fine so the problem is probably somewhere here..
console.log(). See docs.aws.amazon.com/lambda/latest/dg/nodejs-logging.html - Parsifal