Use Private WebHook for SNS Subscription
I have a commercial product that integrate with AWS through SNS https endpoint subscription. However the SNS http/https subscription required the endpoint to be available from Internet. In some restricted environment this is not possible.
Though we cannot use the private webhook directly, but its is still possible to use it indirectly through the lambda function. The idea is to subscribe the SNS topic with a lambda function, where we parse the content and post the message to the private HTTP endpoint.
The Golang Lambda Function
The logic is simple. In the handler function, for each of the records, get its SNS payload, construct the HTTP header and Post it to the target private HTTP endpoint using the resty library.
package main
import (
"context"
"crypto/tls"
"fmt"
"log"
"net/http"
"os"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/go-resty/resty/v2"
)
func handler(ctx context.Context, snsEvent events.SNSEvent) error {
log.Printf("Received event: %v", snsEvent)
client := resty.New()
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
targetURL := os.Getenv("TARGET_URL")
if targetURL == "" {
return fmt.Errorf("TARGET_URL environment variable is not set")
}
for i, record := range snsEvent.Records {…