Making use of Amazon SNS

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:
Now unzip the tool with
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


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

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 /etc/init.d/

Next step is to create a symbolic link:

cd /etc/rc2.d
sudo ln -s ../init.d/

Now update the rc scripts so that the link fires upon server startup:
sudo update-rc.d 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.

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in AWS, cloud and tagged , . Bookmark the permalink.

One Response to Making use of Amazon SNS

  1. Pingback: Set up AWS Autoscaling « The Pragmatic Integrator

Comments are closed.