<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Xi Group Ltd. Company Blog &#187; Xi Group Ltd. Company Blog &#187; spot instances</title>
	<atom:link href="http://blog.xi-group.com/tag/spot-instances/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xi-group.com</link>
	<description>High-quality DevOps Services</description>
	<lastBuildDate>Tue, 09 Jun 2015 11:38:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.2.2</generator>
	<item>
		<title>Small Tip: How to use AWS CLI to start Spot instances with UserData</title>
		<link>http://blog.xi-group.com/2014/07/small-tip-how-to-use-aws-cli-to-start-spot-instances-with-userdata/</link>
		<comments>http://blog.xi-group.com/2014/07/small-tip-how-to-use-aws-cli-to-start-spot-instances-with-userdata/#comments</comments>
		<pubDate>Sat, 12 Jul 2014 18:44:17 +0000</pubDate>
		<dc:creator><![CDATA[Ivo Vachkov]]></dc:creator>
				<category><![CDATA[AWS]]></category>
		<category><![CDATA[DevOps]]></category>
		<category><![CDATA[Operations]]></category>
		<category><![CDATA[Small Tip]]></category>
		<category><![CDATA[AWS CLI]]></category>
		<category><![CDATA[spot instances]]></category>
		<category><![CDATA[UserData]]></category>

		<guid isPermaLink="false">http://blog.xi-group.com/?p=185</guid>
		<description><![CDATA[Common occurrence in the list of daily DevOps tasks is the one to deal with AWS EC2 Spot Instances. They offer the same performance, as the OnDemand counterparts, they are cheap to the extend that user can specify the hourly price. The drawback is that AWS can reclaim them if the market price goes beyond [&#8230;]]]></description>
				<content:encoded><![CDATA[<p style="text-align: justify;">Common occurrence in the list of daily DevOps tasks is the one to deal with <a href="http://aws.amazon.com/ec2/purchasing-options/spot-instances/">AWS EC2 Spot Instances</a>. They offer the same performance, as the OnDemand counterparts, they are cheap to the extend that user can specify the hourly price. The drawback is that AWS can reclaim them if the market price goes beyond the user&#8217;s price. Still, those are key component, a basic building block, in every modern elastic system. As such, DevOps engineers must regularly interact with those.</p>
<p style="text-align: justify;">AWS provides proper command line interface, <a href="http://docs.aws.amazon.com/cli/latest/reference/ec2/request-spot-instances.html">aws ec2 request-spot-instances</a> exposes multiple options to the user. However, some of the common use cases are not comprehensively covered in the documentation. For example, creating Spot Instances with Userdata using the command line tools is somewhat obscure and convoluted, although common need in DevOps and Developers lives. The tricky part: <strong>It must be BASE64 encoded!</strong></p>
<p style="text-align: justify;">Assume the following, simple UserData script, must be deployed on numerous EC2 Spot Instances:</p>
<p></p><pre class="crayon-plain-tag">#!/bin/bash -ex

# Debian apt-get install function
apt_get_install()
{
        DEBIAN_FRONTEND=noninteractive apt-get -y \
        -o DPkg::Options::=--force-confdef \
        -o DPkg::Options::=--force-confold \
        install $@
}

# Mark execution start
echo "STARTING" > /root/user_data_run

# Some initial setup
set -e -x
export DEBIAN_FRONTEND=noninteractive
apt-get update && apt-get upgrade -y

# Install required packages
apt_get_install nginx

# Create test html page
mkdir /var/www
cat > /var/www/index.html << "EOF"
<html>
        <head>
                <title>Demo Page</title>
                </head>

        <body>
                <center><h2>Demo Page</h2></center><br>
                <center>Status: running</center>
        </body>
</html>
EOF

# Configure NginX
cat > /etc/nginx/conf.d/demo.conf << "EOF"
# Minimal NginX VirtualHost setup
server {
        listen 8080;

        root /var/www;
        index index.html index.htm;

        location / {
                try_files $uri $uri/ =404;
        }
}
EOF

# Restart NginX with the new settings
/etc/init.d/nginx restart

# Mark execution end
echo "DONE" > /root/user_data_run</pre><p></p>
<p style="text-align: justify;">Make sure base64 command is available in your system, or use equivalent, to encode the sample userdata.sh file before passing to the launch specification:</p>
<p></p><pre class="crayon-plain-tag">aws ec2 request-spot-instances \
    --spot-price 0.01 \
    --instance-count 2 \
    --launch-specification \
        "{ \
            \"ImageId\":\"ami-a6926dce\", \
            \"InstanceType\":\"m3.medium\", \
            \"KeyName\":\"test-key\", \
            \"SecurityGroups\": [\"test-sg\"], \
            \"UserData\":\"`base64 userdata.sh`\" \
        }"</pre><p></p>
<p style="text-align: justify;">In this example <strong>two</strong> spot instance requests will be created for <strong>m3.medim</strong> instances, using <strong>ami-a6926dce</strong> AMI, <strong>test-key</strong> SSH key, running in <strong>test-sg</strong> Security Group. BASE64-encoded contents of <strong>userdata.sh</strong> will be attached to the request so upon fulfillment the Userdata will be passed to the newly created instances and executed after boot-up.</p>
<p style="text-align: justify;">Spot instance requests will be created in the AWS EC2 Dashboard:</p>
<p><a href="http://blog.xi-group.com/wp-content/uploads/2014/07/Screen-Shot-2014-07-12-at-9.11.20-PM.png"><img src="http://blog.xi-group.com/wp-content/uploads/2014/07/Screen-Shot-2014-07-12-at-9.11.20-PM.png" alt="Screen Shot 2014-07-12 at 9.11.20 PM" width="1240" height="198" class="alignnone size-full wp-image-271 img-thumbnail img-responsive" /></a></p>
<p style="text-align: justify;">Once the Spot Instance Requests (SIRs) are fulfilled, InstanceID will be associated for each SIR:</p>
<p><a href="http://blog.xi-group.com/wp-content/uploads/2014/07/Screen-Shot-2014-07-12-at-9.18.24-PM.png"><img src="http://blog.xi-group.com/wp-content/uploads/2014/07/Screen-Shot-2014-07-12-at-9.18.24-PM.png" alt="Screen Shot 2014-07-12 at 9.18.24 PM" width="1237" height="198" class="alignnone size-full wp-image-272 img-thumbnail img-responsive" /></a></p>
<p style="text-align: justify;">EC2 Instances dashboard will show newly created Spot Instances (notice the &#8220;<strong>Lifecycle: spot</strong>&#8221; in Instance details):</p>
<p><a href="http://blog.xi-group.com/wp-content/uploads/2014/07/Screen-Shot-2014-07-12-at-9.20.30-PM.png"><img src="http://blog.xi-group.com/wp-content/uploads/2014/07/Screen-Shot-2014-07-12-at-9.20.30-PM.png" alt="Screen Shot 2014-07-12 at 9.20.30 PM" width="2286" height="194" class="alignnone size-full wp-image-273 img-thumbnail img-responsive" /></a></p>
<p style="text-align: justify;">Using the proper credentials, one can verify successful execution of the userdata.sh on each instance:</p>
<p></p><pre class="crayon-plain-tag">:~> ssh -i ~/.ssh/test-key.pem ubuntu@ec2-54-211-6-104.compute-1.amazonaws.com "tail /var/log/cloud-init-output.log"
Setting up nginx (1.4.6-1ubuntu3) ...
Processing triggers for libc-bin (2.19-0ubuntu6) ...
+ mkdir /var/www
+ cat
+ cat
+ /etc/init.d/nginx restart
 * Restarting nginx nginx
   ...done.
+ echo DONE
Cloud-init v. 0.7.5 finished at Sat, 12 Jul 2014 18:17:09 +0000. Datasource DataSourceEc2.  Up 76.38 seconds
:~></pre><p></p>
<p style="text-align: justify;">&#8230; and more importantly, if the configured service works as expected:</p>
<p></p><pre class="crayon-plain-tag">:~> curl http://ec2-54-211-6-104.compute-1.amazonaws.com:8080/
<html>
        <head>
                <title>Demo Page</title>
                </head>

        <body>
                <center><h2>Demo Page</h2></center><br>
                <center>Status: running</center>
        </body>
</html>
:~></pre><p></p>
<p style="text-align: justify;">Newly created Spot Instances are serving traffic, running at 0.01 USD/hr and will happily do so until the market price for this instance type goes above the specified price!</p>
<p>References</p>
<ul>
<li><a href="http://docs.aws.amazon.com/cli/latest/reference/ec2/request-spot-instances.html">http://docs.aws.amazon.com/cli/latest/reference/ec2/request-spot-instances.html</a></li>
</ul>
<div class="rpbt_shortcode">
<h3>Related Posts</h3>
<ul>
					
			<li><a href="http://blog.xi-group.com/2015/01/userdata-teplate-for-ubuntu-14-04-ec2-instances-in-aws/">UserData Template for Ubuntu 14.04 EC2 Instances in AWS</a></li>
					
			<li><a href="http://blog.xi-group.com/2015/01/small-tip-how-to-use-aws-cli-filter-parameter/">Small Tip: How to use AWS CLI &#8216;&#8211;filter&#8217; parameter</a></li>
					
			<li><a href="http://blog.xi-group.com/2014/11/small-tip-how-to-use-block-device-mappings-to-manage-instance-volumes-with-aws-cli/">Small Tip: How to use &#8211;block-device-mappings to manage instance volumes with AWS CLI</a></li>
					
			<li><a href="http://blog.xi-group.com/2014/06/small-tip-ebs-volume-allocation-time-is-linear-to-the-size-and-unrelated-to-the-instance-type/">Small Tip: EBS volume allocation time is linear to the size and unrelated to the instance type</a></li>
					
			<li><a href="http://blog.xi-group.com/2015/02/how-to-deploy-single-node-hadoop-setup-in-aws/">How to deploy single-node Hadoop setup in AWS</a></li>
			</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.xi-group.com/2014/07/small-tip-how-to-use-aws-cli-to-start-spot-instances-with-userdata/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
