I am trying to achieve the following: Cloudwatch alarm details are received as JSON to a Lambda The Lambda looks at the JSON to determine if the 'NewStateValue' == "ALARM" If it does == "ALARM" forward the whole JSON received from the SNS out via another SNS.
I am most of the way towards achieving this and I have the following code:
package main
import (
"context"
"fmt"
"encoding/json"
"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"
)
func handler(ctx context.Context, snsEvent events.SNSEvent) {
for _, record := range snsEvent.Records {
snsRecord := record.SNS
//To add in additional fields to publish to the logs add "snsRecord.'fieldname'"
fmt.Printf("Message = %s \n", snsRecord.Message)
var event CloudWatchAlarm
err := json.Unmarshal([]byte(snsRecord.Message), &event)
if err != nil {
fmt.Println("There is an error: " + err.Error())
}
fmt.Printf("Test Message = %s \n", event.NewStateValue)
if ( event.NewStateValue == "ALARM") {
svc := sns.New(session.New())
// params will be sent to the publish call included here is the bare minimum params to send a message.
params := &sns.PublishInput{
Message: Message: aws.String("message"), // This is the message itself (can be XML / JSON / Text - anything you want)
TopicArn: aws.String("my arn"), //Get this from the Topic in the AWS console.
}
resp, err := svc.Publish(params) //Call to puclish the message
if err != nil { //Check for errors
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
return
}
// Pretty-print the response data.
fmt.Println(resp)
}
}
}
func main() {
lambda.Start(handler)
}
Currently this sends an email to the address set-up in the SNS linked to the ARN above. However I would like the email to include the full, ideally formatted, JSON received by the first SNS. I have the Cloudwatch JSON structure defined in another file, this is being called by var event CloudWatchAlarm