-
Notifications
You must be signed in to change notification settings - Fork 0
/
testDocDB.ts
71 lines (60 loc) · 2.73 KB
/
testDocDB.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
import { Duration, NestedStack, NestedStackProps, RemovalPolicy } from "aws-cdk-lib";
import { SubnetType, Vpc } from "aws-cdk-lib/aws-ec2";
import { Construct } from "constructs";
import { LambdaPowertoolsLayer } from 'cdk-aws-lambda-powertools-layer';
import { LayerVersion, Code, Runtime, Function, AssetCode, Tracing } from 'aws-cdk-lib/aws-lambda';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { DatabaseCluster } from "aws-cdk-lib/aws-docdb";
import { Cors, LambdaIntegration, RestApi } from 'aws-cdk-lib/aws-apigateway';
import * as Config from "../config.json"
interface TestDocDBStackProps extends NestedStackProps {
vpc: Vpc
docdbcluster: DatabaseCluster
}
export class TestDocDB extends NestedStack {
testAPI: RestApi
constructor(scope: Construct, id: string, props: TestDocDBStackProps) {
super(scope, id, props)
const powertoolsLayer = new LambdaPowertoolsLayer(this, 'PowerTools', {
includeExtras: true
});
const pymnglayer = new LayerVersion(this, 'pymongopluspem', {
code: Code.fromAsset('lambdalayers/pymng.zip'),
compatibleRuntimes: [Runtime.PYTHON_3_9],
description: 'PyMongo Package with PEM Key'
})
const docDBTestFunction = new Function(this, 'DocDBLambdaTestFunc', {
code: new AssetCode('src'),
handler: 'docdbtestfunc.handler',
runtime: Runtime.PYTHON_3_9,
logRetention: RetentionDays.ONE_DAY,
timeout: Duration.seconds(300),
layers: [powertoolsLayer, pymnglayer],
environment: {
CLUSTERSM: props.docdbcluster.secret?.secretFullArn as string,
DOCDB_DATABASE: Config.DocDB.sampleDatabaseName,
DOCDB_COLLECTION: Config.DocDB.sampleCollectionName
},
vpc: props.vpc,
vpcSubnets: { subnetType: SubnetType.PRIVATE_ISOLATED },
tracing: Tracing.ACTIVE
})
props.docdbcluster.secret?.grantRead(docDBTestFunction)
this.testAPI = new RestApi(this, 'testAPI', {
restApiName: "Test DocDB API",
defaultCorsPreflightOptions: {
allowOrigins: Cors.ALL_ORIGINS,
allowMethods: Cors.ALL_METHODS
},
deployOptions: {
tracingEnabled: true,
dataTraceEnabled: true
}
})
const testCall = this.testAPI.root.addResource('test')
const getId = testCall.addResource('{objectid}')
const testCallIntegration = new LambdaIntegration(docDBTestFunction)
getId.addMethod('GET', testCallIntegration)
testCall.addMethod('POST', testCallIntegration)
}
}