best way to bundle update on server while booting - ruby

I have an AMI which has configured with production code setup.I am using Nginx + unicorn as server setup.
The problem I am facing is, whenever traffic goes up I need to boot the instance log in to instance and do a git pull,bundle update and also precompile the assets.Which is time consuming.So I want to avoid all this process.
Now I want to go with a script/process where I can automate whole deployment process, like git pull, bundle update and precompile as soon as I boot a new instance from this AMI.
Is there any best way process to get this done ? Any help would be appreciated.

You can place your code in /etc/rc.local (commands in this file will be executed when server will be loaded).
But the best way is using (capistrano). You need to add require "capistrano/bundler" to your deploy.rb file, and bundle update will be runned automatically. For more information you can read this article: https://semaphoreapp.com/blog/2013/11/26/capistrano-3-upgrade-guide.html

An alternative approach is to deploy your app to a separate EBS volume (you can still mount this inside /var/www/application or wherever it currently is)
After deploying you create an EBS snapshot of this volume. When you create a new instance, you tell ec2 to create a new volume for your instance from the snapshot, so the instance will start with the latest gems/code already installed (I find bundle install can take several minutes). All your startup script needs to do is to mount the volume (or if you have added it to the fstab when you make the ami then you don't even need to do that). I much prefer scaling operations like this to have no dependencies (eg what would you do if github or rubygems have an outage just when you need to deploy)
You can even take this a step further by using amazon's autoscaling service. In a nutshell you create a launch configuration where you specify the ami, instance type, volume snapshots etc. Then you control the group size either manually (through the web console or the api) according to a fixed schedule or based on cloudwatch metrics. Amazon will create or destroy instances as needed, using the information in your launch configuration.

Related

How to save a compute engine VM-instance setup in Google Cloud Platform?

I have created a compute engine VM-instance in Google Cloud Platform. Then I have installed go using the standard procedure downloading it from https://dl.google.com/go/go1.11.4.linux-amd64.tar.gz. Everything worked properly and I was able to run go application. However, after closing the instance when I reopened it, it says go is not installed. The message is following.
-bash: go: command not found
How can I save the instance setup?
Creating, Deleting, and Deprecating Custom Images
You can create custom images of boot disks and use these images to create new instances. This is ideal for situations where you have created and modified a persistent boot disk to a certain state and need to save that state to create new instances.
I think you should also consider using Docker containers.
Pushing and pulling images
Container-optimized VM images

Foreman finish template is not getting resolve when user-data of the image is enabled

I am using Foreman 1.6 and using AWS EC2 as compute resource.
Problem is, Foreman could not able to resolve the finish template when the user-data of image is enabled. And, I could not able to provision the VM.
When user-date of image is disabled, foreman able to resolve the finish-template and able to provision the vm (Without reading the template, i.e puppet client installation).
Could you guide me where I am going wrong? Its been two week I am struggling with this issue.
Thanks,
Sekhar
You need to create a new provisioning script of type "user-data" (or just use the "Kickstart default user data" and associate it to your OS. Finish scripts are not the right "kind" for cloud-init.

How to update new version of application in a running Ubuntu EC2 machine programmatically

How can I update the new versions of application in my ubuntu machines in private subnet which are in Autoscaling. I am using cloudfromation script to bring up the entire set up. Can I include any script in cloudfromation template to do this? Please help.
There are a few ways to accomplish the upgrade. Many people use amazon's provided script, cfn-hup. The way I do it is different and is as follows:
When instance launches, have script install application from files fetched from S3
Update S3 with new version.
Use a script(or manually) to shutdown instances one at a time, waiting for autoscaling to bring them up with the new version installed.

AWS autoscale and server code updates / editing

Say I've got a PHP site on an ec2 instance in an autoscale group. The group has 10 running instances. I want to add some new php code to the site. Is there a sane way to do this and have it populate to all other instances in the autoscaling group?
The only way I can see is to manually bundle an image from the instance, upload it to S3, register the bundle with aws and attach the bundle to the launch config of the autoscale group. I then have to terminate all my instances and launch new ones with the new image.
I would have to do all this every time I want to make a change to the codebase. Surely this is madness.
The best way is NOT to bundle your page into the image. Make the ami download them from s3 on start. In this way you can upload to only one place and call the download script.

EC2 Instance Cloning

Is it possible to clone a EC2 instance data and all?
You can make an AMI of an existing instance, and then launch other instances using that AMI.
The easier way is through the web management console:
go to the instance
select the instance and click on instance action
create image
Once you have an image you can launch another cloned instance, data and all. :)
There is no explicit Clone button. Basically what you do is create an image, or snapshot of an existing EC2 instance, and then spin up a new instance using that snapshot.
First create an image from an existing EC2 instance.
Check your snapshots list to see if the process is completed. This usually takes around 20 minutes depending on how large your instance drive is.
Then, you need to create a new instance and use that image as the AMI.
Nowadays it is even easier to clone the machine with EBS-backed instances released a while ago. This is how we do it in BitNami Cloud Hosting.
Basically you just take a snapshot of the instance which can be used
later to launch a new server. You can do it either using AWS console
(saving the EBS-backed instance as AWS AMI) or using the EC2 API
tools:
create a snapshot with ec2-create-snapshot
and then launch an instance from a snapshot
Cloning the instance is nothing else but creating the backup and then
launching a new server based on that. You can find bunch of articles
out there describing this problem, try to find the info about "how to
..." backup or resize the whole EC2 instance, for example this blog is
a really good place to start: alestic.com
To Answer your question: now AWS make cloning real easy see Launch instance from your Existing Instance
On the EC2 Instances page, select the instance you want to use
Choose Actions --> Image and Templates, and then Launch More Like This.
Review & Launch
This will take the existing instance as a Template for the new once.
or you can also take a snapshot of the existing volume and use the snapshot with the AMI (existing one) which you ping during your instance launch
You can use AWS API or console UI to create an AMI(Amazon Machine Image) of your running instance. You can specify to reboot the instance when create your AMI. Then you can use AWS API or console UI to launch more instances with the AMI you created.
You can do it very easily with a Cloud Management software -like enStratus, RightScale or Scalr (disclaimer: I work there). With the cloned farm you can:
Create a snapshot or a pre-made image to launch another day
Duplicate your configuration to test it before production

Resources