forked from aws-samples/aws-sdk-js-notes-app
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathnotes-api.ts
More file actions
45 lines (37 loc) · 1.27 KB
/
notes-api.ts
File metadata and controls
45 lines (37 loc) · 1.27 KB
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
import {
aws_dynamodb as dynamodb,
aws_lambda as lambda,
aws_s3 as s3,
} from "aws-cdk-lib";
import { Construct } from "constructs";
export interface NotesApiProps {
/** the dynamodb table to be passed to lambda function **/
table: dynamodb.Table;
/** the actions which should be granted on the table */
grantActions: string[];
}
export class NotesApi extends Construct {
/** allows accessing the counter function */
public readonly handler: lambda.Function;
constructor(scope: Construct, id: string, props: NotesApiProps) {
super(scope, id);
const { table, grantActions } = props;
const isLocalDev = ["true", true].includes(process.env.IS_LOCAL_DEV);
const codeConfig = isLocalDev
? lambda.Code.fromBucket(
s3.Bucket.fromBucketName(this, "hot-reload", "hot-reload"),
`${__dirname}/../../backend/dist/${id}`
)
: lambda.Code.fromAsset(`../backend/dist/${id}`);
this.handler = new lambda.Function(this, "handler", {
runtime: lambda.Runtime.NODEJS_18_X,
handler: "app.handler",
code: codeConfig,
environment: {
NOTES_TABLE_NAME: table.tableName,
},
});
// grant the lambda role read/write permissions to notes table
table.grant(this.handler, ...grantActions);
}
}