Associate elastic ip automatically to a amazon ec2 instance when the instance start

I had amazon ec2 account. I had an instance which associated with a elastic ip . But when I stop and start the instance then the Elastic ip detached from the instance . I have to associate the elastic ip manually each time after starting the instance. Is there any script for auto attach the elastic ip on starting the instance .? Please let me know.


You can run the below command on your instance start up.

  ec2-associate-address -i <instance ID> <elastic IP Address> --region <region name>

A simple approach is to add a line in /etc/rc.local :

 su - USER_FOOBAR -c /PATH/TO/MY_APP &

or if as root :

 /PATH/TO/MY_APP &