1

I am running my ruby on rails e-commerce application running on couple of ec2 instances in the production. I have not enabled auto scaling since we do Continuous Integration, we do deployment when ever required. I using chef for the all the reasons. I am trying to figure out how to use chef to deploy code to the multiple ec2 instances automcatically and manual intervention. I tried to deploy the code during the initial ec2 instance works fine. But my question is how do I do it in the auto scaling mode so that instance pull the latest code from either github or bit bucket and deployes itseld after bundle updating and all.

1 Answer 1

6

Fair enough. This is what i do and it works fine:

I have created an AMI with below settings:

  1. install chef client

  2. Create /etc/chef directory

  3. Copy your_company-validator.pem from chef workstation to AMI at /etc/chef/validation.pem
  4. Create /etc/chef/client.rb as below.

If you use chef-solo, make the changes accordingly.

cat >> /etc/chef/client.rb <EF
log_level        :auto
log_location     STDOUT
chef_server_url  "https://api.opscode.com/organizations/your_company"
validation_client_name "you_company-validator"
EF

Now, you have all above stuff configured in the AMI for chef bootstrap.

When you launch the AMI (using autoscaling or any other way), provide user-data where in it will run the chef-client with run_list of your choice. I provide below user-data:

#!/bin/bash
cat > /etc/chef/firstboot.json << EOL
{"run_list": ["recipe[java::oracle]"]}
EOL
chef-client -j /etc/chef/firstboot.json > /tmp/initialize_client.log 2>&1

basically, I am creating a .json file where I would specify the recipe/role/run_list I want to run. Once you provide the user-data, it will get executed on the 1st boot and chef-client will be run along with the run_list.

I think this is what you are looking for. so to summarize:

  1. Create an AMI with pre-installed chef-client, client.rb and validation.pem
  2. and then provide user-data at the time of instance launch.

This works perfectly for me. Let me know if this set-up gives you any trouble. I have been using this for quite some time.

So when I launch my instance using autscaling, it gets automatically chef-bootstrapped along with the recipes of my choice.

ASSUMPTION: you have all your recipes/roles created on chef-server before doing this. else the bootstrap will eventually fail.

Sign up to request clarification or add additional context in comments.

7 Comments

Hello @slayedbylucifer sounds amazing!!! I will try it out soon and keep u updated
Hi I tried the process which you suggested but, after booting up ec2 instance it throws an error Failed to authenticate to the chef server (http 401). Server Response: ---------------- Failed to authenticate as 'ip-10-128-94-138.ap-southeast-1.compute.internal'. Ensure that your node_name and client key are correct. If these settings are correct, your client_key may be invalid. How I am suppose to debug it and what would be problem.
when the chef-client runs for the 1st time, it would create client.pem itself. was there any client.pem in your image left from any prior run of chef-client? Also, Do you have your validation.pem key copied on the node ?
I don't understand why create AMIs with chef pre-installed... The "knife bootstrap" command will install chef and automatically configure the /etc/chef directory. Better still you could use the ec2 plugin from knife and additionally spin up the VM.
@Mark O'Connor, knife bootstrap or knife ec2 plugin is no help when you are spinning up instances via AWS autoscalling.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.