2

Say I have an app comprised of a web server and a database. If I ec2 deploy a chef client with the DB role, how can I automatically insert the IP of this new ec2 machine into my web server's config?

More generalized, how can I acquire EC2 metadata at launch time for use in attributes of other cookbooks?

I know a roundabout way to do this might be to update a dns server with shell scripts, but I'm interested to here of a solution using only chef.

3 Answers 3

6

You can use a properly formatted search method on the Chef recipes you run on the web server that return the node object for your DB server. Then you can get the aforementioned IPs via node["ipaddress"] οr, if you need the public IP/hostname when running on EC2, node["cloud"]["public_hostname"]/node["cloud"]["public_ipv4"]. For example, after deploying the DB server use something like:

db_servers = search(:node, 'roles:db-server').map{|n| n.attribute?('cloud') ? n['cloud']['public_ipv4'] : n['ipaddress'] }

template "web/config" do
    [...]
    variables({ :dbs_ervers => db_servers })
end

The web server update will occur either asynchronously, whenever the Chef client runs or you can force the run by hand via manual ssh or knife ssh

1
  • If my answer has actually solved your issue, please accept it as solved! Thanks!
    – priestjim
    Jan 8, 2013 at 21:05
1

Chef is just a small bit in your cloud orchestration and cloud monitoring / configuration. You can use chef for configuration and nothing else. It's not for coordination of many servers.

In your case, you need Master Process, to initiate new instance launch, instance will be configured and the Master Process would update the webserver once the db instance is operational.

Many people using Chef do this mistake. Above you have correct solution. Just use it wisely. Use authentication servers too.

3
  • When you say Master Process, is that a general term or a piece of software I don't know of?
    – Jeff V
    Jan 1, 2013 at 0:25
  • Well, you can make Master Process yourself, but there are solutions available as well, but they are more complex, so for a single use, you can make a simple loop, that it will do two things: bootstrap servers and configure them, and another one to monitor them. Then you can use e.g. database queue to control the messages. Basically you have two parts - AWS API scripting (AWS tools) and the Cloud Front (Monitoring), so basically you need to be creative. Jan 1, 2013 at 1:11
  • And this way you dont need to do manually when you change the ip - it's just the matter how much automated you got this Jan 1, 2013 at 1:12
0

You may want to look into AWS CloudFormation templates in combination with Chef. Bootstrap EC2 instances and spin up other AWS resources using CloudFormation i.e. control AWS using CloudFormation. Then use Chef to manage the server configuration side. I believe this is partly the functionality Andrew Smith is referring to by "Master process", the single point that you use to control the entire life-cycle. It definitely replaces AWS API scripts in Andrew's approach.

Code deployment is probably also part of this. Chef can be used for this but I don't believe this is the best way (unless you are doing some this small). Use a code build system such as Jenkins and deployment system. We currently use an in-house system but looking to move to http://octopusdeploy.com/ (as we are primarily a .Net house). Assume that the code in some version control system such as Git.

Useful document:

https://s3.amazonaws.com/cloudformation-examples/IntegratingAWSCloudFormationWithOpscodeChef.pdf

Disclaimer: I'm no expert. Have only recently started working on our deployment design. But the architecture is looking like the above at present.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .