Interesting issue after updating to v3. The updated code works fine in tests, locally and in lambdas, but fails when running it within a Synthetics Canary. Might be an easy solution, but haven't found it yet :/ Using the new v3 style with send() and GetItemCommand makes no difference.
Edit: With fails, is that the getItem()-call doesn't return the DynamoDB item. As seen from the logs, metadata is returned, but the actual Item is missing.
The only obvious thing I've noticed is that v2 makes https calls, while v3 makes http calls. I tried changing the endpoint and tls within the client options, but doesn't seem to make a difference either.
Old v2
'use strict';
const DynamoDBClient = require('aws-sdk/clients/dynamodb');
const dynamodb = new DynamoDBClient({ apiVersion: '2012-08-10' });
async function getAttributeByKey(table, keyName, keyValue, attributeName) {
const params = {
TableName: table,
Key: {
[keyName]: { S: keyValue },
},
};
const {
Item: { [attributeName]: { S: attributeValue } = {} } = {},
} = await dynamodb.getItem(params).promise();
return attributeValue;
}
Canary logs:
2024-02-26T07:45:30.926Z INFO: {"TableName":"<table>","Key":{"<key>":{"S":"<value>"}}}
2024-02-26T07:45:30.928Z INFO: Request: https://dynamodb.eu-central-1.amazonaws.com/
2024-02-26T07:45:30.954Z INFO: Response: 200 OK Request: https://dynamodb.eu-central-1.amazonaws.com/
2024-02-26T07:45:30.955Z INFO: {"Item":{"<key>":{"S":"<value>"},"modified":{"S":"2024-02-21T16:39:48.336Z"},"<attribute>":{"S":"<value>"}}}
New v3:
'use strict';
const { DynamoDB } = require('@aws-sdk/client-dynamodb');
const dynamodb = new DynamoDB({ apiVersion: '2012-08-10' });
async function getAttributeByKey(table, keyName, keyValue, attributeName) {
const params = {
TableName: table,
Key: {
[keyName]: { S: keyValue },
},
};
const {
Item: { [attributeName]: { S: attributeValue } = {} } = {},
} = await dynamodb.getItem(params);
return attributeValue;
}
Canary logs:
2024-02-26T07:52:39.813Z INFO: {"TableName":"<table>","Key":{"<key>":{"S":"<value>"}}}
2024-02-26T07:52:39.815Z INFO: Request: http://dynamodb.eu-central-1.amazonaws.com/
2024-02-26T07:52:39.825Z INFO: Response: 200 OK Request: http://dynamodb.eu-central-1.amazonaws.com/
2024-02-26T07:52:39.826Z INFO: {"$metadata":{"httpStatusCode":200,"requestId":"<...>","attempts":1,"totalRetryDelay":0}}
I have no issue with the code. I would remove the mapping you have on the Item attribute, and just try to print the entire response.
Also ensure that an item exists with the specific key you are passing in.
My item
My code
My Result