Skip to content
v2.276.0

WebSocketAwsIntegration Class ​

AWS WebSocket AWS Type Integration.

Example ​

ts
import { WebSocketAwsIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
import * as iam from 'aws-cdk-lib/aws-iam';

const webSocketApi = new apigwv2.WebSocketApi(this, 'mywsapi');
new apigwv2.WebSocketStage(this, 'mystage', {
  webSocketApi,
  stageName: 'dev',
  autoDeploy: true,
});

declare const apiRole: iam.Role;
declare const table: dynamodb.Table;
webSocketApi.addRoute('$connect', {
  integration: new WebSocketAwsIntegration('DynamodbPutItem', {
    integrationUri: `arn:aws:apigateway:${this.region}:dynamodb:action/PutItem`,
    integrationMethod: apigwv2.HttpMethod.POST,
    credentialsRole: apiRole,
    requestTemplates: {
      'application/json': JSON.stringify({
        TableName: table.tableName,
        Item: {
          id: {
            S: '$context.requestId',
          },
        },
      }),
    },
  }),
});
python
from aws_cdk.aws_apigatewayv2_integrations import WebSocketAwsIntegration
import aws_cdk.aws_dynamodb as dynamodb
import aws_cdk.aws_iam as iam

# api_role: iam.Role
# table: dynamodb.Table


web_socket_api = apigwv2.WebSocketApi(self, "mywsapi")
apigwv2.WebSocketStage(self, "mystage",
    web_socket_api=web_socket_api,
    stage_name="dev",
    auto_deploy=True
)
web_socket_api.add_route("$connect",
    integration=WebSocketAwsIntegration("DynamodbPutItem",
        integration_uri=f"arn:aws:apigateway:{this.region}:dynamodb:action/PutItem",
        integration_method=apigwv2.HttpMethod.POST,
        credentials_role=api_role,
        request_templates={
            "application/json": JSON.stringify({
                "TableName": table.table_name,
                "Item": {
                    "id": {
                        "S": "$context.requestId"
                    }
                }
            })
        }
    )
)
java
import software.amazon.awscdk.aws_apigatewayv2_integrations.WebSocketAwsIntegration;
import software.amazon.awscdk.services.dynamodb.*;
import software.amazon.awscdk.services.iam.*;

Role apiRole;
Table table;


WebSocketApi webSocketApi = new WebSocketApi(this, "mywsapi");
WebSocketStage.Builder.create(this, "mystage")
        .webSocketApi(webSocketApi)
        .stageName("dev")
        .autoDeploy(true)
        .build();
webSocketApi.addRoute("$connect", WebSocketRouteOptions.builder()
        .integration(WebSocketAwsIntegration.Builder.create("DynamodbPutItem")
                .integrationUri(String.format("arn:aws:apigateway:%s:dynamodb:action/PutItem", this.region))
                .integrationMethod(HttpMethod.POST)
                .credentialsRole(apiRole)
                .requestTemplates(Map.of(
                        "application/json", JSON.stringify(Map.of(
                                "TableName", table.getTableName(),
                                "Item", Map.of(
                                        "id", Map.of(
                                                "S", "$context.requestId"))))))
                .build())
        .build());
csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AwsApigatewayv2Integrations;
using Amazon.CDK.AWS.DynamoDB;
using Amazon.CDK.AWS.IAM;

Role apiRole;
Table table;


var webSocketApi = new WebSocketApi(this, "mywsapi");
new WebSocketStage(this, "mystage", new WebSocketStageProps {
    WebSocketApi = webSocketApi,
    StageName = "dev",
    AutoDeploy = true
});
webSocketApi.AddRoute("$connect", new WebSocketRouteOptions {
    Integration = new WebSocketAwsIntegration("DynamodbPutItem", new WebSocketAwsIntegrationProps {
        IntegrationUri = $"arn:aws:apigateway:{this.region}:dynamodb:action/PutItem",
        IntegrationMethod = HttpMethod.POST,
        CredentialsRole = apiRole,
        RequestTemplates = new Dictionary<string, string> {
            { "application/json", JSON.Stringify(new Dictionary<string, object> {
                { "TableName", table.TableName },
                { "Item", new Struct {
                    Id = new Struct {
                        S = "$context.requestId"
                    }
                } }
            }) }
        }
    })
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import "github.com/aws/aws-cdk-go/awscdk"
import dynamodb "github.com/aws/aws-cdk-go/awscdk"
import iam "github.com/aws/aws-cdk-go/awscdk"

var apiRole Role
var table Table


webSocketApi := apigwv2.NewWebSocketApi(this, jsii.String("mywsapi"))
apigwv2.NewWebSocketStage(this, jsii.String("mystage"), &WebSocketStageProps{
	WebSocketApi: WebSocketApi,
	StageName: jsii.String("dev"),
	AutoDeploy: jsii.Boolean(true),
})
webSocketApi.AddRoute(jsii.String("$connect"), &WebSocketRouteOptions{
	Integration: awscdk.NewWebSocketAwsIntegration(jsii.String("DynamodbPutItem"), &WebSocketAwsIntegrationProps{
		IntegrationUri: fmt.Sprintf("arn:aws:apigateway:%v:dynamodb:action/PutItem", this.Region),
		IntegrationMethod: apigwv2.HttpMethod_POST,
		CredentialsRole: apiRole,
		RequestTemplates: map[string]*string{
			"application/json": JSON.stringify(map[string]interface{}{
				"TableName": table.tableName,
				"Item": map[string]map[string]*string{
					"id": map[string]*string{
						"S": jsii.String("$context.requestId"),
					},
				},
			}),
		},
	}),
})

Initializer ​

ts
new WebSocketAwsIntegration(id: string, props: WebSocketAwsIntegrationProps)
python
WebSocketAwsIntegration(id: str, props: WebSocketAwsIntegrationProps)
java
new WebSocketAwsIntegration(String id, WebSocketAwsIntegrationProps props)
csharp
new WebSocketAwsIntegration(string id, WebSocketAwsIntegrationProps props)
go
NewWebSocketAwsIntegration(Id *string, Props WebSocketAwsIntegrationProps)

Methods ​

bind(_options: WebSocketRouteIntegrationBindOptions): WebSocketRouteIntegrationConfigdef bind(self, _options: WebSocketRouteIntegrationBindOptions) -> WebSocketRouteIntegrationConfigpublic WebSocketRouteIntegrationConfig bind(WebSocketRouteIntegrationBindOptions _options)public WebSocketRouteIntegrationConfig Bind(WebSocketRouteIntegrationBindOptions _options)func Bind(_options WebSocketRouteIntegrationBindOptions) WebSocketRouteIntegrationConfig#
Bind this integration to the route.