1
1
import { Construct } from 'constructs' ;
2
2
import { aws_ssm , Duration } from 'aws-cdk-lib' ;
3
3
import { HttpJwtAuthorizer } from 'aws-cdk-lib/aws-apigatewayv2-authorizers' ;
4
- import { CorsHttpMethod , HttpApi } from 'aws-cdk-lib/aws-apigatewayv2' ;
4
+ import { CorsHttpMethod , HttpApi , CfnStage } from 'aws-cdk-lib/aws-apigatewayv2' ;
5
5
import { IStringParameter } from 'aws-cdk-lib/aws-ssm' ;
6
+ import { LogGroup } from 'aws-cdk-lib/aws-logs' ;
7
+ import { Role , ServicePrincipal } from 'aws-cdk-lib/aws-iam' ;
6
8
7
- export interface SRMApiGatewayConstructProps {
9
+ export interface ApiGatewayConstructProps {
8
10
region : string ;
11
+ apiName : string | undefined ;
9
12
cognitoUserPoolIdParameterName : string ;
10
13
cognitoPortalAppClientIdParameterName : string ;
11
14
cognitoStatusPageAppClientIdParameterName : string ;
12
15
}
13
16
14
- export class SRMApiGatewayConstruct extends Construct {
17
+ export class ApiGatewayConstruct extends Construct {
15
18
private readonly _httpApi : HttpApi ;
16
19
17
- constructor ( scope : Construct , id : string , props : SRMApiGatewayConstructProps ) {
20
+ constructor ( scope : Construct , id : string , props : ApiGatewayConstructProps ) {
18
21
super ( scope , id ) ;
19
22
20
23
this . _httpApi = new HttpApi ( this , 'HttpApi' , {
21
- apiName : 'OrcaBusAPI-SequenceRunManager' ,
24
+ apiName : 'OrcaBusAPI-' + props . apiName ,
22
25
corsPreflight : {
23
26
allowHeaders : [ 'Authorization' ] ,
24
27
allowMethods : [
@@ -34,12 +37,48 @@ export class SRMApiGatewayConstruct extends Construct {
34
37
// defaultDomainMapping: ... TODO
35
38
} ) ;
36
39
37
- // TODO Configure access logging. See https://github.com/aws/aws-cdk/issues/11100
40
+ // LogGroups
41
+ this . setupAccessLogs ( ) ;
38
42
39
- // TODO setup cloud map service discovery perhaps
43
+ // CloudMap
44
+ // this.setupCloudServiceDiscovery()
40
45
}
41
46
42
- private getAuthorizer ( props : SRMApiGatewayConstructProps ) : HttpJwtAuthorizer {
47
+ // TODO: https://github.com/aws-samples/aws-cdk-service-discovery-example/tree/main
48
+ // private setupCloudServiceDiscovery() {
49
+ // }
50
+
51
+ // TODO: Taken from https://github.com/aws/aws-cdk/issues/11100#issuecomment-904627081
52
+ // Monitor for higher level CDK construct instead of leveraging CfnStage
53
+ private setupAccessLogs ( ) {
54
+ const accessLogs = new LogGroup ( this , 'OrcaBus-ApiGw-AccessLogs' ) ;
55
+ const stage = this . httpApi . defaultStage ?. node . defaultChild as CfnStage ;
56
+ stage . accessLogSettings = {
57
+ destinationArn : accessLogs . logGroupArn ,
58
+ format : JSON . stringify ( {
59
+ requestId : '$context.requestId' ,
60
+ userAgent : '$context.identity.userAgent' ,
61
+ sourceIp : '$context.identity.sourceIp' ,
62
+ requestTime : '$context.requestTime' ,
63
+ requestTimeEpoch : '$context.requestTimeEpoch' ,
64
+ httpMethod : '$context.httpMethod' ,
65
+ path : '$context.path' ,
66
+ status : '$context.status' ,
67
+ protocol : '$context.protocol' ,
68
+ responseLength : '$context.responseLength' ,
69
+ domainName : '$context.domainName' ,
70
+ } ) ,
71
+ } ;
72
+
73
+ // Allow writing access logs, managed
74
+ const role = new Role ( this , 'AmazonAPIGatewayPushToCloudWatchLogs' , {
75
+ assumedBy : new ServicePrincipal ( 'apigateway.amazonaws.com' ) ,
76
+ } ) ;
77
+
78
+ accessLogs . grantWrite ( role ) ;
79
+ }
80
+
81
+ private getAuthorizer ( props : ApiGatewayConstructProps ) : HttpJwtAuthorizer {
43
82
/**
44
83
* FIXME One fine day in future when we have proper Cognito AAI setup.
45
84
* For the moment, we leverage Portal and established Cognito infrastructure.
0 commit comments