Send message from serverless application to client through WebSocket

0

There is an API Gateway with Regional endpoint and WebSocket protocol that integrated with a lambda function. I can open wss://[gatewayId].execute-api.[region].amazonaws.com/[stage]/ connection, send a message to it and get the ConnectionId in the response. The response is getting in (new WebSocket(wssUri)).addEventListener('message', (event) => ...) on the client; the same as direct messages from server should be got. To send a message to the connected client, the application sends a request to HTTPS endpoint in the AGI Gateway. The call should be signed, so AWSSDK.ApiGatewayManagementApi package is used (.NET6, C#):

  •       var  connectionId = "...";
          var config = new AmazonApiGatewayManagementApiConfig
          {
              ServiceURL = "https://[gatewayId].execute-api.[region].amazonaws.com/[stage]",
              RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName("[region]")
          };
          var client = new AmazonApiGatewayManagementApiClient(config);
          var postToConnectionRequest = new PostToConnectionRequest
          {
              ConnectionId = connectionId,
              Data = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("Hello from C#!"))
          };
          await client.PostToConnectionAsync(postToConnectionRequest);
    

*The code is run either in the integrated with the API Gateway lambda function or other lambda function; they both have a role with "AmazonAPIGatewayInvokeFullAccess" permission. Any case, the PostToConnectionAsync call in the last string fails due to timeout (30s). As I see the same can be done from a local console application, but new BasicAWSCredentials(accessKey, secretKey) parameter should be added to AmazonApiGatewayManagementApiClient constructor to sign the call. Then I can wait for the exception: System.Net.Http.HttpRequestException: The requested name is valid, but no data of the requested type was found. (execute-api.[region].amazonaws.com:443). There are little info about the question and nothing in C#. Could you help, what is wrong?

Erop
asked 7 months ago299 views
2 Answers
0

I understand that you are trying to send a message to the connected clients using PostToConnectionAsync operation from your Lambda function using dotnet runtime and it is timing out after 30 seconds. As your function is trying to access the execute-api endpoint to send a message, you must ensure that your function has an internet connectivity. In case the function is not associated with a VPC, it would have internet connectivity by default. If it is associated with a VPC, you must ensure that the function is deployed in private subnets of the VPC, with default route through public NAT Gateway. You must also ensure that the subnet level NACLs allow both inbound and outbound traffic over port 443. Also, the security group associated with the function, should allow outbound traffic over port 443.

You are also receiving : System.Net.Http.HttpRequestException with error message as The requested name is valid, but no data of the requested type was found. I found a reference to this error in our AWS SDK for dotnet GitHub issue according to which it should be fixed in AWSSDK.Core 3.7.10.9. Hence, the request would be to update your function code to use the latest version of AWSSDK.Core.

In case, you are still observing the issue even after following the above suggestions, then please reach out to our AWS Premium Support engineering team by creating a technical case with Lambda team using Support Center.

profile picture
answered 7 months ago
0

Thank you for response. It had me trying again and solve the problem. It appears the matter was in the code, not in the settings. AmazonApiGatewayManagementApiConfig.RegionEndpoint property should be not set. The same is for AuthenticationRegion property while signing request from a console application. It is not quite clear the reason why, but removing this (correct) property from configuration makes working both integrated and stand-alone lambda functions and a console application test.

Erop
answered 7 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions