douzhong3038 2018-09-19 09:57
浏览 91

从SNS接收到的JSON转发到Lambda-GoLang

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 
", 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 
", 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

Any pointers would be great!

Thanks!

  • 写回答

1条回答 默认 最新

  • drmy1050 2018-09-19 20:29
    关注

    From AWS SDK for Go docs:

    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.
    

    So, params would be:

    params := &sns.PublishInput{
            Message: myjson, // json data you want to send
            TopicArn: aws.String("my arn"),
            MessageStructure: aws.String("json")
        }
    

    WARNING

    Notice the help paragraph says "use a JSON object for the parameter message", such JSON object must have a keys that correspond to supported transport protocol That means

    {
      'default': json_message,
      'email': json_message
    }
    

    It will send json_message using both the default and email transports.

    评论

报告相同问题?

悬赏问题

  • ¥15 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao