Skip to content

Instantly share code, notes, and snippets.

@Pelirrojo
Created October 16, 2018 10:44
Show Gist options
  • Save Pelirrojo/ad14f875be9e564ea8c24b12053cd031 to your computer and use it in GitHub Desktop.
Save Pelirrojo/ad14f875be9e564ea8c24b12053cd031 to your computer and use it in GitHub Desktop.
NodeJS Local example with DynamoDB/Docker

NodeJS Local Examples with DynamoDB/Docker

Some samples to test DynamoDB locally through Docker

  # Download & Run LocalStack
  $ docker pull localstack/localstack:latest
  $ docker run -it -p 4567-4578:4567-4578 -p 8080:8080 localstack/localstack
  
  # Add some fake credentials locally
  $ vi ~/.aws/credentials

  # Data to include >>>>>>>>>>>>>>>>>>>>>>>>>>>>
  [fake]
  region = eu-west-1
  aws_access_key_id = **NOT_REAL**
  aws_secret_access_key = **FAKE_UNUSED_CREDS**
  # Data to include <<<<<<<<<<<<<<<<<<<<<<<<<<<<

  npm i aws-sdk
  node nodejs-dynamodb-create-table-local.js
  node nodejs-dynamodb-populate-table-local.js
  node nodejs-dynamodb-read-table-local.js

And you can also view the new dynamoDB resource created at local dashboard

/**
* Inspired on: https://github.com/gkatzioura/egkatzioura.wordpress.com/tree/master/DynamoDBTutorialNode
*/
const AWS = require("aws-sdk")
// URI and other properties could be load by ENV Vars or by property file (.env)
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:4569"
})
const dynamodb = new AWS.DynamoDB()
const params = {
TableName : "Users",
KeySchema: [{ AttributeName: "email", KeyType: "HASH"}],
AttributeDefinitions: [{ AttributeName: "email", AttributeType: "S" }],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
}
}
dynamodb.createTable(params, console.log)
/**
* Inspired on: https://github.com/gkatzioura/egkatzioura.wordpress.com/tree/master/DynamoDBTutorialNode
*/
const AWS = require("aws-sdk")
// URI and other properties could be load by ENV Vars or by property file (.env)
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:4569"
})
const dynamodb = new AWS.DynamoDB()
var params = {
TableName:"Users",
Item: {
email : { S:"[email protected]"},
fullname: { S:"Jon Doe" },
role: { S:"Super Heroe" }
}
};
dynamodb.putItem(params,console.log)
/**
* Inspired on: https://github.com/gkatzioura/egkatzioura.wordpress.com/tree/master/DynamoDBTutorialNode
*/
const AWS = require("aws-sdk")
// URI and other properties could be load by ENV Vars or by property file (.env)
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:4569"
})
const docClient = new AWS.DynamoDB.DocumentClient()
const email = process.env.EMAIL || '[email protected]'
const params = {
TableName: "Users",
KeyConditionExpression: "#email = :email",
ExpressionAttributeNames:{
"#email": "email"
},
ExpressionAttributeValues: {
":email":email
}
}
docClient.query(params,console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment