4
votes

I have been struggling to send a response to connected clients through aws provided callback url(https://******.execute-api.us-east-1.amazonaws.com/test/@connections).

Have the below code in my backend to send the response which is one way communication.

    AwsClientBuilder.EndpointConfiguration config =
            new AwsClientBuilder.EndpointConfiguration("https://*********.execute-api.us-east-1.amazonaws.com/test", "us-east-1");
    AmazonApiGatewayManagementApi client = AmazonApiGatewayManagementApiClientBuilder.standard()
            .withEndpointConfiguration(config).build();


    PostToConnectionRequest request = new PostToConnectionRequest();
    request.withConnectionId("bGYcWek5oAMCKwA=");
    Charset charset = Charset.forName("UTF-8");
    CharsetEncoder encoder = charset.newEncoder();
    ByteBuffer buff = null;
    try {
        buff = encoder.encode(CharBuffer.wrap("Hello from lambda"));
    } catch (CharacterCodingException e) {
        log.error("Error while encoding", e);
    }
    request.withData(buff);
    PostToConnectionResult result = client.postToConnection(request);
    log.info("Result of websocket send:\n" + result);

Getting the Service: AmazonApiGatewayManagementApi; Status Code: 403; Error Code: InvalidSignatureException exception.

Note: I have everything setup like certificates and aws access keys.

As mentioned in the aws doc, I have tried with awscurl which is working. I am able to see the response on websocket connected clients. https://docs.amazonaws.cn/en_us/apigateway/latest/developerguide/apigateway-how-to-call-websocket-api-connections.html

1

1 Answers

0
votes

You need to provide your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to form BasicAWSCredentials.

BasicAWSCredentials awsCreds = new BasicAWSCredentials("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY");
AmazonApiGatewayManagementApi client = AmazonApiGatewayManagementApiClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds)).withEndpointConfiguration(config).build();