简体   繁体   English

如何通过SNS和Cloudwatch发送短信?

[英]How to send SMS through SNS and Cloudwatch?

I am trying to send SMS to my Mobile when my EC2 instance stops. 我尝试在EC2实例停止时将SMS发送到我的手机。

  1. I am automatically stopping my EC2 instance and now I want to send SMS to my mobile when it stops. 我会自动停止EC2实例,现在我想在停止时将SMS发送到我的手机。
  2. I created SNS topic with my mobile no. 我用手机号码创建了SNS主题。 as subscriber. 作为订户。
  3. I created an Alarm when the EC2 stops. EC2停止时我创建了一个警报。
  4. Under SNS > Mobile > Text messaging (SMS) > Text messaging preferences (Edit): 在SNS>移动>文本消息(SMS)>文本消息首选项(编辑)下:

a. 一种。 I selected "Default message type" as "Transactional". 我选择“默认消息类型”作为“事务”。

b. I created a new IAM role. 我创建了一个新的IAM角色。

IAM role policy IAM角色政策

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "logs:PutMetricFilter",
                "logs:PutRetentionPolicy"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

SNS topic access policy SNS主题访问策略

{
  "Version": "2008-10-17",
  "Id": "__default_policy_ID",
  "Statement": [
    {
      "Sid": "__default_statement_ID",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": [
        "SNS:Publish",
        "SNS:RemovePermission",
        "SNS:SetTopicAttributes",
        "SNS:DeleteTopic",
        "SNS:ListSubscriptionsByTopic",
        "SNS:GetTopicAttributes",
        "SNS:Receive",
        "SNS:AddPermission",
        "SNS:Subscribe"
      ],
      "Resource": "arn:aws:sns:us-west-2:account-id:sns-topic-name",
      "Condition": {
        "StringEquals": {
          "AWS:SourceOwner": "account-id"
        },
        "ArnLike": {
          "AWS:SourceArn": "arn:aws:cloudwatch:us-west-2:account-id:alarm:*"
        }
      }
    }
  ]
}

When the alarm is triggered, I am getting the below error: 触发警报时,出现以下错误:

{
  "actionState": "Failed",
  "stateUpdateTimestamp": 1561102479560,
  "notificationResource": "arn:aws:sns:us-west-2:account-id:sns-topic-name",
  "publishedMessage": null,
  "error": "Resource: arn:aws:cloudwatch:us-west-2:account-id:alarm:alarm-name is not authorized to perform: SNS:Publish on resource: arn:aws:sns:us-west-2:account-id:sns-topic-name"
}

I am unable to understand what permission is it expecting. 我无法理解它需要什么许可。

The cause of the error is most likely due to the policy having incorrect values. 该错误的原因很可能是由于该策略的值不正确。 I'm not sure which values you changed to protect sensitive values, but you'd need to update sns-topic-name and account-id . 我不确定您更改了哪些值来保护敏感值,但是您需要更新sns-topic-nameaccount-id

However, I would recommend another way of achieving your goals... 但是,我建议您实现目标的另一种方式...

You can use Amazon CloudWatch Events to look out for a specific event (eg an instance changing state to Stopped) and have it send a message to Amazon SNS directly (without using an Alarm). 您可以使用Amazon CloudWatch Events查找特定事件(例如,实例将状态更改为“已停止”),并使其直接向Amazon SNS发送消息(不使用警报)。

The steps are: 这些步骤是:

  • In the Amazon CloudWatch console, click Rules 在Amazon CloudWatch控制台中,单击规则
  • Create rule 建立规则
  • Service name: EC2 服务名称: EC2
  • Event type: EC2 Instance State-change Notification 事件类型: EC2实例状态更改通知
  • Specific state(s): Stopped 特定状态:已停止
  • Choose Any instance or Specific instance Id(s) 选择任何实例特定实例ID
  • On the right, under Targets , click Add target 在右侧的“ 目标”下,单击“ 添加目标”
  • SNS topic SNS主题
  • Select your topic 选择你的话题

CloudWatch Events-创建规则

This will then send a message whenever the instance stops. 然后,实例停止时,它将发送一条消息。

It seems the error is due to missing permissions on your IAM role for publishing messages to an SNS topic. 似乎该错误是由于您的IAM角色缺少将消息发布到SNS主题的权限所致。 Make arrangements to attach necessary permissions to the role you use or to the user, like this: 安排将必要的权限附加到您使用的角色或用户身上,如下所示:

 {
  "Id": "Policy1415489375392",
  "Statement": [
    {
      "Sid": "AWSConfigSNSPolicy20150201",
      "Action": [
        "SNS:Publish"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:sns:region:account-id:myTopic",
      "Principal": {
        "AWS": [
          "account-id1",
          "account-id2",
          "account-id3",
        ]
      }
    }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM