Skip to content

Instantly share code, notes, and snippets.

Created April 29, 2022 04:31
What would you like to do?
Importing CSV to DynamoDB table
const fs = require('fs')
const parse = require('csv-parse/lib/sync')
const AWS = require('aws-sdk')
var credentials = new AWS.SharedIniFileCredentials({ profile: '<aws-profile>' });
region: 'ap-southeast-1',
credentials: credentials
const docClient = new AWS.DynamoDB.DocumentClient()
const contents = fs.readFileSync('./selected.csv', 'utf-8')
// If you made an export of a DynamoDB table you need to remove (S) etc from header
const data = parse(contents, { columns: true })
data.forEach((item) => {
if (!item.maybeempty) delete item.maybeempty // need to remove empty items
item.version = parseInt(1) // added this since my table has sort key
TableName: '<table-name>',
Item: item
}, (err, res) => {
if (err) console.log(err)
console.log(`Inserting ${}`)
"name": "dydbimport",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"aws-sdk": "^2.153.0",
"csv-parse": "^2.0.0"
Copy link

Install Package using:

npm install

Run the app using

node index.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment