Over a million developers have joined DZone.

Making Use of Amazon SNS

DZone 's Guide to

Making Use of Amazon SNS

· Cloud Zone ·
Free Resource

In my previous post I showed how to setup an Amazon SNS topic and subscription. In this post I will integrate this service with my EC2 server so it will send out a mail when the instance is started. This will be handy when we start to use autoscaling so instances might be started ‘automatically’ for different reasons but I will describe that later. Currently we have a EC2 server running Debian Linux and a SNS topic. Now lets see how we can combine these two to get the desired result. To do this we are going to make use of SNS command-line tools.
First step is to login via SSH at the EC2 server with the command (remember I setup a elastic IP so I can make use of that here):
ssh -i 4synergy_palma.pem bitnami@

Then we have to install the Java runtime environment which is used by the command line tool. To do this enter the command
sudo apt-get update
sudo apt-get install openjdk-6-jre-headless
And when it is finished check that it installed okay by running the command
 java -version.

This shoud give the following result:
Screen Shot 2012-12-22 at 14.04.35
Besides Java we also need the unzip tool to extract the SNS tool download. To install it simply run:
 sudo apt-get install unzip
Next step is to install the SNS tools. Download it to the machine with the following command:
wget http://sns-public-resources.s3.amazonaws.com/SimpleNotificationServiceCli-2010-03-31.zip
Now unzip the tool with
 unzip SimpleNotificationServiceCli-2010-03-31.zip
I am going to install the tool in the ‘/opt/amazonSNS’ directory:
sudo mkdir /opt/amazonSNS
sudo mv SimpleNotificationServiceCli-* /opt/amazonSNS/
chmod +x /usr/lib/amazonSNS/bin/*
The command line tool uses a credential file to communicate with Amazon. In this file you have to supply your AccesKey and SecretKey. To create this file copy the template file and edit it:
cd /usr/lib/amazonSNS/
sudo cp credential-file-path.template credentials.cnf
sudo nano credentials.cnf
You can find the necessary credentials in your AWS console page under ‘Account’->’Security Credentials’ and add them to the credential file as follows:
Screen Shot 2012-12-23 at 13.50.42 Now I will create a simple shell script to test the installation. In the script we need the ARN of our SNS topic so first select that in the SNS overview page:
Screen Shot 2012-12-23 at 14.01.43
Now create a script by entering the command
nano sns.sh
  and put the following in the script
export AWS_SNS_HOME=/usr/lib/AmazonSNS
export AWS_CREDENTIAL_FILE=$AWS_SNS_HOME/credentials.cnf
export JAVA_HOME=/usr
export PATH=$PATH:/usr/lib/AmazonSNS/bin
sns-publish arn:aws:sns:eu-west-1:024658091597:WorpressServerNotificaction --message --message "A WordPress EC2 instance is started at $START" --subject "AWS EC2 Alert"
After saving the script make the script executable with:
 sudo chmod +x sns.sh
You can run the script to see if it works with the command
The final step is to make the script part of the startup scripts. We do this by modifying the runlevel script. First we move the script we just made to the /init.d directory
sudo mv sns.sh /etc/init.d/
Next step is to create a symbolic link:
cd /etc/rc2.d
sudo ln -s ../init.d/sns.sh S99sns.sh
Now update the rc scripts so that the link fires upon server startup:
sudo update-rc.d sns.sh enable 2
That is it. Now reboot the machine and you should receive a message when it is up again. As said before this implementation will be very useful when we implement auto-scaling because we will be aware of unexpected server launches. How to do that I will describe in the next post.



Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}