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 firstname.lastname@example.org
sudo apt-get update sudo apt-get install openjdk-6-jre-headlessAnd when it is finished check that it installed okay by running the command
This shoud give the following result:
Besides Java we also need the unzip tool to extract the SNS tool download. To install it simply run:
sudo apt-get install unzipNext step is to install the SNS tools. Download it to the machine with the following command:
unzip SimpleNotificationServiceCli-2010-03-31.zipI am going to install the tool in the ‘/opt/amazonSNS’ directory:
sudo mkdir /opt/amazonSNS sudo mv SimpleNotificationServiceCli-22.214.171.124/* /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.cnfYou can find the necessary credentials in your AWS console page under ‘Account’->’Security Credentials’ and add them to the credential file as follows:
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:
Now create a script by entering the command
cd nano sns.shand 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.shYou can run the script to see if it works with the command
./sns.shThe 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.shNow 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.