For a upcoming Ruby project that I plan on working on I want to be able to send small messages to myself with information from a form that gets filled out. Amazon has just updated Simple Notification Service to be able to send SMS messages along with emails. This fits in perfectly with what I’m looking for. Looking on Amazon’s Github page for the aws-sdk gem I didn’t see any sample code for using SNS and googling I couldn’t find any right away. As I needed to learn this anyways I picked to make my own sample code which I hope to get a pull request in soon to have it on the official repository. After looking at the documentation and the source code for the library I was able to work this out.

require File.expand_path(File.dirname(__FILE__) + '/../samples_config')
# Topic's arn can be found in AWS Management, under Topic Details.
(topic_arn, message) = ARGV
unless topic_arn && message
puts "Usage: upload.rb <TOPIC_ARN> <MESSAGE>"
exit 1
end
# Get an instance of the SNS interface using the default configuration
sns = AWS::SNS.new
# Find the topic by using the topic's arn
t = sns.topics[topic_arn]
puts "Now publishing to '#{t.name}' topic the following message:"
puts "\"#{message}\""
# Publish the message to the topic
t.publish(message)
puts "\nMessage published"
view raw publish.rb hosted with ❤ by GitHub

If you see a way to make this better please feel free to post here or fork the Gist with your updates.