- Le plus récent
- Le plus de votes
- La plupart des commentaires
You should await the response, as you don't, your Lambda container is getting shutdown too soon:
import {DynamoDB, PutItemCommand} from "@aws-sdk/client-dynamodb";
const DDB = new DynamoDB({region: 'us-east-1'});
export const handler = async(event) => {
console.log("entered handler function");
let params = {};
params.ReturnConsumedCapacity = "TOTAL";
params.TableName = "weather";
params.Item = {
"sc": {
"S": "Sacramento"
},
"t": {
"N": "78"
}
};
console.log(params);
try {
// Added await to the DDB promise returned.
const response = await DDB.send(new PutItemCommand(params));
console.log("Success", response)
} catch (err) {
console.log("Error", err);
}
/*
setTimeout(function(){
callback(null, "ok");
}, 1000 * 10);
*/
};
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
Hi, following page will explain you how to use promises with DDB in order to manage its asynchronous responses.
https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-promises.html
So, if you properly process the returned promise, you should be able to see your item after it gets written
I've attached the code I'm currently working on below. I commented out some code I am playing around with that I can get to successfully write to the DynamoDB table 'weather'. The code is below the ``// USING DynamoDBCLient, DynamoDBDocumentClient, and PutCommand`
I don't understand why I am getting the ** "errorType": "Runtime.UserCodeSyntaxError", "errorMessage": "SyntaxError: Unexpected reserved word",** error message after adding the await keyword in front of the DDB.send function call in the second section of the code below the ``// USING DynamoDB and PutItemCommand This code reads items from a csv file and uses a try/catch block to call the DDB.send function to write to the DynamoDB table 'weather'
UPDATE: Fixed the SyntaxError by adding async keyword to innermost function. I also rewrote the code in ES6 arrow function format. However, the original problem comes back in which the items don't get written to the DynamoDB table. I know I've done something wrong, but I've looked at it for hours and still no luck. See updated code below.
// es6 syntax for including modules
import * as fs from 'fs';
// es6 syntax for including modules
import {DynamoDB, PutItemCommand, DynamoDBClient} from "@aws-sdk/client-dynamodb";
import { PutCommand, DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
export const handler = async(event) => {
console.log("entered handler function");
//
// USING DynamoDBCLient, DynamoDBDocumentClient, and PutCommand
//
/* const DDB = new DynamoDBClient({region: 'us-east-1'});
const docClient = DynamoDBDocumentClient.from(DDB);
let params = {};
const command = new PutCommand({
"TableName": 'weather',
"ReturnConsumedCapacity": "TOTAL",
"Item": {
// "sc": {"S": "Sacramento" } ,
"sc": "Sac",
"t" : "74"
}
});
const response = await docClient.send(command);
console.log(response);
return(response);*/
//
// USING DynamoDB and PutItemCommand
//
const DDB = new DynamoDB({region: 'us-east-1'});
let params = {};
const fileContents = fs.readFileSync("cities.csv", "utf8").split('\n').map(function(item_str){
params.ReturnConsumedCapacity = "TOTAL";
params.TableName = "weather";
params.Item = {
"sc": {
"S": item_str.split(",")[0]
},
"t": {
"N": String(item_str.split(",")[1])
}
};
console.log(params);
try {
const responses = await DDB.send(new PutItemCommand(params));
console.log("Success", responses)
} catch (err) {
console.log("Error", err);
}
});
/*
setTimeout(function(){
callback(null, "ok");
}, 1000 * 10);
*/
};
UPDATED CODE
// es6 syntax for including modules
import * as fs from 'fs';
// es6 syntax for including modules
import {DynamoDB, PutItemCommand, DynamoDBClient} from "@aws-sdk/client-dynamodb";
import { PutCommand, DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
export const handler = async(event, callback) => {
console.log("entered handler function");
const DDB = new DynamoDBClient({region: 'us-east-1'});
const docClient = DynamoDBDocumentClient.from(DDB);
let params = {};
const fileContents = fs.readFileSync("cities.csv", "utf8").split('\n').map(async (item_str) => {
params.ReturnConsumedCapacity = "TOTAL";
params.TableName = "weather";
params.Item = {
"sc": {
"S": item_str.split(",")[0]
},
"t": {
"N": String(item_str.split(",")[1])
}
};
console.log(params);
try {
const responses = await docClient.send(new PutCommand(params));
console.log(responses);
return(responses);
} catch (err) {
console.log("Error", err);
} finally {
console.log("Success");
}
});
setTimeout(function(){
callback(null, "ok");
}, 1000 * 10);
};
Execution Log
Test Event Name
seedDYnamoDB
Response
null
Function Logs
'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'CATHEDRAL CITY' }, t: { N: '39' } }
}
2023-06-06T20:43:41.064Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'ELKHART' }, t: { N: '45' } }
}
2023-06-06T20:43:41.064Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'FONTANA' }, t: { N: '31' } }
}
2023-06-06T20:43:41.065Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'LINCOLN' }, t: { N: '57' } }
}
2023-06-06T20:43:41.065Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'MACON' }, t: { N: '38' } }
}
2023-06-06T20:43:41.121Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'MARYSVILLE' }, t: { N: '73' } }
}
2023-06-06T20:43:41.121Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'MEMPHIS' }, t: { N: '44' } }
}
2023-06-06T20:43:41.122Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'MESA' }, t: { N: '66' } }
}
2023-06-06T20:43:41.122Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'MYRTLE BEACH' }, t: { N: '44' } }
}
2023-06-06T20:43:41.122Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'NAPERVILLE' }, t: { N: '61' } }
}
2023-06-06T20:43:41.122Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'PANAMA CITY' }, t: { N: '28' } }
}
2023-06-06T20:43:41.122Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'PATERSON' }, t: { N: '77' } }
}
2023-06-06T20:43:41.123Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'POMPANO BEACH' }, t: { N: '38' } }
}
2023-06-06T20:43:41.123Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'SABANA' }, t: { N: '67' } }
}
2023-06-06T20:43:41.123Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'SAN BUENAVENTURA' }, t: { N: '50' } }
}
2023-06-06T20:43:41.123Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'SANTA ROSA' }, t: { N: '41' } }
}
2023-06-06T20:43:41.123Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'SARASOTA' }, t: { N: '43' } }
}
2023-06-06T20:43:41.241Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'SIOUX CITY' }, t: { N: '60' } }
}
2023-06-06T20:43:41.261Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'SOUTH BEND' }, t: { N: '52' } }
}
2023-06-06T20:43:41.301Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'SPRINGDALE' }, t: { N: '41' } }
}
2023-06-06T20:43:41.302Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'ST. PETERSBURG' }, t: { N: '44' } }
}
2023-06-06T20:43:41.302Z 5ed80d11-17a2-4300-a11e-4a41afebd8a1 INFO {
ReturnConsumedCapacity: 'TOTAL',
TableName: 'weather',
Item: { sc: { S: 'WEST COVINA' }, t: { N: '69' } }
}
END RequestId: 5ed80d11-17a2-4300-a11e-4a41afebd8a1
REPORT RequestId: 5ed80d11-17a2-4300-a11e-4a41afebd8a1 Duration: 2556.87 ms Billed Duration: 2557 ms Memory Size: 128 MB Max Memory Used: 35 MB
Request ID
5ed80d11-17a2-4300-a11e-4a41afebd8a1
Adding an await Promise.all( in front of the fs.readFileSync function call seemed to do the trick.
Contenus pertinents
- demandé il y a un an
- demandé il y a un an
- demandé il y a un an
- AWS OFFICIELA mis à jour il y a 2 ans
- AWS OFFICIELA mis à jour il y a 10 mois
- AWS OFFICIELA mis à jour il y a 8 mois
I get the following error after adding the await keyword.
`Test Event Name seedDYnamoDB
Response { "errorType": "Runtime.UserCodeSyntaxError", "errorMessage": "SyntaxError: Unexpected reserved word", "trace": [ "Runtime.UserCodeSyntaxError: SyntaxError: Unexpected reserved word", " at _loadUserApp (file:///var/runtime/index.mjs:994:17)", " at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1032:21)", " at async start (file:///var/runtime/index.mjs:1195:23)", " at async file:///var/runtime/index.mjs:1201:1" ] }`
Can you share the code you are running, i'm guessing its different to what you previously shared?
I just shared the updated code below.