Making use of Amazon SNS

2 Jan

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@54.246.90.116

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-1.0.3.3/* /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

cd
nano sns.sh

and put the following in the script

#!/bin/bash
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
START=`date`
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
./sns.sh

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

cd
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.

One Response to “Making use of Amazon SNS”

Trackbacks/Pingbacks

  1. Set up AWS Autoscaling « The Pragmatic Integrator - 18/01/2013

    [...] is it. After a few minutes you will receive mails from the SNS we set up when an instance was booted and you will see two instances in the EC2 Console: And both instances are registered with our Load [...]

Comments are closed.

Follow

Get every new post delivered to your Inbox.

Join 105 other followers

%d bloggers like this: