【问题标题】:Send SNS notifications to Slack向 Slack 发送 SNS 通知
【发布时间】:2019-03-26 09:55:02
【问题描述】:

我想将 SNS 通知发送到 Slack。我在我的电子邮件中收到通知。它看起来像:

Instance: i-0f9606e41cd6f1e8e has changed state
State: running
Type: c5.4xlarge
Public IP Address: 52.32.193.26
Private IP Address: 10.10.75.168
Region: us-west-2a
Name: VOSaaS-Cluster-SaaS-Longevity-055ba27d-f7c4-b70a-0954-a08ae21ccb2d-vos-node-i-0f9606e41cd6f1e8e

但我也想在我的 Slack 频道中接收相同的输出。我已经设置了传入的 webhook,我可以接收简单的消息,但是在发送输出时遇到了问题。

MY_SNS_TOPIC_ARN = 'arn:aws:sns:us-west-2:421572644019:CloudWatchAlarmsForSpotInstances'
sns_client = boto3.client('sns')
ec2_spot_info = sns_client.publish(
    TopicArn = MY_SNS_TOPIC_ARN,
    Subject = 'EC2 Spot Instances Termination Notifications',
    Message =   'Instance: ' + instance_id + ' has changed state\n' +
                'State: ' + instance['State']['Name'] + '\n' +
                'Type: ' + instance['InstanceType'] + '\n' +
                'Public IP Address: ' + instance['PublicIpAddress'] + '\n' +
                'Private IP Address: ' + instance['PrivateIpAddress'] + '\n' +
                'Region: ' + instance['Placement']['AvailabilityZone'] + '\n' +
                'Name: ' + name
)

slack_url='https://hooks.slack.com/services/+token'
slack_msg = {
                "attachments": [
                    {
                        "title": "EC2 Spot Instance Info",
                        "pretext": "EC2 Spot Instances Termination Notifications",
                        "color": "#ed1717",
                        "text": ec2_spot_info
                    }
                ]
            }
output = json.dumps(slack_msg)
r = requests.post(slack_url, data = output)

【问题讨论】:

    标签: amazon-web-services aws-lambda amazon-sns slack-api


    【解决方案1】:

    sns_client.publish() 调用返回以下响应:

    {
        'MessageId': 'string'
    }
    

    但您的 slack 命令正在将其作为消息发送:

    "text": ec2_spot_info
    

    这意味着,您发送的不是向 slack 发送消息,而是发送包含 MessageId 的字典。

    相反,您应该:

    • message构造为变量
    • 致电sns_client.publish()Message = message
    • 使用"text": message调用slack

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多