Skip to content

Instantly share code, notes, and snippets.

@strund3r
Created March 31, 2022 21:18
Show Gist options
  • Save strund3r/44c9b4859f42cd057abe9c54de27f245 to your computer and use it in GitHub Desktop.
Save strund3r/44c9b4859f42cd057abe9c54de27f245 to your computer and use it in GitHub Desktop.
AWS Lambda to publish an AWS Health event on an AWS SNS topic
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
)
var (
sns_topic_arn string = os.Getenv("SNS_TOPIC_ARN")
environment string = os.Getenv("ENVIRONMENT")
)
type CloudWatchEventDetails struct {
EventArn string `json:"eventArn"`
Service string `json:"service"`
EventTypeCode string `json:"eventTypeCode"`
EventTypeCategory string `json:"eventTypeCategory"`
StartTime string `json:"startTime"`
EndTime string `json:"endTime"`
EventDescription []struct {
Language string `json:"language"`
LatestDescription string `json:"latestDescription"`
} `json:"eventDescription"`
AffectedEntities []struct {
EntityValue string `json:"entityValue"`
Tags struct {
Stage string `json:"stage"`
App string `json:"app"`
} `json:"tags"`
} `json:"affectedEntities"`
}
type SNSEventMessage struct {
Default string `json:"default"`
}
type EventMessage struct {
Description string `json:"description"`
StartTime string `json:"startTime"`
EndTime string `json:"endTime"`
Resources []string `json:"resources"`
}
func handleRequest(ctx context.Context, event events.CloudWatchEvent) {
fmt.Println("This is the JSON for event details", string(event.Detail))
var eventDetails CloudWatchEventDetails
// Unmarshal the event details
err := json.Unmarshal(event.Detail, &eventDetails)
if err != nil {
log.Fatal(fmt.Errorf("unmarshal: %w", err))
}
fmt.Printf("This is the CloudWatchEvent: %+v\n", eventDetails)
// Create SNS message info
topicSubject := fmt.Sprintf("[AWS %s - %s] %s", eventDetails.Service, strings.Title(environment), eventDetails.EventTypeCode)
eventMessage, err := json.Marshal(EventMessage{
Description: eventDetails.EventDescription[0].LatestDescription,
StartTime: eventDetails.StartTime,
EndTime: eventDetails.EndTime,
Resources: event.Resources,
})
if err != nil {
log.Fatal(fmt.Errorf("marshaling event message: %w", err))
}
snsMessage, err := json.Marshal(SNSEventMessage{
Default: string(eventMessage),
})
if err != nil {
log.Fatal(fmt.Errorf("marshaling sns event message: %w", err))
}
// Add SNS params
snsPublishParams := &sns.PublishInput{
Message: aws.String(string(snsMessage)),
MessageStructure: aws.String("json"),
Subject: aws.String(topicSubject),
TopicArn: aws.String(sns_topic_arn),
}
// Create AWS session
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// Create SNS client
svc := sns.New(sess)
// Publish SNS message
pub, err := svc.Publish(snsPublishParams)
if err != nil {
log.Fatal("Error publishing message:", err)
return
}
log.Print("MessageID:", *pub.MessageId)
}
// handleRequest is our lambda handler invoked by the `lambda.Start` function call
func main() {
lambda.Start(handleRequest)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment