scripts: add SNS publishing wrapper (bug 1379559) r?gps draft
authorbyron jones <glob@mozilla.com>
Mon, 17 Jul 2017 14:05:07 +0800
changeset 11374 cf98a840aafe1b04e016486faa6fb3e185208926
parent 11373 293da19054d5f8da74846455f435852b880503b4
child 11375 e0ab42a3d9215dc282b55cb11238c05abbc66eba
push id1733
push userbjones@mozilla.com
push dateWed, 19 Jul 2017 07:17:46 +0000
reviewersgps
bugs1379559
scripts: add SNS publishing wrapper (bug 1379559) r?gps Add a wrapper around aws-cli that publishes to SNS. This wrapper exists to support passing in the message via STDIN, and removes the requirement to specify the AWS region explicitly (as it can be extracted from the topic's ARN). MozReview-Commit-ID: EYc1L8JYD9r
scripts/publish-to-sns
new file mode 100755
--- /dev/null
+++ b/scripts/publish-to-sns
@@ -0,0 +1,49 @@
+#!/usr/bin/env python2
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+# Simple wrapper around aws-cli to publish messages to a SNS topic.
+#
+# This wrapper exists to support passing in the message via STDIN, and removes
+# the requirement to specify the AWS region explicitly (as it can be extracted
+# from the topic's ARN).
+#
+# Requires AWS credentials to already be configured in the environment
+# (environmental vars, ~/.aws/credentials, instance profile credentials, etc).
+
+import argparse
+import sys
+import subprocess
+
+# Parse arguments.
+
+parser = argparse.ArgumentParser()
+parser.add_argument('--topic-arn',
+                    required=True,
+                    help='ARN of SNS topic to publish to.')
+parser.add_argument('--subject',
+                    required=True,
+                    help='Email subject.')
+parser.add_argument('--message',
+                    help='Message to publish.')
+args = parser.parse_args()
+
+if not args.message:
+    args.message = sys.stdin.read()
+
+# Extract region from topic ARN.
+
+arn = args.topic_arn.split(':')
+if len(arn) < 6 or not args.topic_arn.startswith('arn:aws:sns:'):
+    raise Exception('"%s" is not a valid AWS SNS ARN' % args.topic_arn)
+
+# Publish, consuming and discarding the JSON response.
+
+subprocess.check_output([
+    'aws', 'sns', 'publish',
+    '--topic-arn', args.topic_arn,
+    '--region', arn[3],
+    '--subject', args.subject,
+    '--message', args.message,
+])