CRUD operations using DynamoDB with expressjs (node โ€‹โ€‹js)

I am trying to create a route that will perform some CRUD operations on DynamoDB. At a high level, it can be understood as:

  • The node js server application starts. (i.e. the command 'node server.js' is run)
  • The user uses the Chrome browserโ€™s POSTMAN to make route requests.
  • The user makes a GET request for http: // localhost: 8080 / listtablesofdynamodb '.
  • The specific route associated with this URL, which should perform certain dynamodb activity, is determined. (e.g. connecting to dynamodb, fetching table names and displaying them in the callback method.)

the reason I'm asking this question is because I could not find a suitable tutorial on how to do dynamodb work using js node expressions. All I could find were console applications on the aws website, which I thought werenโ€™t useful to me. Any help is much appreciated.

+5
source share
2 answers

Fortunately, I managed to use aws-sdk in my route. The solution consists of two stages:

  • Run the code in the AWS account of the EC2 instance and attach an IAM role that allows the EC2 instance to talk to dynamodb. (In this case, you do not need an access key difficult to code into code) see the article .
  • may refer to the code below for source code forests.

`

var express = require('express'); var router = express.Router(); var AWS = require("aws-sdk"); AWS.config.update({ region: "us-west-2", endpoint: "dynamodb endpoint specific to your aws account" }); var dynamodb = new AWS.DynamoDB(); var params = { ExclusiveStartTableName: "stringvalue", Limit: 10 }; /* GET users listing. */ router.get('/', function (req, res) { console.log("entered into dynadb route"); dynamodb.listTables(params, function (err, data) { if (err) console.log(err, err.stack); // an error occurred else { res.send(data); } }); }); module.exports = router; 

`

0
source

Passkey required

All you need for d is also to make a DynamoDB object to connect

 var ddb = require('dynamodb').ddb({ accessKeyId: '< your_access_key_id >', secretAccessKey: '< your_secret_access_key >' }); 

put this under your statements of need, turn on the server. Then you can simply fill in the routes to perform CRUD operations.

For testing use

 ddb.listTables({}, function(err, res) {console.log(res);}); 

All tables in your db will be listed here.

for full source check here

Good luck

+2
source

Source: https://habr.com/ru/post/1246760/


All Articles