/
apigateway-java-lambda-authorizer-stack.ts
53 lines (49 loc) · 1.85 KB
/
apigateway-java-lambda-authorizer-stack.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
import * as cdk from '@aws-cdk/core';
import * as apigateway from '@aws-cdk/aws-apigateway';
import * as lambda from '@aws-cdk/aws-lambda';
import * as logs from '@aws-cdk/aws-logs';
export class ApigatewayJavaLambdaAuthorizerStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const accessLogGroup = new logs.LogGroup(this, 'AccessLog');
const api = new apigateway.RestApi(this, 'Api', {
deployOptions: {
loggingLevel: apigateway.MethodLoggingLevel.INFO,
dataTraceEnabled: true,
accessLogDestination: new apigateway.LogGroupLogDestination(accessLogGroup),
accessLogFormat: apigateway.AccessLogFormat.clf()
}
});
const authorizerFunction = new lambda.Function(this, 'AuthorizerFunction', {
runtime: lambda.Runtime.JAVA_8,
handler: 'com.kazfuku.aws.MapLambdaAuthorizer::handleRequest',
// handler: 'com.kazfuku.aws.PojoLambdaAuthorizer::handleRequest',
code: lambda.Code.fromAsset('lambda/authorizer/target/apigateway-java-lambda-authorizer-1.0.jar')
});
const lambdaAuthorizer = new apigateway.RequestAuthorizer(this, 'Authorizer', {
handler: authorizerFunction,
identitySources: [apigateway.IdentitySource.header('Authorization')]
});
const mockIntegration = new apigateway.MockIntegration({
requestTemplates: {
'application/json': '{"statusCode": 200}'
},
integrationResponses: [
{
statusCode: '200',
responseTemplates: {
'application/json': '{ "message": "mock", "now": "$context.authorizer.now" }'
}
}
]
});
const method = api.root.addMethod('GET', mockIntegration, {
methodResponses: [
{
statusCode: '200'
}
],
authorizer: lambdaAuthorizer
});
}
}