Created
August 23, 2021 13:04
-
-
Save rogerchi/662fd33ccf22bb4d77693747e0c0fc6f to your computer and use it in GitHub Desktop.
CDK Websocket Domain Name example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as path from 'path'; | |
import * as apigatewayv2 from '@aws-cdk/aws-apigatewayv2'; | |
import * as apigatewayv2Integrations from '@aws-cdk/aws-apigatewayv2-integrations'; | |
import * as certificatemanager from '@aws-cdk/aws-certificatemanager'; | |
import * as lnjs from '@aws-cdk/aws-lambda-nodejs'; | |
import * as route53 from '@aws-cdk/aws-route53'; | |
import * as alias from '@aws-cdk/aws-route53-targets'; | |
import * as cdk from '@aws-cdk/core'; | |
export interface WebsocketExampleProps { | |
wssDomainName: string; | |
certificate: certificatemanager.ICertificate; | |
zone: route53.IHostedZone; | |
} | |
export class WebsocketExample extends cdk.Construct { | |
constructor( | |
scope: cdk.Construct, | |
id: string, | |
{ wssDomainName, certificate, zone }: WebsocketExampleProps, | |
) { | |
super(scope, id); | |
const connectHandler = new lnjs.NodejsFunction(this, 'ConnectHandler', { | |
entry: path.join(__dirname, './connect-handler.ts'), | |
}); | |
const disconnectHandler = new lnjs.NodejsFunction( | |
this, | |
'DisconnectHandler', | |
{ | |
entry: path.join(__dirname, './disconnect-handler.ts'), | |
}, | |
); | |
const defaultHandler = new lnjs.NodejsFunction(this, 'DefaultHandler', { | |
entry: path.join(__dirname, './default-handler.ts'), | |
}); | |
const webSocketApi = new apigatewayv2.WebSocketApi(this, 'WebsocketExample', { | |
connectRouteOptions: { | |
integration: new apigatewayv2Integrations.LambdaWebSocketIntegration({ | |
handler: connectHandler, | |
}), | |
}, | |
disconnectRouteOptions: { | |
integration: new apigatewayv2Integrations.LambdaWebSocketIntegration({ | |
handler: disconnectHandler, | |
}), | |
}, | |
defaultRouteOptions: { | |
integration: new apigatewayv2Integrations.LambdaWebSocketIntegration({ | |
handler: defaultHandler, | |
}), | |
}, | |
}); | |
const stageName = 'prod'; | |
const stage = new apigatewayv2.WebSocketStage(this, 'WssStage', { | |
webSocketApi, | |
stageName, | |
autoDeploy: true, | |
}); | |
const domainName = new apigatewayv2.DomainName(this, 'WssDomain', { | |
certificate, | |
domainName: wssDomainName, | |
}); | |
const apiMapping = new apigatewayv2.ApiMapping(this, 'ApiMapping', { | |
api: webSocketApi, | |
domainName: domainName, | |
stage, | |
}); | |
apiMapping.node.addDependency(domainName); | |
new route53.ARecord(this, 'WssAGW', { | |
recordName: wssDomainName, | |
zone, | |
target: route53.RecordTarget.fromAlias( | |
new alias.ApiGatewayv2DomainProperties( | |
domainName.regionalDomainName, | |
domainName.regionalHostedZoneId, | |
), | |
), | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment