1
1
import {
2
- aws_apigatewayv2 as ApiGateway ,
2
+ aws_apigatewayv2 as ApiGatewayV2 ,
3
3
Duration ,
4
4
aws_dynamodb as DynamoDB ,
5
5
aws_events as Events ,
@@ -12,6 +12,7 @@ import {
12
12
} from 'aws-cdk-lib'
13
13
import { Construct } from 'constructs'
14
14
import type { PackedLambda } from '../helpers/lambdas/packLambda'
15
+ import { ApiLogging } from './ApiLogging.js'
15
16
import type { DeviceStorage } from './DeviceStorage.js'
16
17
17
18
export const integrationUri = (
@@ -159,12 +160,12 @@ export class WebsocketAPI extends Construct {
159
160
} )
160
161
161
162
// API
162
- const api = new ApiGateway . CfnApi ( this , 'api' , {
163
+ const api = new ApiGatewayV2 . CfnApi ( this , 'api' , {
163
164
name : 'websocketGateway' ,
164
165
protocolType : 'WEBSOCKET' ,
165
166
routeSelectionExpression : '$request.body.message' ,
166
167
} )
167
- const authorizer = new ApiGateway . CfnAuthorizer ( this , 'authorizer' , {
168
+ const authorizer = new ApiGatewayV2 . CfnAuthorizer ( this , 'authorizer' , {
168
169
apiId : api . ref ,
169
170
authorizerType : 'REQUEST' ,
170
171
name : `authorizer` ,
@@ -174,7 +175,7 @@ export class WebsocketAPI extends Construct {
174
175
principal : new IAM . ServicePrincipal ( 'apigateway.amazonaws.com' ) ,
175
176
} )
176
177
// API on connect
177
- const connectIntegration = new ApiGateway . CfnIntegration (
178
+ const connectIntegration = new ApiGatewayV2 . CfnIntegration (
178
179
this ,
179
180
'connectIntegration' ,
180
181
{
@@ -184,7 +185,7 @@ export class WebsocketAPI extends Construct {
184
185
integrationUri : integrationUri ( this , onConnect ) ,
185
186
} ,
186
187
)
187
- const connectRoute = new ApiGateway . CfnRoute ( this , 'connectRoute' , {
188
+ const connectRoute = new ApiGatewayV2 . CfnRoute ( this , 'connectRoute' , {
188
189
apiId : api . ref ,
189
190
routeKey : '$connect' ,
190
191
authorizationType : 'CUSTOM' ,
@@ -193,7 +194,7 @@ export class WebsocketAPI extends Construct {
193
194
authorizerId : authorizer ?. ref ,
194
195
} )
195
196
// API on message
196
- const onMessageIntegration = new ApiGateway . CfnIntegration (
197
+ const onMessageIntegration = new ApiGatewayV2 . CfnIntegration (
197
198
this ,
198
199
'onMessageIntegration' ,
199
200
{
@@ -203,15 +204,15 @@ export class WebsocketAPI extends Construct {
203
204
integrationUri : integrationUri ( this , onMessage ) ,
204
205
} ,
205
206
)
206
- const onMessageRoute = new ApiGateway . CfnRoute ( this , 'onMessageRoute' , {
207
+ const onMessageRoute = new ApiGatewayV2 . CfnRoute ( this , 'onMessageRoute' , {
207
208
apiId : api . ref ,
208
209
routeKey : 'message' ,
209
210
authorizationType : 'NONE' ,
210
211
operationName : 'OnMessageRoute' ,
211
212
target : `integrations/${ onMessageIntegration . ref } ` ,
212
213
} )
213
214
// API on disconnect
214
- const disconnectIntegration = new ApiGateway . CfnIntegration (
215
+ const disconnectIntegration = new ApiGatewayV2 . CfnIntegration (
215
216
this ,
216
217
'disconnectIntegration' ,
217
218
{
@@ -221,21 +222,21 @@ export class WebsocketAPI extends Construct {
221
222
integrationUri : integrationUri ( this , onDisconnect ) ,
222
223
} ,
223
224
)
224
- const disconnectRoute = new ApiGateway . CfnRoute ( this , 'disconnectRoute' , {
225
+ const disconnectRoute = new ApiGatewayV2 . CfnRoute ( this , 'disconnectRoute' , {
225
226
apiId : api . ref ,
226
227
routeKey : '$disconnect' ,
227
228
authorizationType : 'NONE' ,
228
229
operationName : 'DisconnectRoute' ,
229
230
target : `integrations/${ disconnectIntegration . ref } ` ,
230
231
} )
231
232
// API deploy
232
- const deployment = new ApiGateway . CfnDeployment ( this , 'apiDeployment' , {
233
+ const deployment = new ApiGatewayV2 . CfnDeployment ( this , 'apiDeployment' , {
233
234
apiId : api . ref ,
234
235
} )
235
236
deployment . node . addDependency ( connectRoute )
236
237
deployment . node . addDependency ( onMessageRoute )
237
238
deployment . node . addDependency ( disconnectRoute )
238
- const prodStage = new ApiGateway . CfnStage ( this , 'prodStage' , {
239
+ const prodStage = new ApiGatewayV2 . CfnStage ( this , 'prodStage' , {
239
240
stageName : '2023-06-22' ,
240
241
deploymentId : deployment . ref ,
241
242
apiId : api . ref ,
@@ -269,13 +270,22 @@ export class WebsocketAPI extends Construct {
269
270
} :${ api . ref } /${ prodStage . stageName } /$disconnect`,
270
271
} )
271
272
273
+ // Construct URLs
274
+ this . websocketURI = `wss://${ api . ref } .execute-api.${
275
+ Stack . of ( this ) . region
276
+ } .amazonaws.com/${ prodStage . ref } `
272
277
this . websocketAPIArn = `arn:aws:execute-api:${ Stack . of ( this ) . region } :${
273
278
Stack . of ( this ) . account
274
279
} :${ api . ref } /${ prodStage . stageName } /POST/@connections/*`
275
280
this . websocketManagementAPIURL = `https://${ api . ref } .execute-api.${
276
281
Stack . of ( this ) . region
277
282
} .amazonaws.com/${ prodStage . stageName } `
278
283
284
+ // Logging
285
+ if ( this . node . tryGetContext ( 'isTest' ) === true ) {
286
+ new ApiLogging ( this , api , prodStage )
287
+ }
288
+
279
289
// Publish event to sockets
280
290
const publishToWebsocketClients = new Lambda . Function (
281
291
this ,
0 commit comments