forked from serverless/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Art Cuaresma
authored and
Art Cuaresma
committed
Jun 7, 2021
1 parent
e0ebab6
commit e087792
Showing
8 changed files
with
36,928 additions
and
6,961 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as producer } from './kinesis/producer'; | ||
export { default as consumer } from './kinesis/consumer'; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const { KinesisStreamHandler, KinesisStreamRecordPayload } = require("aws-sdk"); | ||
|
||
const consumer: KinesisSreamHandler = async (event) => { | ||
try { | ||
for (const record of event.Records) { | ||
const payload: KinesisStreamRecordPayload = record.kinesis; | ||
const message: string = Buffer.from(payload.data, 'base64').toString(); | ||
|
||
console.log( | ||
`Kinesis Message: | ||
partition key: ${payload.partitionKey} | ||
sequence number: ${payload.sequenceNumber} | ||
kinesis schema version: ${payload.kinesisSchemaVersion} | ||
data: ${message} | ||
`); | ||
|
||
// Do something | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
|
||
export default consumer; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { APIGatewayProxyHandler } from 'aws-lambda'; | ||
import { Kinesis } from 'aws-sdk'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
const kinesis = new Kinesis({ | ||
apiVersion: '2013-12-02', | ||
}); | ||
|
||
const producer: APIGatewayProxyHandler = async (event) => { | ||
let statusCode: number = 200; | ||
let message: string; | ||
|
||
if (!event.body) { | ||
return { | ||
statusCode: 400, | ||
body: JSON.stringify({ | ||
message: 'No body was found', | ||
}), | ||
}; | ||
} | ||
|
||
const streamName: string = 'eventStream'; | ||
|
||
try { | ||
await kinesis.putRecord({ | ||
StreamName: streamName, | ||
PartitionKey: uuidv4(), | ||
Data: event.body, | ||
}).promise(); | ||
|
||
message = 'Message placed in the Event Stream!'; | ||
|
||
} catch (error) { | ||
console.log(error); | ||
message = error; | ||
statusCode = 500; | ||
} | ||
|
||
return { | ||
statusCode, | ||
body: JSON.stringify({ | ||
message, | ||
}), | ||
}; | ||
}; | ||
|
||
export default producer; |
Oops, something went wrong.