Sending websocket message in Go lambda function

Viewed 246

I have two Go functions:

func NewAPIGatewaySession() *apigatewaymanagementapi.ApiGatewayManagementApi {
    sesh := session.Must(session.NewSessionWithOptions(session.Options{
        SharedConfigState: session.SharedConfigEnable,
    }))
    return apigatewaymanagementapi.New(sesh)
}

and

func SendWsMessage(connectionID string, msgData []byte) error {
    connectionInput := &apigatewaymanagementapi.PostToConnectionInput{
        ConnectionId: aws.String(connectionID),
        Data:         msgData,
    }
    _, err := NewAPIGatewaySession().PostToConnection(connectionInput)
    return err
}

But unfortunately I am getting the error:

RequestError: send request failed caused by: Post "https://execute-api.us-east-1.amazonaws.com/@connections/GN5OCf-coAMCElw%3D": dial tcp: lookup execute-api.us-east-1.amazonaws.com on 169.254.78.1:53: no such host

This code is inside a Docker image lambda function. I am not sure if this is some sort of DNS error (but probably not if it has found out "169.254.78.1")?

1 Answers

Your lambda function needs to know what is the exact endpoint address of AWS Gateway management API. You can configure the address of the endpoint like this.

api := apigatewaymanagementapi.New(session)
api.Endpoint = "https://xxxxxxx.execute-api.eu-west-1.amazonaws.com/prod"

Endpoint address can be found in AWS console in API Gateway configuration under API -> Stage -> Connection URL.

Related