Skip to content

Commit 7451a9a

Browse files
committed
fix(allow ITable for dynamodb stream patterns)
1 parent f902cc7 commit 7451a9a

File tree

4 files changed

+94
-8
lines changed

4 files changed

+94
-8
lines changed

source/patterns/@aws-solutions-constructs/aws-dynamodb-stream-lambda-elasticsearch-kibana/lib/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export interface DynamoDBStreamToLambdaToElasticSearchAndKibanaProps {
5151
*
5252
* @default - None
5353
*/
54-
readonly existingTableObj?: dynamodb.Table,
54+
readonly existingTableObj?: dynamodb.ITable,
5555
/**
5656
* Optional user provided props to override the default props
5757
*
@@ -101,7 +101,7 @@ export class DynamoDBStreamToLambdaToElasticSearchAndKibana extends Construct {
101101
private dynamoDBStreamToLambda: DynamoDBStreamToLambda;
102102
private lambdaToElasticSearchAndKibana: LambdaToElasticSearchAndKibana;
103103
public readonly lambdaFunction: lambda.Function;
104-
public readonly dynamoTable: dynamodb.Table;
104+
public readonly dynamoTable?: dynamodb.Table;
105105
public readonly userPool: cognito.UserPool;
106106
public readonly userPoolClient: cognito.UserPoolClient;
107107
public readonly identityPool: cognito.CfnIdentityPool;

source/patterns/@aws-solutions-constructs/aws-dynamodb-stream-lambda/lib/index.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface DynamoDBStreamToLambdaProps {
4545
*
4646
* @default - None
4747
*/
48-
readonly existingTableObj?: dynamodb.Table,
48+
readonly existingTableObj?: dynamodb.ITable,
4949
/**
5050
* Optional user provided props to override the default props
5151
*
@@ -69,7 +69,7 @@ export interface DynamoDBStreamToLambdaProps {
6969

7070
export class DynamoDBStreamToLambda extends Construct {
7171
public readonly lambdaFunction: lambda.Function;
72-
public readonly dynamoTable: dynamodb.Table;
72+
public readonly dynamoTable?: dynamodb.Table;
7373

7474
/**
7575
* @summary Constructs a new instance of the LambdaToDynamoDB class.
@@ -88,20 +88,24 @@ export class DynamoDBStreamToLambda extends Construct {
8888
lambdaFunctionProps: props.lambdaFunctionProps
8989
});
9090

91-
this.dynamoTable = defaults.buildDynamoDBTableWithStream(this, {
91+
const table: dynamodb.ITable = defaults.buildDynamoDBTableWithStream(this, {
9292
dynamoTableProps: props.dynamoTableProps,
9393
existingTableObj: props.existingTableObj
9494
});
9595

96+
if (table instanceof dynamodb.Table) {
97+
this.dynamoTable = table;
98+
}
99+
96100
// Grant DynamoDB Stream read perimssion for lambda function
97-
this.dynamoTable.grantStreamRead(this.lambdaFunction.grantPrincipal);
101+
table.grantStreamRead(this.lambdaFunction.grantPrincipal);
98102

99103
// Add the Lambda event source mapping
100104
const eventSourceProps = defaults.DynamoEventSourceProps(this, {
101105
eventSourceProps: props.dynamoEventSourceProps,
102106
deploySqsDlqQueue: props.deploySqsDlqQueue,
103107
sqsDlqQueueProps: props.sqsDlqQueueProps
104108
});
105-
this.lambdaFunction.addEventSource(new DynamoEventSource(this.dynamoTable, eventSourceProps));
109+
this.lambdaFunction.addEventSource(new DynamoEventSource(table, eventSourceProps));
106110
}
107111
}

source/patterns/@aws-solutions-constructs/aws-dynamodb-stream-lambda/test/dynamodb-stream-lambda.test.ts

+66
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,70 @@ test('check exception for Missing existingObj from props', () => {
205205
} catch (e) {
206206
expect(e).toBeInstanceOf(Error);
207207
}
208+
});
209+
210+
test('check dynamodb table stream override with ITable', () => {
211+
const stack = new cdk.Stack();
212+
const existingTableObj = dynamodb.Table.fromTableAttributes(stack, 'existingtable', {
213+
tableArn: 'arn:aws:dynamodb:us-east-1:xxxxxxxxxxxxx:table/existing-table',
214+
tableStreamArn: 'arn:aws:dynamodb:us-east-1:xxxxxxxxxxxxx:table/existing-table/stream/2020-06-22T18:34:05.824'
215+
});
216+
const props: DynamoDBStreamToLambdaProps = {
217+
lambdaFunctionProps: {
218+
code: lambda.Code.fromAsset(`${__dirname}/lambda`),
219+
runtime: lambda.Runtime.NODEJS_12_X,
220+
handler: 'index.handler'
221+
},
222+
existingTableObj
223+
};
224+
225+
new DynamoDBStreamToLambda(stack, 'test-lambda-dynamodb-stack', props);
226+
227+
expect(stack).toHaveResource('AWS::Lambda::EventSourceMapping', {
228+
EventSourceArn: "arn:aws:dynamodb:us-east-1:xxxxxxxxxxxxx:table/existing-table/stream/2020-06-22T18:34:05.824",
229+
});
230+
231+
expect(stack).toHaveResource('AWS::IAM::Policy', {
232+
PolicyDocument: {
233+
Statement: [
234+
{
235+
Action: [
236+
"xray:PutTraceSegments",
237+
"xray:PutTelemetryRecords"
238+
],
239+
Effect: "Allow",
240+
Resource: "*"
241+
},
242+
{
243+
Action: "dynamodb:ListStreams",
244+
Effect: "Allow",
245+
Resource: "*"
246+
},
247+
{
248+
Action: [
249+
"dynamodb:DescribeStream",
250+
"dynamodb:GetRecords",
251+
"dynamodb:GetShardIterator"
252+
],
253+
Effect: "Allow",
254+
Resource: "arn:aws:dynamodb:us-east-1:xxxxxxxxxxxxx:table/existing-table/stream/2020-06-22T18:34:05.824"
255+
},
256+
{
257+
Action: [
258+
"sqs:SendMessage",
259+
"sqs:GetQueueAttributes",
260+
"sqs:GetQueueUrl"
261+
],
262+
Effect: "Allow",
263+
Resource: {
264+
"Fn::GetAtt": [
265+
"testlambdadynamodbstackSqsDlqQueue4CC9868B",
266+
"Arn"
267+
]
268+
}
269+
}
270+
],
271+
Version: "2012-10-17"
272+
}
273+
});
208274
});

source/patterns/@aws-solutions-constructs/core/lib/dynamodb-table-helper.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ export interface BuildDynamoDBTableProps {
3232
readonly existingTableObj?: dynamodb.Table
3333
}
3434

35+
export interface BuildDynamoDBTableWithStreamProps {
36+
/**
37+
* Optional user provided props to override the default props
38+
*
39+
* @default - Default props are used
40+
*/
41+
readonly dynamoTableProps?: dynamodb.TableProps,
42+
/**
43+
* Existing instance of dynamodb table object.
44+
* Providing both this and `dynamoTableProps` will cause an error.
45+
*
46+
* @default - None
47+
*/
48+
readonly existingTableObj?: dynamodb.ITable
49+
}
50+
3551
export function buildDynamoDBTable(scope: cdk.Construct, props: BuildDynamoDBTableProps): dynamodb.Table {
3652
// Conditional DynamoDB Table creation
3753
if (!props.existingTableObj) {
@@ -47,7 +63,7 @@ export function buildDynamoDBTable(scope: cdk.Construct, props: BuildDynamoDBTab
4763
}
4864
}
4965

50-
export function buildDynamoDBTableWithStream(scope: cdk.Construct, props: BuildDynamoDBTableProps): dynamodb.Table {
66+
export function buildDynamoDBTableWithStream(scope: cdk.Construct, props: BuildDynamoDBTableWithStreamProps): dynamodb.ITable {
5167
// Conditional DynamoDB Table creation
5268
if (!props.existingTableObj) {
5369
// Set the default props for DynamoDB table

0 commit comments

Comments
 (0)