-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathindex.ts
112 lines (101 loc) · 3.89 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
109
110
111
112
/**
* Copyright 2019 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 * as dynamodb from '@aws-cdk/aws-dynamodb';
import * as defaults from '@aws-solutions-constructs/core';
import { Construct } from '@aws-cdk/core';
import { overrideProps } from '@aws-solutions-constructs/core';
/**
* @summary The properties for the LambdaToDynamoDB Construct
*/
export interface LambdaToDynamoDBProps {
/**
* Whether to create a new lambda function or use an existing lambda function.
* If set to false, you must provide a lambda function object as `existingObj`
*
* @default - true
*/
readonly deployLambda: boolean,
/**
* Existing instance of Lambda Function object.
* If `deploy` is set to false only then this property is required
*
* @default - None
*/
readonly existingLambdaObj?: lambda.Function,
/**
* Optional user provided props to override the default props.
* If `deploy` is set to true only then this property is required
*
* @default - Default props are used
*/
readonly lambdaFunctionProps?: lambda.FunctionProps,
/**
* Existing instance of dynamodb table object.
* If this is set then the dynamoTableProps is ignore.
*
* @default - None
*/
readonly existingTableObj?: dynamodb.Table,
/**
* Optional user provided props to override the default props
*
* @default - Default props are used
*/
readonly dynamoTableProps?: dynamodb.TableProps
}
export class LambdaToDynamoDB extends Construct {
public readonly lambdaFunction: lambda.Function;
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 {LambdaToDynamoDBProps} props - user provided props for the construct
* @since 0.8.0
* @access public
*/
constructor(scope: Construct, id: string, props: LambdaToDynamoDBProps) {
super(scope, id);
this.lambdaFunction = defaults.buildLambdaFunction(this, {
deployLambda: props.deployLambda,
existingLambdaObj: props.existingLambdaObj,
lambdaFunctionProps: props.lambdaFunctionProps
});
if (!props.existingTableObj) {
// Set the default props for DynamoDB table
if (props.dynamoTableProps) {
const dynamoTableProps = overrideProps(defaults.DefaultTableProps, props.dynamoTableProps);
this.dynamoTable = new dynamodb.Table(this, 'DynamoTable', dynamoTableProps);
} else {
this.dynamoTable = new dynamodb.Table(this, 'DynamoTable', defaults.DefaultTableProps);
}
} else {
this.dynamoTable = props.existingTableObj;
}
this.lambdaFunction.addEnvironment('DDB_TABLE_NAME', this.dynamoTable.tableName);
this.dynamoTable.grantReadWriteData(this.lambdaFunction.grantPrincipal);
// Conditional metadata for cfn_nag
if (props.dynamoTableProps?.billingMode === dynamodb.BillingMode.PROVISIONED) {
const cfnTable: dynamodb.CfnTable = this.dynamoTable.node.findChild('Resource') as dynamodb.CfnTable;
cfnTable.cfnOptions.metadata = {
cfn_nag: {
rules_to_suppress: [{
id: 'W73',
reason: `PROVISIONED billing mode is a default and is not explicitly applied as a setting.`
}]
}
};
}
}
}