{"id":552,"date":"2019-06-11T10:17:08","date_gmt":"2019-06-11T01:17:08","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2019\/06\/11\/send_result_of_AWS_Systems_Manager_Run_Command_To_slack"},"modified":"2022-10-28T22:36:44","modified_gmt":"2022-10-28T13:36:44","slug":"send_result_of_aws_systems_manager_run_command_to_slack","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2019\/06\/11\/send_result_of_aws_systems_manager_run_command_to_slack\/","title":{"rendered":"AWS Systems Manager Run Command\u306e\u5b9f\u884c\u7d50\u679c\u3092Slack\u306b\u901a\u77e5\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"

\u3069\u3046\u3082\u30017\u6708\u4e2d\u65ec\u304b\u30891~3\u30f6\u6708\u306e\u30ec\u30f3\u30b8\u3067\u30db\u30fc\u30c1\u30df\u30f3\u3067\u30ea\u30e2\u30fc\u30c8\u30ef\u30fc\u30af\u3092\u3059\u308b\u4e88\u5b9a\u306e\u305f\u3081\u3001\u4eca\u304b\u3089\u3068\u3066\u3082\u697d\u3057\u307f\u306a\u53e4\u5ddd\u3067\u3059\u3002<\/p>\n

\u4eca\u56de\u306f\u30bf\u30a4\u30c8\u30eb\u306e\u901a\u308a\u3001AWS Systems Manager Run Command\u306e\u5b9f\u884c\u7d50\u679c\u3092Slack\u306b\u901a\u77e5\u3059\u308b\u65b9\u6cd5\u3092\u5171\u6709\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

\u51e6\u7406\u30d5\u30ed\u30fc<\/h2>\n

AWS Systems Manager Run Command -> Amazon SNS -> AWS Lambda -> Slack<\/p>\n

\u524d\u63d0<\/h2>\n
    \n
  1. AWS Systems Manager Run Command\u3092\u5b9f\u884c\u3059\u308b\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u30a6\u30a3\u30f3\u30c9\u30a6\u3092\u4f5c\u6210\u6e08\u307f<\/li>\n
  2. AWS Systems Manager Run Command\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306f AWS-RunPatchBaseline<\/code> \u306e\u60f3\u5b9a\u3068\u3059\u308b<\/li>\n
  3. Slack\u3078\u306e\u901a\u77e5\u306fIncoming Webhook<\/a>\u3067\u4f5c\u6210\u3057\u305f Webhook URL<\/code> \u3092\u5bfe\u8c61\u306b\u884c\u3044\u3001\u65e2\u306b\u8a2d\u5b9a\u304c\u5b8c\u4e86\u3057\u3066\u3044\u308b\u3082\u306e\u3068\u3059\u308b<\/li>\n
  4. AWS Lambda\u306fRuby\u3067\u4f5c\u6210\u3059\u308b<\/li>\n<\/ol>\n

    \u4f5c\u696d\u306e\u5927\u307e\u304b\u306a\u6d41\u308c<\/h2>\n
      \n
    1. Amazon SNS\u306e\u30c8\u30d4\u30c3\u30af\u4f5c\u6210<\/li>\n
    2. Slack\u901a\u77e5\u306e\u51e6\u7406\u3092\u62c5\u5f53\u3059\u308bAWS Lambda\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7<\/li>\n
    3. AWS Systems Manager Run Command\u306bAmazon SNS\u3092\u95a2\u9023\u3065\u3051\u308b<\/li>\n<\/ol>\n

      \u5b9f\u4f5c\u696d<\/h2>\n

      1. Amazon SNS\u306e\u30c8\u30d4\u30c3\u30af\u4f5c\u6210<\/h3>\n

      1-1. IAM\u30ed\u30fc\u30eb\u306e\u4f5c\u6210<\/h4>\n

      URL<\/a>\u306e\u300c\u30bf\u30b9\u30af 1: Amazon SNS \u901a\u77e5\u306e IAM \u30ed\u30fc\u30eb\u3092\u4f5c\u6210\u3059\u308b\u300d\u3001\u300c\u30bf\u30b9\u30af 2: iam:PassRole \u30dd\u30ea\u30b7\u30fc\u3092 Amazon SNS \u30ed\u30fc\u30eb\u306b\u30a2\u30bf\u30c3\u30c1\u3059\u308b\u300d\u306b\u5f93\u3044IAM\u30ed\u30fc\u30eb\u3092\u4f5c\u6210\u3002<\/p>\n

      \u5f8c\u8ff0\u306e\u300c4. AWS Systems Manager Run Command\u306bAmazon SNS\u3092\u95a2\u9023\u3065\u3051\u308b\u300d\u5185\u3067\u4f7f\u7528\u3059\u308b\u60f3\u5b9a\u3002<\/p>\n

      \u4e0a\u8a18\u624b\u9806\u306b\u3088\u308a\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u8a2d\u5b9a\u306eIAM\u30ed\u30fc\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u3002<\/p>\n

      \"\"
      \n\"\"<\/p>\n

      \u203b \u30a4\u30f3\u30e9\u30a4\u30f3\u30dd\u30ea\u30b7\u30fc\u304c\u4f5c\u6210\u3055\u308c\u3066\u3044\u308b\u304b\u8981\u78ba\u8a8d\u3002<\/p>\n

      1-2. \u30c8\u30d4\u30c3\u30af\u306e\u4f5c\u6210<\/h4>\n

      Amazon SNS\u7ba1\u7406\u30b3\u30f3\u30bd\u30fc\u30eb\u4e0a\u3067\u30c8\u30d4\u30c3\u30af\u306e\u4f5c\u6210\u3092\u30af\u30ea\u30c3\u30af\u3002<\/p>\n

      \"\"<\/p>\n

      \u540d\u524d\u3092\u5165\u529b\u3057\u3066\u4fdd\u5b58\u3002<\/p>\n

      \"\"<\/p>\n

      2.AWS Lambda\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7<\/h3>\n

      2-1. AWS Lambda\u3092\u4f5c\u6210<\/h4>\n

      \u30e9\u30f3\u30bf\u30a4\u30e0\u3092Ruby2.5\u3068\u3057\u3001Lambda\u3092\u4f5c\u6210\u3002<\/p>\n

      \"\"<\/p>\n

      \u4f5c\u6210\u3057\u305fLambda\u306e\u300c\u30b3\u30fc\u30c9\u30a8\u30f3\u30c8\u30ea\u30bf\u30a4\u30d7\u300d\u3092\u300c\u30b3\u30fc\u30c9\u3092\u30a4\u30f3\u30e9\u30a4\u30f3\u3067\u7de8\u96c6\u300d\u306b\u3057\u3066\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092 main.rb<\/code> \u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u540d\u3067\u4f5c\u6210\u3002\u30cf\u30f3\u30c9\u30e9\u3092\u300c main.lambda_handler<\/code> \u300d\u3068\u3059\u308b\u3002<\/p>\n

      \"\"<\/p>\n

      \u30b3\u30fc\u30c9<\/h5>\n
      require 'uri'\nrequire 'net\/http'\nrequire 'json'\n\ndef color_by(status)\n  color = ""\n  case status\n  when "Failed"\n    color = "danger"\n  when "Success"\n    color = "good"\n  when "InProgress"\n    color = "#16B0FF" #skyblue\n  else\n    color = "warning"\n  end\n  color\nend\n\ndef make_notification_text(subject, msg)\n  content = {\n    "Subject"           => subject,\n    "CommandID"         => msg["commandId"],\n    "Status"            => msg["status"],\n    "DocumentName"      => msg["documentName"],\n    "InstanceID"        => msg["instanceId"],\n    "RequestedDatetime" => msg["requestedDateTime"],\n    "EventTime"         => msg["eventTime"]\n  }\n\n  text = ""\n  content.each do |k, v|\n    text << "*#{k}:*\n#{v}\n\n"\n  end\n  text\nend\n\ndef lambda_handler(event:, context:)\n  puts event\n  event["Records"].each do |record|\n    msg = JSON.parse(record["Sns"]["Message"])\n\n    # HTTPS\u3067\u901a\u4fe1\u3059\u308b\u305f\u3081\u306e\u8a2d\u5b9a\n    uri = URI.parse(ENV['SLACK_HOOK_URL'])\n    http = Net::HTTP.new(uri.host, uri.port)\n    http.use_ssl = true\n    http.verify_mode = OpenSSL::SSL::VERIFY_NONE\n    req = Net::HTTP::Post.new(uri.path, initheader = { 'Content-Type' => 'application\/json' })\n\n    # attachments\u3092\u30ad\u30fc\u3068\u3059\u308b\u3053\u3068\u3067Slack\u306e\u901a\u77e5\u30e1\u30c3\u30bb\u30fc\u30b8\u306bcolor\u3092\u30bb\u30c3\u30c8\n    # \u53c2\u8003: https:\/\/api.slack.com\/docs\/message-attachments\n    req.body = { "attachments": [{ "text" => make_notification_text(record["Sns"]["Subject"], msg), "color" => color_by(msg["status"]) }] }.to_json\n    res = http.request(req)\n    puts "Response #{res.code} #{res.message}: #{res.body}"\n  end\nend<\/code><\/pre>\n

      2-2. \u74b0\u5883\u5909\u6570\u306e\u8a2d\u5b9a<\/h4>\n

      Lambda\u306e\u74b0\u5883\u5909\u6570\u8a2d\u5b9a\u306b\u3066\u74b0\u5883\u5909\u6570\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002<\/p>\n

      SLACK_HOOK_URL<\/code> \u3068\u3044\u3046\u30ad\u30fc\u3067 Incoming Webhook<\/code> \u3067\u4f5c\u6210\u3057\u3066\u3042\u308b Webhook URL<\/code> \u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002<\/p>\n

      \"\"<\/p>\n

      2-3. Amazon SNS\u306e\u30c8\u30ea\u30ac\u30fc\u3092\u8ffd\u52a0<\/h4>\n

      AWS Lambda\u306bAmazon SNS\u306e\u30c8\u30ea\u30ac\u30fc\u3092\u8ffd\u52a0\u3002
      \n\u8a2d\u5b9a\u3059\u308bARN\u306f\u300c1. Amazon SNS\u306e\u30c8\u30d4\u30c3\u30af\u4f5c\u6210\u300d\u306e\u624b\u9806\u3067\u4f5c\u6210\u3057\u305fAmazon SNS\u306e\u30c8\u30d4\u30c3\u30af\u306e\u3082\u306e\u3068\u3059\u308b\u3002<\/p>\n

      \"\"<\/p>\n

      2-4. \u5b9f\u884c\u30ed\u30fc\u30eb\u306e\u8a2d\u5b9a<\/h4>\n

      AWSLambdaBasicExecutionRole<\/code> \u306e\u30dd\u30ea\u30b7\u30fc\u304c\u30a2\u30bf\u30c3\u30c1\u3055\u308c\u305f\u30ed\u30fc\u30eb\u3092\u300c\u5b9f\u884c\u30ed\u30fc\u30eb\u300d\u306b\u3066\u9078\u629e\u3002<\/p>\n

      \"\"<\/p>\n

      3. AWS Systems Manager Run Command\u306bAmazon SNS\u3092\u95a2\u9023\u3065\u3051\u308b<\/h3>\n

      \u65e2\u306b\u4f5c\u6210\u3055\u308c\u3066\u3044\u308bAWS Systems Manager\u306e\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u30a6\u30a3\u30f3\u30c9\u30a6\u3088\u308a\u64cd\u4f5c\u3002<\/p>\n

      \u2193\u306e\u3088\u3046\u306b\u5bfe\u8c61\u306e\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u30a6\u30a3\u30f3\u30c9\u30a6\u306e\u300c\u30bf\u30b9\u30af\u300d\u3088\u308a\u2460\u3001\u2461\u3068\u9806\u756a\u306b\u30af\u30ea\u30c3\u30af\u3057\u7de8\u96c6\u753b\u9762\u3078\u79fb\u52d5\u3002<\/p>\n

      \"\"<\/p>\n

      \u7de8\u96c6\u753b\u9762\u4e0b\u90e8\u306b\u2193\u306e\u7de8\u96c6\u9805\u76ee\u304c\u3042\u308b\u306e\u3067\u305d\u308c\u305e\u308c\u5024\u3092\u5165\u529b\u3057\u4fdd\u5b58\u3059\u308b\u3002<\/p>\n

      \u2460 - \u30c1\u30a7\u30c3\u30af\u3092\u3057\u6709\u52b9\u5316\u3002
      \n\u2461 - \u524d\u8ff0\u306e\u300c1. Amazon SNS\u306e\u30c8\u30d4\u30c3\u30af\u4f5c\u6210\u300d\u306e\u300c1-1. IAM\u30ed\u30fc\u30eb\u306e\u4f5c\u6210\u300d\u3067\u4f5c\u6210\u3057\u305fIAM\u30ed\u30fc\u30eb\u3092\u9078\u629e\u3002
      \n\u2462 - \u524d\u8ff0\u306e\u300c1. Amazon SNS\u306e\u30c8\u30d4\u30c3\u30af\u4f5c\u6210\u300d\u306e\u300c1-2. \u30c8\u30d4\u30c3\u30af\u306e\u4f5c\u6210\u300d\u3067\u4f5c\u6210\u3057\u305f\u30c8\u30d4\u30c3\u30af\u306eARN\u3092\u5165\u529b\u3002<\/p>\n

      \"\"<\/p>\n

      \u4ee5\u4e0a\u306e\u8a2d\u5b9a\u304c\u5b8c\u4e86\u6b21\u7b2c\u3001AWS Systems Manager Run Command\u306e\u5b9f\u884c\u7d50\u679c\u304c\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5f62\u5f0f\u3067Slack\u3078\u3068\u901a\u77e5\u304c\u884c\u304f\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002
      \n\u30b9\u30c6\u30fc\u30bf\u30b9\u3054\u3068\u306b\u8272\u304c\u5909\u308f\u308b\u306e\u3067\u4e00\u898b\u3057\u3066\u308f\u304b\u308a\u3084\u3059\u304f\u3066\u826f\u3044\u3067\u3059\u306d\u3002<\/p>\n

      \"\"
      \n\"\"
      \n\"\"
      \n\"\"<\/p>\n","protected":false},"excerpt":{"rendered":"

      \u3069\u3046\u3082\u30017\u6708\u4e2d\u65ec\u304b\u30891~3\u30f6\u6708\u306e\u30ec\u30f3\u30b8\u3067\u30db\u30fc\u30c1\u30df\u30f3\u3067\u30ea\u30e2\u30fc\u30c8\u30ef\u30fc\u30af\u3092\u3059\u308b\u4e88\u5b9a\u306e\u305f\u3081\u3001\u4eca\u304b\u3089\u3068\u3066\u3082\u697d\u3057\u307f\u306a\u53e4\u5ddd\u3067\u3059\u3002 \u4eca\u56de\u306f\u30bf\u30a4\u30c8\u30eb\u306e\u901a\u308a\u3001AWS Systems Manager Run Command\u306e\u5b9f\u884c\u7d50\u679c\u3092Slack\u306b\u901a\u77e5\u3059\u308b\u65b9\u6cd5\u3092\u5171\u6709\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 \u51e6\u7406\u30d5\u30ed\u30fc AWS Systems Manager Run Command -> Amazon SNS -> AWS Lamb […]<\/p>\n","protected":false},"author":1,"featured_media":684,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[399],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/552"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=552"}],"version-history":[{"count":1,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/552\/revisions"}],"predecessor-version":[{"id":3290,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/552\/revisions\/3290"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}