Last active
          June 23, 2021 17:30 
        
      - 
      
- 
        Save skolhustick/a03db23eca1b19bed64ec964738c9967 to your computer and use it in GitHub Desktop. 
    Add data to AWS Kinesis Data Stream with Node.js
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const AWS = require('aws-sdk') | |
| require('dotenv').config() | |
| // Configure AWS SDK with the credentials created before | |
| // You should probably use a .env file for this | |
| AWS.config.update({ | |
| region: process.env.AWS_REGION, | |
| accessKeyId: process.env.AWS_ACCESS_KEY_ID, | |
| secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY | |
| }) | |
| // Create a kinesis client | |
| const kinesisClient = new AWS.Kinesis() | |
| // Let's generate some random data | |
| // Usually you will get this from a request body on a POST route | |
| const exampleData = { | |
| voterid: Math.round(Math.random() * 100000), | |
| choice: Math.random() * 10 > 5 ? 'blue' : 'red' | |
| } | |
| // The stream name for the data stream we created | |
| const KINESIS_STREAM_NAME = 'voting-app' | |
| // Call the putRecord to send the data to the stream | |
| kinesisClient.putRecord( | |
| { | |
| Data: JSON.stringify(exampleData), | |
| StreamName: KINESIS_STREAM_NAME, | |
| PartitionKey: '1' | |
| }, | |
| (err, data) => { | |
| if (err) { | |
| throw err | |
| } | |
| console.log(data) | |
| } | |
| ) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment