-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple-function.ts
59 lines (53 loc) · 2.56 KB
/
simple-function.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
import { Duration, RemovalPolicy, StackProps, aws_logs as logs } from 'aws-cdk-lib';
import { AccessLogFormat, LambdaIntegration, LogGroupLogDestination, RestApi } from 'aws-cdk-lib/aws-apigateway';
import { Architecture, Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';
import { LogGroup } from 'aws-cdk-lib/aws-logs';
import { AwsStack } from './aws-cdk-js-dev-guide-stack';
export class SimpleFunction {
constructor(stack: AwsStack, id: string, props?: StackProps, customOptions?: any) {
const simpleFunction = new Function(stack, 'simple-function', {
runtime: Runtime.NODEJS_20_X,
architecture: Architecture.ARM_64,
handler: 'index.handler',
code: Code.fromAsset('./handlers/simple'),
environment: {
...stack.cors.corsEnvironment,
HELLO: "Hello",
WORLD: "World"
},
logGroup: new logs.LogGroup(stack, `simple-function-logs`, {
logGroupName: `${id}-simple-function`,
removalPolicy: RemovalPolicy.DESTROY,
retention: logs.RetentionDays.THREE_MONTHS,
}),
timeout: Duration.seconds(2),
});
// simple REST api interface
// configure log group for RestApi access logs
const simpleFunctionAccessLogGroup = new LogGroup(stack, 'simple-function-access-log-group', {
logGroupName: `apigateway/${id}-simple-function`,
retention: 1 // retention in days
// see https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-logs.LogGroup.html
});
// to enable CORS on all resources of the api, uncomment the line
// beginning with defaultCorsPreflightOptions:
const simpleApi = new RestApi(stack, 'simple-api', {
restApiName: 'Simple API sample',
description: "Simple API sample with no dependencies",
// defaultCorsPreflightOptions: corsOptions,
deployOptions: {
accessLogDestination: new LogGroupLogDestination(simpleFunctionAccessLogGroup),
accessLogFormat: AccessLogFormat.jsonWithStandardFields()
}
});
// to enable CORS on just the root or any other specific resource,
// uncomment the following line:
// simpleApi.root.addCorsPreflight(corsOptions);
simpleApi.root.addMethod('GET', new LambdaIntegration(
simpleFunction,
{
requestTemplates: { "application/json": '{ "statusCode": "200" }' }
}
));
}
};