Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { WebSocketApi, WebSocketStage } from 'aws-cdk-lib/aws-apigatewayv2';
import * as cdk from 'aws-cdk-lib';
import { IntegTest } from '@aws-cdk/integ-tests-alpha';
import { WebSocketMockIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations';

/*
* Stack verification steps:
* 1. Verify that the new property 'disableExecuteApiEndpoint' is set to 'true', and that it is not callable.
*/

const app = new cdk.App();
const stack = new cdk.Stack(app, 'integ-apigwv2-disable-execute-api-endpoint');

// API Gateway WebSocket API
const webSocketApi = new WebSocketApi(stack, 'webSocketApi', {
description: 'Test stack for the disableExecuteApiEndpoint property.',
disableExecuteApiEndpoint: true,
defaultRouteOptions: { integration: new WebSocketMockIntegration('DefaultIntegration') },
});

// Optionally, create a WebSocket stage
new WebSocketStage(stack, 'DevStage', {
webSocketApi: webSocketApi,
stageName: 'dev',
autoDeploy: true,
});

new IntegTest(app, 'DisableExecuteApiEndpointPropIntegrationTest', {
testCases: [stack],
cdkCommandOptions: {
deploy: {
args: {
rollback: true,
},
},
},
});

app.synth();
31 changes: 29 additions & 2 deletions packages/aws-cdk-lib/aws-apigatewayv2/lib/websocket/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ export interface WebSocketApiProps {
* @default false
*/
readonly disableSchemaValidation?: boolean;

/**
* Specifies whether clients can invoke your API using the default endpoint.
* By default, clients can invoke your API with the default
* `https://{api_id}.execute-api.{region}.amazonaws.com` endpoint. Set this to
* true if you would like clients to use your custom domain name.
*
* @default false execute-api endpoint enabled.
*/
readonly disableExecuteApiEndpoint?: boolean;
}

/**
Expand Down Expand Up @@ -148,19 +158,25 @@ export class WebSocketApi extends ApiBase implements IWebSocketApi {
}

public readonly apiId: string;
public readonly apiEndpoint: string;
private readonly _apiEndpoint: string;

/**
* A human friendly name for this WebSocket API. Note that this is different from `webSocketApiId`.
*/
public readonly webSocketApiName?: string;

/**
* Specifies whether clients can invoke this HTTP API by using the default execute-api endpoint.
*/
public readonly disableExecuteApiEndpoint?: boolean;

constructor(scope: Construct, id: string, props?: WebSocketApiProps) {
super(scope, id);
// Enhanced CDK Analytics Telemetry
addConstructMetadata(this, props);

this.webSocketApiName = props?.apiName ?? id;
this.disableExecuteApiEndpoint = props?.disableExecuteApiEndpoint;

const resource = new CfnApi(this, 'Resource', {
name: this.webSocketApiName,
Expand All @@ -170,9 +186,10 @@ export class WebSocketApi extends ApiBase implements IWebSocketApi {
routeSelectionExpression: props?.routeSelectionExpression ?? '$request.body.action',
ipAddressType: props?.ipAddressType,
disableSchemaValidation: props?.disableSchemaValidation,
disableExecuteApiEndpoint: this.disableExecuteApiEndpoint,
});
this.apiId = resource.ref;
this.apiEndpoint = resource.attrApiEndpoint;
this._apiEndpoint = resource.attrApiEndpoint;

if (props?.connectRouteOptions) {
this.addRoute('$connect', props.connectRouteOptions);
Expand All @@ -185,6 +202,16 @@ export class WebSocketApi extends ApiBase implements IWebSocketApi {
}
}

/**
* Get the default endpoint for this API.
*/
public get apiEndpoint(): string {
if (this.disableExecuteApiEndpoint) {
throw new ValidationError('apiEndpoint is not accessible when disableExecuteApiEndpoint is set to true.', this);
}
return this._apiEndpoint;
}

/**
* Add a new route
*/
Expand Down
24 changes: 24 additions & 0 deletions packages/aws-cdk-lib/aws-apigatewayv2/test/websocket/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,30 @@ describe('WebSocketApi', () => {
});
});

test('disableExecuteApiEndpoint is enabled', () => {
const stack = new Stack();
new WebSocketApi(stack, 'api', {
disableExecuteApiEndpoint: true,
});

Template.fromStack(stack).hasResourceProperties('AWS::ApiGatewayV2::Api', {
Name: 'api',
ProtocolType: 'WEBSOCKET',
DisableExecuteApiEndpoint: true,
});
});

test('throws when accessing apiEndpoint and disableExecuteApiEndpoint is true', () => {
const stack = new Stack();
const api = new WebSocketApi(stack, 'api', {
disableExecuteApiEndpoint: true,
});

expect(() => api.apiEndpoint).toThrow(
/apiEndpoint is not accessible when disableExecuteApiEndpoint is set to true./,
);
});

test('import', () => {
// GIVEN
const stack = new Stack();
Expand Down
Loading