Skip to content

Commit 172e127

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

File tree

9 files changed

+449
-22
lines changed

9 files changed

+449
-22
lines changed

source/patterns/@aws-solutions-constructs/aws-dynamodb-stream-lambda-elasticsearch-kibana/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ _Parameters_
6363
|existingLambdaObj?|[`lambda.Function`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Function.html)|Existing instance of Lambda Function object, providing both this and `lambdaFunctionProps` will cause an error.|
6464
|lambdaFunctionProps?|[`lambda.FunctionProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.FunctionProps.html)|User provided props to override the default props for the Lambda function.|
6565
|dynamoTableProps?|[`dynamodb.TableProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.TableProps.html)|Optional user provided props to override the default props for DynamoDB Table|
66-
|existingTableObj?|[`dynamodb.Table`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.Table.html)|Existing instance of DynamoDB table object, providing both this and `dynamoTableProps` will cause an error.|
66+
|existingTableInterface?|[`dynamodb.ITable`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.ITable.html)|Existing instance of DynamoDB table object or interface, providing both this and `dynamoTableProps` will cause an error.|
6767
|dynamoEventSourceProps?|[`aws-lambda-event-sources.DynamoEventSourceProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda-event-sources.DynamoEventSourceProps.html)|Optional user provided props to override the default props for DynamoDB Event Source|
6868
|esDomainProps?|[`elasticsearch.CfnDomainProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-elasticsearch.CfnDomainProps.html)|Optional user provided props to override the default props for the Elasticsearch Service|
6969
|domainName|`string`|Domain name for the Cognito and the Elasticsearch Service|
@@ -73,7 +73,8 @@ _Parameters_
7373

7474
| **Name** | **Type** | **Description** |
7575
|:-------------|:----------------|-----------------|
76-
|dynamoTable|[`dynamodb.Table`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.Table.html)|Returns an instance of dynamodb.Table created by the construct|
76+
|dynamoTableInterface|[`dynamodb.ITable`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.ITable.html)|Returns an instance of dynamodb.ITable created by the construct|
77+
|dynamoTable?|[`dynamodb.Table`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.Table.html)|Returns an instance of dynamodb.Table created by the construct|
7778
|lambdaFunction|[`lambda.Function`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Function.html)|Returns an instance of lambda.Function created by the construct|
7879
|userPool|[`cognito.UserPool`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cognito.UserPool.html)|Returns an instance of cognito.UserPool created by the construct|
7980
|userPoolClient|[`cognito.UserPoolClient`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cognito.UserPoolClient.html)|Returns an instance of cognito.UserPoolClient created by the construct|

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

