-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathindex.ts
108 lines (100 loc) · 3.97 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import * as lambda from '@aws-cdk/aws-lambda';
import { DynamoEventSourceProps, DynamoEventSource } from '@aws-cdk/aws-lambda-event-sources';
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import * as defaults from '@aws-solutions-constructs/core';
import { Construct } from '@aws-cdk/core';
import * as sqs from '@aws-cdk/aws-sqs';
/**
* @summary The properties for the DynamoDBStreamToLambda Construct
*/
export interface DynamoDBStreamToLambdaProps {
/**
* Existing instance of Lambda Function object, providing both this and `lambdaFunctionProps` will cause an error.
*
* @default - None
*/
readonly existingLambdaObj?: lambda.Function,
/**
* User provided props to override the default props for the Lambda function.
*
* @default - Default props are used
*/
readonly lambdaFunctionProps?: lambda.FunctionProps,
/**
* Optional user provided props to override the default props
*
* @default - Default props are used
*/
readonly dynamoTableProps?: dynamodb.TableProps,
/**
* Existing instance of DynamoDB table object, providing both this and `dynamoTableProps` will cause an error.
*
* @default - None
*/
readonly existingTableInterface?: dynamodb.ITable,
/**
* Optional user provided props to override the default props
*
* @default - Default props are used
*/
readonly dynamoEventSourceProps?: DynamoEventSourceProps | any,
/**
* Whether to deploy a SQS dead letter queue when a data record reaches the Maximum Retry Attempts or Maximum Record Age,
* its metadata like shard ID and stream ARN will be sent to an SQS queue.
*
* @default - true.
*/
readonly deploySqsDlqQueue?: boolean,
/**
* Optional user provided properties for the SQS dead letter queue
*
* @default - Default props are used
*/
readonly sqsDlqQueueProps?: sqs.QueueProps
}
export class DynamoDBStreamToLambda extends Construct {
public readonly lambdaFunction: lambda.Function;
public readonly dynamoTableInterface: dynamodb.ITable;
public readonly dynamoTable?: dynamodb.Table;
/**
* @summary Constructs a new instance of the LambdaToDynamoDB class.
* @param {cdk.App} scope - represents the scope for all the resources.
* @param {string} id - this is a a scope-unique id.
* @param {DynamoDBStreamToLambdaProps} props - user provided props for the construct
* @since 0.8.0
* @access public
*/
constructor(scope: Construct, id: string, props: DynamoDBStreamToLambdaProps) {
super(scope, id);
defaults.CheckProps(props);
this.lambdaFunction = defaults.buildLambdaFunction(this, {
existingLambdaObj: props.existingLambdaObj,
lambdaFunctionProps: props.lambdaFunctionProps
});
[this.dynamoTableInterface, this.dynamoTable] = defaults.buildDynamoDBTableWithStream(this, {
dynamoTableProps: props.dynamoTableProps,
existingTableInterface: props.existingTableInterface
});
// Grant DynamoDB Stream read perimssion for lambda function
this.dynamoTableInterface.grantStreamRead(this.lambdaFunction.grantPrincipal);
// Add the Lambda event source mapping
const eventSourceProps = defaults.DynamoEventSourceProps(this, {
eventSourceProps: props.dynamoEventSourceProps,
deploySqsDlqQueue: props.deploySqsDlqQueue,
sqsDlqQueueProps: props.sqsDlqQueueProps
});
this.lambdaFunction.addEventSource(new DynamoEventSource(this.dynamoTableInterface, eventSourceProps));
}
}