Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/@aws-cdk/aws-apigatewayv2-integrations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
- [Lambda Integration](#lambda)
- [HTTP Proxy Integration](#http-proxy)
- [Private Integration](#private-integration)
- [WebSocket APIs](#websocket-apis)
- [Lambda WebSocket Integration](#lambda-websocket-integration)

## HTTP APIs

Expand Down Expand Up @@ -146,3 +148,32 @@ const httpEndpoint = new HttpApi(stack, 'HttpProxyPrivateApi', {
}),
});
```

## WebSocket APIs

WebSocket integrations connect a route to backend resources. The following integrations are supported in the CDK.

### Lambda WebSocket Integration

Lambda integrations enable integrating a WebSocket API route with a Lambda function. When a client connects/disconnects
or sends message specific to a route, the API Gateway service forwards the request to the Lambda function

The API Gateway service will invoke the lambda function with an event payload of a specific format.

The following code configures a `sendmessage` route with a Lambda integration

```ts
const webSocketApi = new WebSocketApi(stack, 'mywsapi');
new WebSocketStage(stack, 'mystage', {
webSocketApi,
stageName: 'dev',
autoDeploy: true,
});

const messageHandler = new lambda.Function(stack, 'MessageHandler', {...});
webSocketApi.addRoute('sendmessage', {
integration: new LambdaWebSocketIntegration({
handler: connectHandler,
}),
});
```
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './http';
export * from './websocket';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lambda';
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
IWebSocketRouteIntegration,
WebSocketIntegrationType,
WebSocketRouteIntegrationBindOptions,
WebSocketRouteIntegrationConfig,
} from '@aws-cdk/aws-apigatewayv2';
import { ServicePrincipal } from '@aws-cdk/aws-iam';
import { IFunction } from '@aws-cdk/aws-lambda';
import { Names, Stack } from '@aws-cdk/core';

/**
* Lambda WebSocket Integration props
*/
export interface LambdaWebSocketIntegrationProps {
/**
* The handler for this integration.
*/
readonly handler: IFunction
}

/**
* Lambda WebSocket Integration
*/
export class LambdaWebSocketIntegration implements IWebSocketRouteIntegration {
constructor(private props: LambdaWebSocketIntegrationProps) {}

bind(options: WebSocketRouteIntegrationBindOptions): WebSocketRouteIntegrationConfig {
const route = options.route;
this.props.handler.addPermission(`${Names.nodeUniqueId(route.node)}-Permission`, {
scope: options.scope,
principal: new ServicePrincipal('apigateway.amazonaws.com'),
sourceArn: Stack.of(route).formatArn({
service: 'execute-api',
resource: route.webSocketApi.apiId,
resourceName: `*/*${route.routeKey}`,
}),
});

return {
type: WebSocketIntegrationType.AWS_PROXY,
uri: this.props.handler.functionArn,
};
}
}
Loading