About praveencastelino


Quick tip on MySQL dump

Terminal command to dump MySQL data: 
mysqldump -u -p > /path/to/sqldump/db_database.sql

eg: mysqldump -uroot -padmin123 db_sports > /home/ec2-user/db_sports.sql

Import sql dump:
mysql -u -p < /path/of/sqldump/db_database.sql

ex: mysql -uroot -padmin123 db_sports < /home/ec2-user/db_sports.sql

AWS S3 permissions

A quick tip on aws S3 permissions to a specific folder in a bucket.

"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"Resource": [

Try to avoid bucket root level permission as much as possible. Also, try to use cognito if the client wants to directly access aws resources.

Validating digits session on server side to build secure apps

Digits is a  service from twitter which helps you onboard your mobile app users with simple phone number authentication and it’s for free 🙂

If you have a mobile app that onboard the user using digits service and you want to validate the session generated by digits on your server side then this blog is for you.

The app communicates with the server using REST APIs. Send the token and secret generated by the twitter to the server using the REST API. Twitter provides verify credentials API to validate the sessions generated by Digits at the server side.

Dive into the server side component developed using node.js
Fill in all the details related to digits.
var token = "";
var token_secret = "";
var consumer_key = "";
var consumer_secret = "";
var oauth =
consumer_key: consumer_key
, consumer_secret: consumer_secret
, token: token
, token_secret: token_secret

