I am having a hard time with using a dynamodb stream to send data to the client via websockets. I have streams working and i have websockets working separetly, but not together.
This is my yml stack. Please show me how you’d craft a NODEjs runtime function to receive a stream event, then send a websocket connection to the clients.
service: itsm-stream-demo3
provider:
name: aws
runtime: nodejs10.x
environment:
websocketIDTable:
Ref: websocketIDTableNew1
demoTable:
Ref: demoTableNew1
iamRoleStatements:
- Effect: Allow
Action:
- “dynamodb:PutItem”
- “dynamodb:GetItem”
- “dynamodb:DeleteItem”
- “dynamodb:Scan”
Resource:
- Fn::GetAtt: [websocketIDTableNew1, Arn]
- Effect: Allow
Action:
- “execute-api:ManageConnections”
Resource:
- “arn:aws:execute-api:::**/@connections/*”
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
- dynamodb:DescribeStream
- dynamodb:GetRecords
- dynamodb:GetShardIterator
- dynamodb:ListStreams
Resource:
- Fn::GetAtt: [demoTableNew1, Arn]
functions:
connectionHandler:
handler: handler/handler.connectionHandler
events:
- websocket:
route: $connect
- websocket:
route: $disconnect
defaultHandler:
handler: handler/handler.defaultHandler
events:
- websocket:
route: $default
sendMessageHandler:
handler: handler/handler.sendMessageHandler
events:
- websocket:
route: sendMessage
getStream:
handler: handler/handler.getStream
events:
- stream:
type: dynamodb
batchSize: 1
startingPosition: LATEST
arn:
Fn::GetAtt:
- demoTableNew1
- StreamArn
resources:
Resources:
websocketIDTableNew1:
Type: “AWS::DynamoDB::Table”
Properties:
AttributeDefinitions:
- AttributeName: “connectionId”
AttributeType: “S”
KeySchema:
- AttributeName: “connectionId”
KeyType: “HASH”
BillingMode: PAY_PER_REQUEST
TableName: websocketIDTableNew1
demoTableNew1:
Type: AWS::DynamoDB::Table
Properties:
BillingMode: PAY_PER_REQUEST
TableName: demoTableNew1
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
AttributeDefinitions:
- AttributeName: pk
AttributeType: S
- AttributeName: sk
AttributeType: S
KeySchema:
- AttributeName: pk
KeyType: HASH
- AttributeName: sk
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: reverse
KeySchema:
- AttributeName: sk
KeyType: HASH
- AttributeName: pk
KeyType: RANGE
Projection:
ProjectionType: ALL