-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynamodb.ts
140 lines (125 loc) · 6.41 KB
/
dynamodb.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { RemovalPolicy, StackProps, aws_logs as logs } from 'aws-cdk-lib';
import { LambdaIntegration, RestApi } from 'aws-cdk-lib/aws-apigateway';
import { AttributeType, BillingMode, ProjectionType, Table } from 'aws-cdk-lib/aws-dynamodb';
import { Architecture, Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';
import { AwsStack } from './aws-cdk-js-dev-guide-stack';
export class DynamoDbComponents {
constructor(stack: AwsStack, id: string, props?: StackProps, customOptions?: any) {
// If you're not already familiar with DynamoDB's reserved keywords, it's
// worth checking your attribute names against
// https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html.
// NOTE: remove timeToLiveAttribute if you don't want to set a TTL for the data
const dynamodbTable = new Table(stack, 'dynamodb-table', {
partitionKey: { name: 'dataOwner', type: AttributeType.STRING },
sortKey: { name: 'objectId', type: AttributeType.STRING },
billingMode: BillingMode.PAY_PER_REQUEST,
timeToLiveAttribute: 'expiration'
});
stack.dynamodbTable = dynamodbTable;
// Querying a DynamoDB table without knowing the partition key is not
// possible without a Secondary Index, see
// https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SecondaryIndexes.html
// for more details.
// NOTE: by default the projection type is set to ALL when it should
// be set to the minimum number of fields required by the queries.
const DDB_GSI_NAME = 'dynamodb-table-flip-index';
dynamodbTable.addGlobalSecondaryIndex({
indexName: DDB_GSI_NAME,
projectionType: ProjectionType.KEYS_ONLY,
partitionKey: { name: 'objectId', type: AttributeType.STRING },
});
const dynamodbGetFunction = new Function(stack, 'dynamodb-function-get', {
runtime: Runtime.NODEJS_20_X,
architecture: Architecture.ARM_64,
handler: 'get.handler',
code: Code.fromAsset('./handlers/dynamodb'),
environment: {
...stack.cors.corsEnvironment,
TABLE_NAME: dynamodbTable.tableName,
DDB_GSI_NAME
},
layers: [stack.layer],
logGroup: new logs.LogGroup(stack, `dynamodb-function-get-logs`, {
logGroupName: `${id}-dynamodb-function-get`,
removalPolicy: RemovalPolicy.DESTROY,
retention: logs.RetentionDays.THREE_MONTHS,
}),
});
dynamodbTable.grantReadData(dynamodbGetFunction);
const dynamodbScanFunction = new Function(stack, 'dynamodb-function-scan', {
runtime: Runtime.NODEJS_20_X,
architecture: Architecture.ARM_64,
handler: 'scan.handler',
code: Code.fromAsset('./handlers/dynamodb'),
environment: {
...stack.cors.corsEnvironment,
TABLE_NAME: dynamodbTable.tableName
},
layers: [stack.layer],
logGroup: new logs.LogGroup(stack, `dynamodb-function-scan-logs`, {
logGroupName: `${id}-dynamodb-function-scan`,
removalPolicy: RemovalPolicy.DESTROY,
retention: logs.RetentionDays.THREE_MONTHS,
}),
});
dynamodbTable.grantReadData(dynamodbScanFunction);
// create reusable lambda integration
const dynamodbScanFunctionIntegration = new LambdaIntegration(dynamodbScanFunction);
stack.dynamodbScanFunctionIntegration = dynamodbScanFunctionIntegration;
const dynamodbCreateFunction = new Function(stack, 'dynamodb-function-create', {
runtime: Runtime.NODEJS_20_X,
architecture: Architecture.ARM_64,
handler: 'create.handler',
code: Code.fromAsset('./handlers/dynamodb'),
environment: {
...stack.cors.corsEnvironment,
TABLE_NAME: dynamodbTable.tableName
},
layers: [stack.layer],
logGroup: new logs.LogGroup(stack, `dynamodb-function-create-logs`, {
logGroupName: `${id}-dynamodb-function-create`,
removalPolicy: RemovalPolicy.DESTROY,
retention: logs.RetentionDays.THREE_MONTHS,
}),
});
dynamodbTable.grantWriteData(dynamodbCreateFunction);
const dynamodbUpdateFunction = new Function(stack, 'dynamodb-function-update', {
runtime: Runtime.NODEJS_20_X,
architecture: Architecture.ARM_64,
handler: 'update.handler',
code: Code.fromAsset('./handlers/dynamodb'),
environment: {
...stack.cors.corsEnvironment,
TABLE_NAME: dynamodbTable.tableName,
DDB_GSI_NAME
},
layers: [stack.layer],
logGroup: new logs.LogGroup(stack, `dynamodb-function-update-logs`, {
logGroupName: `${id}-dynamodb-function-update`,
removalPolicy: RemovalPolicy.DESTROY,
retention: logs.RetentionDays.THREE_MONTHS,
}),
});
// the update itself doesn't require read permissions, but we may need to query
// the object using the global secondary index. see handlers/dynamodb/update.js
dynamodbTable.grantReadData(dynamodbUpdateFunction);
dynamodbTable.grantWriteData(dynamodbUpdateFunction);
// dynamodb api
const dynamodbApi = new RestApi(stack, 'dynamodb-api', {
defaultCorsPreflightOptions: stack.cors.corsOptions,
});
// /objects
// https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/prod/objects
let apiObjects = dynamodbApi.root.addResource('objects');
// GET /objects : list all objects
apiObjects.addMethod('GET', dynamodbScanFunctionIntegration);
// POST /objects : add a new object
apiObjects.addMethod('POST', new LambdaIntegration(dynamodbCreateFunction));
// objects/{id}
let apiObjectsObject = apiObjects.addResource("{objectId}")
// GET /objects/{id} : get object with specified id
apiObjectsObject.addMethod('GET', new LambdaIntegration(dynamodbGetFunction));
// PUT /objects/{id} : update object with specified id
apiObjectsObject.addMethod('PUT', new LambdaIntegration(dynamodbUpdateFunction));
}
};