-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lambda-event-sources) add DynamoEventSource (#1563)
Add a Dynamo EventSource for consuming a table's stream from Lambda.
- Loading branch information
Sam Goodwin
authored
Jan 23, 2019
1 parent
9f8bfa5
commit d8207e3
Showing
9 changed files
with
497 additions
and
2 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
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
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
43 changes: 43 additions & 0 deletions
43
packages/@aws-cdk/aws-lambda-event-sources/lib/dynamodb.ts
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,43 @@ | ||
import dynamodb = require('@aws-cdk/aws-dynamodb'); | ||
import lambda = require('@aws-cdk/aws-lambda'); | ||
|
||
export interface DynamoEventSourceProps { | ||
/** | ||
* The largest number of records that AWS Lambda will retrieve from your event | ||
* source at the time of invoking your function. Your function receives an | ||
* event with all the retrieved records. | ||
* | ||
* Valid Range: Minimum value of 1. Maximum value of 1000. | ||
* | ||
* @default 100 | ||
*/ | ||
batchSize?: number; | ||
|
||
/** | ||
* Where to begin consuming the DynamoDB stream. | ||
*/ | ||
startingPosition: lambda.StartingPosition; | ||
} | ||
|
||
/** | ||
* Use an Amazon DynamoDB stream as an event source for AWS Lambda. | ||
*/ | ||
export class DynamoEventSource implements lambda.IEventSource { | ||
constructor(private readonly table: dynamodb.Table, private readonly props: DynamoEventSourceProps) { | ||
if (this.props.batchSize !== undefined && (this.props.batchSize < 1 || this.props.batchSize > 1000)) { | ||
throw new Error(`Maximum batch size must be between 1 and 1000 inclusive (given ${this.props.batchSize})`); | ||
} | ||
} | ||
|
||
public bind(target: lambda.FunctionBase) { | ||
new lambda.EventSourceMapping(target, `DynamoDBEventSource:${this.table.node.uniqueId}`, { | ||
target, | ||
batchSize: this.props.batchSize || 100, | ||
eventSourceArn: this.table.tableStreamArn, | ||
startingPosition: this.props.startingPosition | ||
}); | ||
|
||
this.table.grantStreamRead(target.role); | ||
dynamodb.Table.grantListStreams(target.role); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './dynamodb'; | ||
export * from './kinesis'; | ||
export * from './sqs'; | ||
export * from './s3'; | ||
|
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
130 changes: 130 additions & 0 deletions
130
packages/@aws-cdk/aws-lambda-event-sources/test/integ.dynamodb.expected.json
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,130 @@ | ||
{ | ||
"Resources": { | ||
"FServiceRole3AC82EE1": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "lambda.amazonaws.com" | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"ManagedPolicyArns": [ | ||
{ | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"arn:", | ||
{ | ||
"Ref": "AWS::Partition" | ||
}, | ||
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" | ||
] | ||
] | ||
} | ||
] | ||
} | ||
}, | ||
"FServiceRoleDefaultPolicy17A19BFA": { | ||
"Type": "AWS::IAM::Policy", | ||
"Properties": { | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": [ | ||
"dynamodb:DescribeStream", | ||
"dynamodb:GetRecords", | ||
"dynamodb:GetShardIterator" | ||
], | ||
"Effect": "Allow", | ||
"Resource": { | ||
"Fn::GetAtt": [ | ||
"TD925BC7E", | ||
"StreamArn" | ||
] | ||
} | ||
}, | ||
{ | ||
"Action": "dynamodb:ListStreams", | ||
"Effect": "Allow", | ||
"Resource": "*" | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"PolicyName": "FServiceRoleDefaultPolicy17A19BFA", | ||
"Roles": [ | ||
{ | ||
"Ref": "FServiceRole3AC82EE1" | ||
} | ||
] | ||
} | ||
}, | ||
"FC4345940": { | ||
"Type": "AWS::Lambda::Function", | ||
"Properties": { | ||
"Code": { | ||
"ZipFile": "exports.handler = async function handler(event) {\n console.log('event:', JSON.stringify(event, undefined, 2));\n return { event };\n}" | ||
}, | ||
"Handler": "index.handler", | ||
"Role": { | ||
"Fn::GetAtt": [ | ||
"FServiceRole3AC82EE1", | ||
"Arn" | ||
] | ||
}, | ||
"Runtime": "nodejs8.10" | ||
}, | ||
"DependsOn": [ | ||
"FServiceRole3AC82EE1", | ||
"FServiceRoleDefaultPolicy17A19BFA" | ||
] | ||
}, | ||
"FDynamoDBEventSourcelambdaeventsourcedynamodbT7967476AE652DA48": { | ||
"Type": "AWS::Lambda::EventSourceMapping", | ||
"Properties": { | ||
"EventSourceArn": { | ||
"Fn::GetAtt": [ | ||
"TD925BC7E", | ||
"StreamArn" | ||
] | ||
}, | ||
"FunctionName": { | ||
"Ref": "FC4345940" | ||
}, | ||
"BatchSize": 5, | ||
"StartingPosition": "TRIM_HORIZON" | ||
} | ||
}, | ||
"TD925BC7E": { | ||
"Type": "AWS::DynamoDB::Table", | ||
"Properties": { | ||
"KeySchema": [ | ||
{ | ||
"AttributeName": "id", | ||
"KeyType": "HASH" | ||
} | ||
], | ||
"AttributeDefinitions": [ | ||
{ | ||
"AttributeName": "id", | ||
"AttributeType": "S" | ||
} | ||
], | ||
"ProvisionedThroughput": { | ||
"ReadCapacityUnits": 5, | ||
"WriteCapacityUnits": 5 | ||
}, | ||
"StreamSpecification": { | ||
"StreamViewType": "NEW_IMAGE" | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.