Update cookbook after EC2 instance restart


#1

Hello I am using an Amazon EC2 instance to go through the Chef tutorial. If I restart the EC2 instance I get a new IP Address. How do I update the chef node with the new IP address so that I can upload changes to my cookbook. For example when I execute the following command,

knife ssh ‘name:node2-rhel’ sudo chef-client --ssh-user ec2-user --identity-file …\ChefTutorial.pem --attribute cloud.public_hostname

I get the following error,

WARNING: Failed to connect to ec2-52-55-19-236.compute-1.amazonaws.com – Net::SSH::ConnectionTimeout: Net::SSH::ConnectionTimeout

The address it is trying to connect to is the original address amazon assigned not the new one assigned after rebooting my instance.

Thanks in advance
Mark


#2

Right. “Restarting” an EC2 instance, in the sense of logging into it and
typing “reboot”, does not do this. Stopping in AWS, then starting, the EC2
instance does this, You’re asking the chef server to look in its known
configuratons for IP information that has never been reported to it,
because chef has not been run since the new IP was assigned.

The simplest approach may be to set up an init script to run chef on every
reboot. I do this myself for various services, such as MySQL, where
duplicate hosts built from the same AWS image must have a unique
server-id for MySQL slaves, which I base on the assigned IP address for
consistency. Other approaches could involve using EC2 “tags” to identify
such a particular host or set of hosts, rather than relying on chef to work
its way around DNS the way that “knife ssh” is effectively doing for you,
and run “aws” commands to report the IP address or addresses of hosts with
the relevant tag for these one-off operations, and go run “chef” on that
host to get it into chef.

Not in AWS, but in other environments, I’ve left regular cron jobs running
to report discrepancies between the list of hosts in chef and the list of
hosts in virtualization, to help keep track of obsolete or confusing nodes.
It can be useful for keeping chef uncluttered, especially in dynamic
environments.


#3

Thanks, I’ve solved this by setting up an elastic ip and associating it to my EC2 instance. That way I always get the same IP. I suspect that is the simplest thing to do for Chef beginners who want to go through the tutorials using nodes in AWS.

The Chef folks may want to add that to the tutorial instructions.


#4

I’m glad that is working for you. Yes, that will preserve the exposed IP
address. It also costs money. See
https://aws.amazon.com/premiumsupport/knowledge-center/elastic-ip-charges/