# How to Launch Spot Instances with Linode Cloud Hosting and Ruby ### Introduction A tutorial for VPS CRUD with [Linode Cloud Hosting](https://www.linode.com/) and Ruby. As a Rails developer have you ever wondered how to launch and delete cloud instance on demand with Ruby? In this post I will take you through how you can do a CRUD(Create, Read, Update, Delete) of a VPS or SSD Cloud server hosted on [Linode](https://www.linode.com/). ### Where to start? To implement a VPS CRUD you need to have a Linode account and obtain a API key from profile API keys section and use this obtained API key in Linode API client [gem](https://github.com/rick/linode) to create API client. Install linode client [gem](https://github.com/rick/linode) ```sh gem install linode ``` Once you install this gem, instantiate a API client as ```ruby API_KEY = 'xyz' @client = Linode.new(api_key: API_KEY) ``` ### Prerequisites Before you proceed there are some prerequisites you need to know and have them configured ### One or More Template/Source Linodes, Which Will Be Used While Spawning/Creating Instances by Cloning * Source Linode is a instance with all your necessary setup. * This source Linode has running Rails application or scripts that can be executed remotely or services you need to trigger remotely. * Once you launch source linodes and deploy tested code on these instances next thing you need to do is ‘Power Off’ these source linodes. * This is very important to note that if you want to clone a new Linode from source Linode then source Linode instance needs to be in ‘Powered off’ state. * Also there is a limit of 5 active clone operations per source/template Linode. * I would recommend to have one source Linode per region/datacenter if you need more that one Linode and keep all in one group something like ‘Source Linodes’. * Once you have list of source linodes, configure list of source Linode ids with location as following which will be used in clone operation. ```ruby #List of powered off instances to be cloned from SOURCE_LINODES = { london: 5679, newark: 1234 } ``` ### Configure List of Linode Data Centers You Needed * You need to give a [data center](https://www.linode.com/api/utility/avail.datacenters) id while cloning a instance . * Essentially this is the location where your new instance will be spawned/created. * Linode offers 8 data centers. ```ruby #List of available data centers and data center ids DATACENTERS = { newark: 6, london: 7 } ``` ### Configure Linode Plan Id * You need to give a plan id while cloning a instance. * Linode offers various [plans](https://www.linode.com/api/utility/avail.linodeplans) for instances. * Configure the best plan as per your needs. ```ruby #Linode plan id used while launching new instances PLAN_ID = 1 ``` Once you configure above all then we are set to do a CRUD of instances. ### Let's Do CRUD of Instances ### Creating and Booting an Instance Launching and Booting Linode using source linodeid, datacenterid and planid. ```ruby #Launching instance @linode = @client.linode.clone( linodeid: SOURCE_LINODES[:london], datacenterid: DATACENTERS[:london], planid: PLAN_ID ) #Booting instance @linode.boot(linodeid: @linode.linodeid) ``` ### Updating an Instance Updating Linode label, display group by linodeid ```ruby #Updating instance @client.linode.update( linodeid: @linode.linodeid, label: 'Instance' + '-' + @linode.linodeid.to_s, lpm_displaygroup: 'My Linodes' ) ``` ### Listing All Instances Listing all instances using instantiated linode client ```ruby #Listing instances @client.linode.list #[#