Skip to content

Commit f203845

Browse files
feat(apigatewayv2): Import existing WebSocketApi from attributes (#18958)
Closes: #18755 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent d7b220e commit f203845

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

packages/@aws-cdk/aws-apigatewayv2/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,12 @@ webSocketApi.addRoute('sendmessage', {
404404
});
405405
```
406406

407+
To import an existing WebSocketApi:
408+
409+
```ts
410+
const webSocketApi = apigwv2.WebSocketApi.fromWebSocketApiAttributes(this, 'mywsapi', { webSocketId: 'api-1234' });
411+
```
412+
407413
### Manage Connections Permission
408414

409415
Grant permission to use API Gateway Management API of a WebSocket API by calling the `grantManageConnections` API.

packages/@aws-cdk/aws-apigatewayv2/lib/websocket/api.ts

+36
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,47 @@ export interface WebSocketApiProps {
8585
readonly defaultRouteOptions?: WebSocketRouteOptions;
8686
}
8787

88+
/**
89+
* Attributes for importing a WebSocketApi into the CDK
90+
*/
91+
export interface WebSocketApiAttributes {
92+
/**
93+
* The identifier of the WebSocketApi
94+
*/
95+
readonly webSocketId: string;
96+
97+
/**
98+
* The endpoint URL of the WebSocketApi
99+
* @default - throw san error if apiEndpoint is accessed.
100+
*/
101+
readonly apiEndpoint?: string;
102+
}
103+
104+
88105
/**
89106
* Create a new API Gateway WebSocket API endpoint.
90107
* @resource AWS::ApiGatewayV2::Api
91108
*/
92109
export class WebSocketApi extends ApiBase implements IWebSocketApi {
110+
/**
111+
* Import an existing WebSocket API into this CDK app.
112+
*/
113+
public static fromWebSocketApiAttributes(scope: Construct, id: string, attrs: WebSocketApiAttributes): IWebSocketApi {
114+
class Import extends ApiBase {
115+
public readonly apiId = attrs.webSocketId;
116+
public readonly websocketApiId = attrs.webSocketId;
117+
private readonly _apiEndpoint = attrs.apiEndpoint;
118+
119+
public get apiEndpoint(): string {
120+
if (!this._apiEndpoint) {
121+
throw new Error('apiEndpoint is not configured on the imported WebSocketApi.');
122+
}
123+
return this._apiEndpoint;
124+
}
125+
}
126+
return new Import(scope, id);
127+
}
128+
93129
public readonly apiId: string;
94130
public readonly apiEndpoint: string;
95131

packages/@aws-cdk/aws-apigatewayv2/test/websocket/api.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,25 @@ describe('WebSocketApi', () => {
107107
});
108108
});
109109

110+
test('import', () => {
111+
// GIVEN
112+
const stack = new Stack();
113+
const imported = WebSocketApi.fromWebSocketApiAttributes(stack, 'imported', { webSocketId: 'ws-1234', apiEndpoint: 'api-endpoint' });
114+
115+
// THEN
116+
expect(imported.apiId).toEqual('ws-1234');
117+
expect(imported.apiEndpoint).toEqual('api-endpoint');
118+
});
119+
120+
test('apiEndpoint for imported', () => {
121+
// GIVEN
122+
const stack = new Stack();
123+
const api = WebSocketApi.fromWebSocketApiAttributes(stack, 'imported', { webSocketId: 'api-1234' });
124+
125+
// THEN
126+
expect(() => api.apiEndpoint).toThrow(/apiEndpoint is not configured/);
127+
});
128+
110129
describe('grantManageConnections', () => {
111130
test('adds an IAM policy to the principal', () => {
112131
// GIVEN

0 commit comments

Comments
 (0)