+4-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.ITable,
54+
readonly existingTableInterface?: dynamodb.ITable,
5555
/**
5656
* Optional user provided props to override the default props
5757
*
@@ -101,6 +101,7 @@ export class DynamoDBStreamToLambdaToElasticSearchAndKibana extends Construct {
101101
private dynamoDBStreamToLambda: DynamoDBStreamToLambda;
102102
private lambdaToElasticSearchAndKibana: LambdaToElasticSearchAndKibana;
103103
public readonly lambdaFunction: lambda.Function;
104+
public readonly dynamoTableInterface: dynamodb.ITable;
104105
public readonly dynamoTable?: dynamodb.Table;
105106
public readonly userPool: cognito.UserPool;
106107
public readonly userPoolClient: cognito.UserPoolClient;
@@ -126,7 +127,7 @@ export class DynamoDBStreamToLambdaToElasticSearchAndKibana extends Construct {
126127
lambdaFunctionProps: props.lambdaFunctionProps,
127128
dynamoEventSourceProps: props.dynamoEventSourceProps,
128129
dynamoTableProps: props.dynamoTableProps,
129-
existingTableObj: props.existingTableObj,
130+
existingTableInterface: props.existingTableInterface,
130131
deploySqsDlqQueue: props.deploySqsDlqQueue,
131132
sqsDlqQueueProps: props.sqsDlqQueueProps
132133
};
@@ -146,6 +147,7 @@ export class DynamoDBStreamToLambdaToElasticSearchAndKibana extends Construct {
146147
this.lambdaToElasticSearchAndKibana = new LambdaToElasticSearchAndKibana(this, 'LambdaToElasticSearch', _props2);
147148

148149
this.dynamoTable = this.dynamoDBStreamToLambda.dynamoTable;
150+
this.dynamoTableInterface = this.dynamoDBStreamToLambda.dynamoTableInterface;
149151
this.userPool = this.lambdaToElasticSearchAndKibana.userPool;
150152
this.userPoolClient = this.lambdaToElasticSearchAndKibana.userPoolClient;
151153
this.identityPool = this.lambdaToElasticSearchAndKibana.identityPool;

source/patterns/@aws-solutions-constructs/aws-dynamodb-stream-lambda/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ _Parameters_
5858
|existingLambdaObj?|[`lambda.Function`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Function.html)|Existing instance of Lambda Function object, providing both this and `lambdaFunctionProps` will cause an error.|
5959
|lambdaFunctionProps?|[`lambda.FunctionProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.FunctionProps.html)|User provided props to override the default props for the Lambda function.|
6060
|dynamoTableProps?|[`dynamodb.TableProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.TableProps.html)|Optional user provided props to override the default props for DynamoDB Table|
61-
|existingTableObj?|[`dynamodb.Table`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.Table.html)|Existing instance of DynamoDB table object, providing both this and `dynamoTableProps` will cause an error.|
61+
|existingTableInterface?|[`dynamodb.ITable`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.ITable.html)|Existing instance of DynamoDB table object or interface, providing both this and `dynamoTableProps` will cause an error.|
6262
|dynamoEventSourceProps?|[`aws-lambda-event-sources.DynamoEventSourceProps`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda-event-sources.DynamoEventSourceProps.html)|Optional user provided props to override the default props for DynamoDB Event Source|
6363

6464
## Pattern Properties
6565

6666
| **Name** | **Type** | **Description** |
6767
|:-------------|:----------------|-----------------|
68-
|dynamoTable|[`dynamodb.Table`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.Table.html)|Returns an instance of dynamodb.Table created by the construct|
68+
|dynamoTableInterface|[`dynamodb.ITable`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.ITable.html)|Returns an instance of dynamodb.ITable created by the construct|
69+
|dynamoTable?|[`dynamodb.Table`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-dynamodb.Table.html)|Returns an instance of dynamodb.Table created by the construct|
6970
|lambdaFunction|[`lambda.Function`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Function.html)|Returns an instance of lambda.Function created by the construct|
7071

7172
## Lambda Function

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

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

7070
export class DynamoDBStreamToLambda extends Construct {
7171
public readonly lambdaFunction: lambda.Function;
72+
public readonly dynamoTableInterface: dynamodb.ITable;
7273
public readonly dynamoTable?: dynamodb.Table;
7374

7475
/**
@@ -90,11 +91,12 @@ export class DynamoDBStreamToLambda extends Construct {
9091

9192
const table: dynamodb.ITable = defaults.buildDynamoDBTableWithStream(this, {
9293
dynamoTableProps: props.dynamoTableProps,
93-
existingTableObj: props.existingTableObj
94+
existingTableInterface: props.existingTableInterface
9495
});
9596

96-
if (table instanceof dynamodb.Table) {
97-
this.dynamoTable = table;
97+
this.dynamoTableInterface = table;
98+
if (!props.existingTableInterface) {
99+
this.dynamoTable = table as dynamodb.Table;
98100
}
99101

100102
// Grant DynamoDB Stream read perimssion for lambda function

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

+30-6
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,37 @@ test('check dynamodb table stream override', () => {
185185

186186
});
187187

188-
test('check getter methods', () => {
188+
test('check getter methods without existingTableInterface', () => {
189189
const stack = new cdk.Stack();
190190

191191
const construct: DynamoDBStreamToLambda = deployNewFunc(stack);
192192

193-
expect(construct.lambdaFunction !== null);
194-
expect(construct.dynamoTable !== null);
193+
expect(construct.lambdaFunction).toBeInstanceOf(lambda.Function);
194+
expect(construct.dynamoTableInterface).toHaveProperty('tableName');
195+
expect(construct.dynamoTable).toBeInstanceOf(dynamodb.Table);
196+
expect(construct.dynamoTable).toHaveProperty('addGlobalSecondaryIndex');
197+
});
198+
199+
test('check getter methods with existingTableInterface', () => {
200+
const stack = new cdk.Stack();
201+
202+
const construct: DynamoDBStreamToLambda = new DynamoDBStreamToLambda(stack, 'test', {
203+
existingTableInterface: new dynamodb.Table(stack, 'table', {
204+
partitionKey: {
205+
name: 'id',
206+
type: dynamodb.AttributeType.STRING
207+
},
208+
stream: dynamodb.StreamViewType.NEW_AND_OLD_IMAGES
209+
}),
210+
lambdaFunctionProps: {
211+
code: lambda.Code.fromAsset(`${__dirname}/lambda`),
212+
runtime: lambda.Runtime.NODEJS_12_X,
213+
handler: 'index.handler'
214+
},
215+
});
216+
217+
expect(construct.lambdaFunction).toBeInstanceOf(lambda.Function);
218+
expect(construct.dynamoTable).toBeUndefined();
195219
});
196220

197221
test('check exception for Missing existingObj from props', () => {
@@ -209,7 +233,7 @@ test('check exception for Missing existingObj from props', () => {
209233

210234
test('check dynamodb table stream override with ITable', () => {
211235
const stack = new cdk.Stack();
212-
const existingTableObj = dynamodb.Table.fromTableAttributes(stack, 'existingtable', {
236+
const existingTableInterface = dynamodb.Table.fromTableAttributes(stack, 'existingtable', {
213237
tableArn: 'arn:aws:dynamodb:us-east-1:xxxxxxxxxxxxx:table/existing-table',
214238
tableStreamArn: 'arn:aws:dynamodb:us-east-1:xxxxxxxxxxxxx:table/existing-table/stream/2020-06-22T18:34:05.824'
215239
});
@@ -219,7 +243,7 @@ test('check dynamodb table stream override with ITable', () => {
219243
runtime: lambda.Runtime.NODEJS_12_X,
220244
handler: 'index.handler'
221245
},
222-
existingTableObj
246+
existingTableInterface
223247
};
224248

225249
new DynamoDBStreamToLambda(stack, 'test-lambda-dynamodb-stack', props);
@@ -271,4 +295,4 @@ test('check dynamodb table stream override with ITable', () => {
271295
Version: "2012-10-17"
272296
}
273297
});
274-
});
298+
});

0 commit comments

Comments
 (0)