mhart/aws4

Need Insert data into DynamoDB

Javeed-JargonHandlers opened this issue · 4 comments

I am trying to insert into DynamoDB table. I have tried much.

Any Example or Looking for the suggestion.

Thanks in advance.

mhart commented

What errors are you seeing? Can you show me the code you're using?

Hi @mhart ,

Looks like i achieved something here please confirm the code

`var params = '{"TableName": "testtable","Item": {"fuel": { "S": "mys-part-key" },"value1": {"S": "shire"},"vallue2": {"S": "fahmitha"}}}';

let request = {
host: 'dynamodb.us-east-1.amazonaws.com',
service: 'dynamodb',
region: process.env.REGION,
method: 'POST',
path: '/',
headers: {
'Content-Type': 'application/x-amz-json-1.0',
'X-Amz-Target': 'DynamoDB_20120810.PutItem'
},
body: params
};

let signedRequest = aws4.sign(request,
{
// assumes user has authenticated and we have called
// AWS.config.credentials.get to retrieve keys and
// session tokens
secretAccessKey: process.env.SECRET_ACCESS_KEY,
accessKeyId: process.env.ACCESS_KEY_ID,
// sessionToken: AWS.config.credentials.sessionToken
})

delete signedRequest.headers['Host']
delete signedRequest.headers['Content-Length']

console.log(signedRequest)

await fetch(https://dynamodb.us-east-1.amazonaws.com, signedRequest)
.then()
.then(response => {
console.log(response);
});`

mhart commented

What errors are you seeing?

Looks Like it is working fine now, Thank you @mhart