AWS::Serverless::WebSocketApi
Creates an Amazon API Gateway WebSocket API, which enables you to create two-way interactive communication applications. WebSocket APIs allow the server to send messages to clients without the client having to request them. For more information, see Working with WebSocket APIs in the API Gateway Developer Guide.
We recommend that you use CloudFormation hooks or IAM policies to verify that API Gateway resources have authorizers attached to them to control access to them.
For more information about using CloudFormation hooks, see Registering hooks in the CloudFormation CLI user guide and
the apigw-enforce-authorizer
For more information about using IAM policies, see Require that API routes have authorization in the API Gateway Developer Guide.
Note
When you deploy to AWS CloudFormation, AWS SAM transforms your AWS SAM resources into CloudFormation resources. For more information, see Generated CloudFormation resources for AWS SAM.
Syntax
To declare this entity in your AWS Serverless Application Model (AWS SAM) template, use the following syntax.
YAML
Type: AWS::Serverless::WebSocketApi Properties: ApiKeySelectionExpression:StringAccessLogSettings:AccessLogSettingsAuth:WebSocketApiAuthDefaultRouteSettings:RouteSettingsDescription:StringDisableExecuteApiEndpoint:BooleanDisableSchemaValidation:BooleanDomain:WebSocketApiDomainConfigurationIpAddressType:StringName:StringPropagateTags:BooleanRoutes:RouteConfigurationRouteSelectionExpression:StringRouteSettings:RouteSettingsStageName:StringStageVariables:JsonTags:Map
Properties
-
ApiKeySelectionExpression -
An API key selection expression. For more information, see API Key Selection Expressions in the API Gateway Developer Guide.
Type: String
Required: No
CloudFormation compatibility: This property is passed directly to the
ApiKeySelectionExpressionproperty of anAWS::ApiGatewayV2::Apiresource. -
AccessLogSettings -
The settings for access logging in a stage.
Type: AccessLogSettings
Required: No
CloudFormation compatibility: This property is passed directly to the
AccessLogSettingsproperty of anAWS::ApiGatewayV2::Stageresource. -
Auth -
Configures authorization for controlling access to your WebSocket API. Authorization is applied to the
$connectroute.For more information, see Controlling access to WebSocket APIs in the API Gateway Developer Guide.
Type: WebSocketApiAuth
Required: No
CloudFormation compatibility: This property is unique to AWS SAM and doesn't have an CloudFormation equivalent.
-
DefaultRouteSettings -
The default route settings for this WebSocket API. These settings apply to all routes unless overridden by the
RouteSettingsproperty for certain routes.Type: RouteSettings
Required: No
CloudFormation compatibility: This property is passed directly to the
DefaultRouteSettingsproperty of anAWS::ApiGatewayV2::Stageresource. -
Description -
A description of the WebSocket API.
Type: String
Required: No
CloudFormation compatibility: This property is passed directly to the
Descriptionproperty of anAWS::ApiGatewayV2::Apiresource. -
DisableExecuteApiEndpoint -
Specifies whether clients can invoke your API by using the default
execute-apiendpoint. To require that clients use a custom domain name to invoke your API, disable the default endpoint.Type: Boolean
Required: No
CloudFormation compatibility: This property is passed directly to the
DisableExecuteApiEndpointproperty of anAWS::ApiGatewayV2::Apiresource. -
DisableSchemaValidation -
Avoid validating models when creating a deployment.
Type: Boolean
Required: No
CloudFormation compatibility: This property is passed directly to the
DisableSchemaValidationproperty of anAWS::ApiGatewayV2::Apiresource. -
Domain -
Configures a custom domain for this WebSocket API.
Note
WebSocket APIs do not support mutual TLS authentication (MTLS). If you specify
MutualTlsAuthenticationorOwnershipVerificationCertificateArn, AWS SAM will return an error.Type: WebSocketApiDomainConfiguration
Required: No
CloudFormation compatibility: This property is unique to AWS SAM and doesn't have an CloudFormation equivalent.
-
IpAddressType -
The IP address type for the API. Valid values are
ipv4for IPv4 only anddualstackfor IPv4 and IPv6.Type: String
Required: No
CloudFormation compatibility: This property is passed directly to the
IpAddressTypeproperty of anAWS::ApiGatewayV2::Apiresource. -
Name -
A name for the WebSocket API. If you don't specify a name, AWS SAM generates a name for you.
Type: String
Required: No
CloudFormation compatibility: This property is passed directly to the
Nameproperty of anAWS::ApiGatewayV2::Apiresource. -
If
true, AWS SAM adds theTagsproperty to theAWS::ApiGatewayV2::StageandAWS::ApiGatewayV2::DomainNameresources that AWS SAM generates.Type: Boolean
Required: No
CloudFormation compatibility: This property is unique to AWS SAM and doesn't have an CloudFormation equivalent.
-
Routes -
The route configurations for this WebSocket API. Routes define how messages are routed to Lambda functions. Each route consists of a route key and a Lambda function ARN.
WebSocket APIs support three predefined routes:
$connect,$disconnect, and$default. You can also define custom routes.Type: RouteConfiguration
Required: Yes
CloudFormation compatibility: This property is unique to AWS SAM and doesn't have an CloudFormation equivalent.
-
RouteSelectionExpression -
The route selection expression for the WebSocket API. For more information, see Route Selection Expressions in the API Gateway Developer Guide.
A common value is
$request.body.action, which routes messages based on anactionfield in the message body.Type: String
Required: Yes
CloudFormation compatibility: This property is passed directly to the
RouteSelectionExpressionproperty of anAWS::ApiGatewayV2::Apiresource. -
RouteSettings -
The route settings for this WebSocket API. These settings override the
DefaultRouteSettingsfor specific routes.Type: RouteSettings
Required: No
CloudFormation compatibility: This property is passed directly to the
RouteSettingsproperty of anAWS::ApiGatewayV2::Stageresource. -
StageName -
The name of the API stage. If you don't specify a name, AWS SAM uses
defaultas the stage name.Type: String
Required: No
CloudFormation compatibility: This property is passed directly to the
StageNameproperty of anAWS::ApiGatewayV2::Stageresource. -
StageVariables -
A map that defines the stage variables. Variable names can have alphanumeric and underscore characters, and the values must match
[A-Za-z0-9-._~:/?#&=,]+.Type: Json
Required: No
CloudFormation compatibility: This property is passed directly to the
StageVariablesproperty of anAWS::ApiGatewayV2::Stageresource. -
A map (string to string) that specifies the tags to be added to this WebSocket API. For details about valid keys and values for tags, see Resource tag in the CloudFormation User Guide.
Type: Map
Required: No
CloudFormation compatibility: This property is unique to AWS SAM and doesn't have an CloudFormation equivalent.
Examples
Simple WebSocket API
The following example creates a WebSocket API with three routes.
Resources: MyWebSocketApi: Type: AWS::Serverless::WebSocketApi Properties: RouteSelectionExpression: $request.body.action Routes: $connect: FunctionArn: !GetAtt ConnectFunction.Arn $disconnect: FunctionArn: !GetAtt DisconnectFunction.Arn sendMessage: FunctionArn: !GetAtt SendMessageFunction.Arn ConnectFunction: Type: AWS::Serverless::Function Properties: Handler: index.connect Runtime: nodejs20.x CodeUri: ./src DisconnectFunction: Type: AWS::Serverless::Function Properties: Handler: index.disconnect Runtime: nodejs20.x CodeUri: ./src SendMessageFunction: Type: AWS::Serverless::Function Properties: Handler: index.sendMessage Runtime: nodejs20.x CodeUri: ./src
WebSocket API with Lambda Authorizer
The following example creates a WebSocket API with a Lambda authorizer.
Resources: MyWebSocketApi: Type: AWS::Serverless::WebSocketApi Properties: RouteSelectionExpression: $request.body.action Auth: AuthType: CUSTOM AuthArn: !GetAtt AuthorizerFunction.Arn IdentitySource: - route.request.header.Authorization Routes: $connect: FunctionArn: !GetAtt ConnectFunction.Arn sendMessage: FunctionArn: !GetAtt SendMessageFunction.Arn AuthorizerFunction: Type: AWS::Serverless::Function Properties: Handler: index.authorize Runtime: nodejs20.x CodeUri: ./src ConnectFunction: Type: AWS::Serverless::Function Properties: Handler: index.connect Runtime: nodejs20.x CodeUri: ./src SendMessageFunction: Type: AWS::Serverless::Function Properties: Handler: index.sendMessage Runtime: nodejs20.x CodeUri: ./src