Amazon web services 将从SNS接收到的JSON转发给Lambda-GoLang
我正在努力实现以下目标: Cloudwatch报警详细信息作为JSON接收到Lambda Lambda查看JSON以确定“NewStateValue”是否为“ALARM” 如果确实是,则==“报警”将从SNS接收到的整个JSON通过另一个SNS转发出去 我是实现这一目标的主要途径,我有以下代码:Amazon web services 将从SNS接收到的JSON转发给Lambda-GoLang,amazon-web-services,go,aws-lambda,Amazon Web Services,Go,Aws Lambda,我正在努力实现以下目标: Cloudwatch报警详细信息作为JSON接收到Lambda Lambda查看JSON以确定“NewStateValue”是否为“ALARM” 如果确实是,则==“报警”将从SNS接收到的整个JSON通过另一个SNS转发出去 我是实现这一目标的主要途径,我有以下代码: package main import ( "context" "fmt" "encoding/json" "github.com/aws/aws-lambda-go
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)
}
目前,这会向与上述ARN链接的SNS中设置的地址发送电子邮件。不过,我希望电子邮件中包含第一个SNS收到的完整、理想格式的JSON。我在另一个文件中定义了Cloudwatch JSON结构,它由var event Cloudwatch Alarm
任何指针都会很棒
谢谢 来自AWS SDK的Go文档:
type PublishInput struct {
// The message you want to send.
//
// If you are publishing to a topic and you want to send the same message to
// all transport protocols, include the text of the message as a String value.
// If you want to send different messages for each transport protocol, set the
// value of the MessageStructure parameter to json and use a JSON object for
// the Message parameter.
因此,params
将是:
params := &sns.PublishInput{
Message: myjson, // json data you want to send
TopicArn: aws.String("my arn"),
MessageStructure: aws.String("json")
}
警告
请注意,帮助段落中说“为参数消息使用JSON对象”,这样的JSON对象必须具有与支持的传输协议对应的密钥,这意味着
{
'default': json_message,
'email': json_message
}
它将使用默认传输和电子邮件传输发送
json_消息。可能重复的请参见此处-。SNS只能发送纯文本的电子邮件。将SES用于格式化/HTML电子邮件。感谢您的评论。虽然我理解SNS可能不是发送电子邮件的最佳工具,但问题更多地围绕着如何将JSON数据推送到任何地方。迁移到SES仍然会导致同样的问题,我不知道如何发布Lambda接收到的JSON数据。说实话,我可能最终会将SNS发送给OpsGenie,OpsGenie可以处理JSON blob的接收。如果OpsGenie公开了一个类似webhooks的HTTP端点,那么您可以直接使用它而不是电子邮件。这将是理想的。您是否知道如何将从SNS接收到的数据推送到Lambda,再推送到连接到Ops Genie的SNS,SNS配置部分不是问题,只是代码需要如何处理JSON的发送?谢谢您的回复!我试图实现上述功能,但遇到了一个问题,我希望您在输入“myjson”时能提供帮助,在我的情况下,我试图使用“snsRecord”或“snsRecord.Message”我收到了错误无法将snsRecord(type events.SNSEntity)用作字段值中的type*字符串
和无法使用snsRecord.Message(type string)在构建时分别作为字段值中的type*string
。知道这是什么原因吗?