Make the verify_credentials API call to validate the session information.
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
} else {
console.log("error: ", error);
var request = require('request');
var url = "https://api.twitter.com/1.1/account/verify_credentials.json";
request.get({ url: url, oauth: oauth }, callback);

Note: I have used request package to make the HTTP requests from the server.

Monitoring Memory Utilisation of aws EC2 instance in CloudWatch

Connect to your EC2 instance using ssh.
ssh -i ec2-user@my_ec2_ip_address

Install following perl packages
sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https

Download the monitoring scripts
curl http://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.1.zip -O

Install the monitoring scripts
unzip CloudWatchMonitoringScripts-1.2.1.zip
rm CloudWatchMonitoringScripts-1.2.1.zip
cd aws-scripts-mon

run the following command to check the memory utilisation.
./mon-get-instance-stats.pl --recent-hours=12

In order to push this data periodically to cloud watch, we need to create an IAM user with relevant permissions, then schedule a cron job to periodically push the data to aws cloud watch.

Create a new IAM user and provide him access to the ec2 instance and cloud watch. Make a note of the aws credentials i.e. access key and secret key. You need this later. Make sure the IAM user has following access.


For the purpose of the demo, you can provide full access to EC2 and CloudWatch (This is not recommended for production.)

run the following command
cp awscreds.template awscreds.con

open the file  awscreds.conf and enter the key & secret.
The content of the file should look something like this


Run the following command to push the data to cloud watch
./mon-put-instance-data.pl --mem-util --mem-used --mem-avail

Now, configure the cron tab to automate this process at eveny 5 minutes interval.
crontab -e
*/5 * * * * ~/aws-scripts-mon/mon-put-instance-data.pl --mem-util --disk-space-util --disk-path=/ --from-cron

In your aws console, go to cloud watch section, and select Metrics => custom namespaces => Linux System. Select the required metrics.



Detailed documnetation from aws can be found here

Installing node.js on amazon EC2 instance

Launch an EC2 instance of type Amazon Linux AMI from your aws console.

SSH into your ec2 instance
ssh -i ec2-user@my_ec2_ip_address

Update the instance
sudo yum update -y

#install developer tools
sudo yum groupinstall -y "Development Tools"

Install the node using nvm as it allows you to switch between any version of the node 🙂
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install 4.4.5
node -e "console.log('Running Node.js ' + process.version)"

Install pm2
npm install pm2 -g --save

pull the source code in ec2 instance. run your node app using pm2
pm2 start app.js --name="api"

Setting git on aws EC2 instance

Launch an EC2 instance of type Amazon Linux AMI from your aws console.

SSH into your ec2 instance
ssh -i ec2-user@my_ec2_ip_address

Update the instance
sudo yum update -y

install developer tools
sudo yum groupinstall -y "Development Tools"

install git
sudo yum install git

checkout the source code
git clone https://my.git.repo.git
cd my_local_git_folder
git checkout -f branch_to_cjeckout

Setting up redis on aws EC2 instance

Launch an EC2 instance of type Amazon Linux AMI from your aws console.

SSH into your ec2 instance
ssh -i ec2-user@my_ec2_ip

Update the instance
sudo yum update -y

Install the developer tools to compile the redis source
sudo yum groupinstall -y "Development Tools"

Install tcl
sudo yum install -y tcl

Download the latest redis source
wget http://download.redis.io/redis-stable.tar.gz

unzip the source
tar xvzf redis-stable.tar.gz

go to the redis folder and run the make command to compile the source
cd redis-stable
make test
sudo make install

to run the server, go to the src folder and run the daemonize command.
cd src
./redis-server --daemonize yes

Run the redis-cli command to check if server resonds.

If the response is PONG then you are good to go 🙂

Setting up MySQL server on aws EC2 instance

Launch an EC2 instance of type Amazon Linux AMI from your aws console.

SSH into your ec2 instance
ssh -i ec2-user@my_ec2_ip_address

Update the instance
sudo yum update -y

Install the mysqld server
sudo yum install -y mysql55-server

start the mysqld instance
sudo service mysqld start

the following command ensure launches mysqld on server restart
sudo chkconfig mysqld on

run the following command to set password for root user and delete test databases.
sudo mysql_secure_installation

make a note of the root password 🙂

Let’s try to create a user and database. This way we can control the database access levels.
mysql -uroot -pmy_root_password

I’m going to create a db_demo with demo_user having password demo123.
USE db_demo;
CREATE USER 'demo_user'@'localhost' IDENTIFIED BY 'demo123';
CREATE USER 'demo_user'@'%' IDENTIFIED BY 'demo123';

That’s it. You are all set.


  • details on mysql privileges can be found here
  • Don’t forget to open the default port 3306 if you want to access the database outside the ec2 instance boundary
  • useful mysqld commands
    • sudo service mysqld start
    • sudo service mysqld stop
    • sudo service mysqld restart
    • sudo service mysqld status

Useful Bitwise tips

When it comes to maintaining state of an entity, bitwise operator plays an important role.Consider that you are developing a game and your character can have jump, walk, fire and run states. The characters can be in more than 2 states at any point of time.You can store this information easily using bitwise operators.

Define states of the player.

typedef enum {
eNone = 0, (0000 0000)
eRunning =1, (0000 0001)
eJumping =2, (0000 0010)
eWalking =4, (0000 0100)
eShooting =8 (0000 1000)

Consider you want to set the player state to Running, you can do this by ORing.

0000 0000
0000 0001     (eRunning)
------------  (Bitwise OR)
0000 0001

You can set another state like Shooting.

0000 0001     (Current state)
0000 1000     (eShooting)
------------  (Bitwise OR)
0000 1001

We have captured both the states of the player.
To query the state ‘x’ of the player, you simply need to AND it.
To check if the player is Running or not,

0000 1001     (Current State)
0000 0001     (eRunning)
-----------   (Bitwise AND)
0000 0001

To reset any state, you need complement the state and then AND it.
To reset ‘Shooting’ state,

Complement the Shooting state.

0000 1000 -> 1111 0111 (complement)

Now AND it. This will reset only the ‘Shooting’ state and retain all the other states of the player.

0000 1001     (Current State)
1111 0111
-----------   (Bitwise AND)
0000 0001

These are the convenient methods.

_playerSate = _playerSate|state;

_playerSate = _playerSate & (~state);

BOOL isSet = _playerSate & state;
return isSet;

Connecting to Amazon EC2 server on Mac using Terminal app

Go to the folder where your .pem file is stored. You can retrieve the .pem file from your amazon account.

Launch the terminal app and run the  command

cd /folder/where/perm/file/stored/
ssh-add filename.pem

This should work fine most of the time and you should get a response similar to this.

Identity added:xxxxx

If the permission set on the .pem file aren’t correct then terminal will show this error

Permissions 0444 for 'filename.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
ec2-user@xx.xx.xx.xx:22: No such file or directory

To fix it, set the appropriate permissions.

chmod 400 filename.pem

Now, it’s time to connect to ec2 server.

ssh-add filename.pem
ssh ec2-user@xx.xx.xx.xx:22

If evrything works fine, then you should see this response on your terminal screen:

Last login: Tue Apr 23 03:34:27 2013 from xx.xx.xx.xx
__| __|_ )
_| ( / Amazon Linux AMI
There are 15 security update(s) out of 141 total update(s) available
Run "sudo yum update" to apply all updates.
Amazon Linux version 2013.03 is available.
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8)
[ec2-user@ip-xx-xx-xx-xx ~]$