{"id":1379,"date":"2019-11-05T13:13:05","date_gmt":"2019-11-05T07:43:05","guid":{"rendered":"https:\/\/opstree.com\/blog\/\/?p=1379"},"modified":"2020-02-24T17:21:11","modified_gmt":"2020-02-24T11:51:11","slug":"perfect-spot-instances-imperfections-part-i","status":"publish","type":"post","link":"https:\/\/opstree.com\/blog\/2019\/11\/05\/perfect-spot-instances-imperfections-part-i\/","title":{"rendered":"Perfect Spot Instance&#8217;s Imperfections | part-I"},"content":{"rendered":"\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"788\" class=\"wp-image-1820\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2019\/11\/ec2spotinstances.png?w=940\" alt=\"\" \/><\/figure>\r\n\r\n\r\n\r\n<p>In this blog I am going to share my opinion on <strong>spot instances<\/strong> and why we should go for it. While I was going thorough the category(<strong>on-demand, reserved, and spot<\/strong>) that AWS provides to launch our instances into, I found spot instances very fascinating and a little challenging.<\/p>\r\n<p><!--more--><\/p>\r\n\r\n\r\n\r\n<p>What I found about spot instances is that they are normal ec2 instances. But what makes it different from the other two(<strong>on-demand, reserved<\/strong>)? What strategy do AWS uses for spot instances to make it <strong>cheaper<\/strong> than other two and why? Let\u2019s know about these first.<\/p>\r\n\r\n\r\n\r\n<p>With AWS continuously expanding their region and Availability Zones in their region, they are left with huge amount of unused capacity. How AWS take advantage of their unused capacity? AWS floats its spare capacity on market on a very low base price and allows us to bid on instances and the person with the highest bidding price is provided with the instance, however the price that person pays is only market price i.e if market price is $1 for t2.micro instance and you places a bid of $2 on t2.micro then you will get that instance but the price you will pay is market price i.e $1 only. Interesting?? Let\u2019s bring more fascinating things by comparing the prices of the all three.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-table\">\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>Discounts<\/strong><\/td>\r\n<td><strong>\u00a0 \u00a0 \u00a0 Types<\/strong><\/td>\r\n<td><strong> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Details<\/strong><\/td>\r\n<\/tr>\r\n<tr>\r\n<td>0%<\/td>\r\n<td>On-demand Instances<\/td>\r\n<td>No commitment from your side.<br \/>You pay the most.<br \/>Costs fixed price per hour.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>40%-60%<\/td>\r\n<td>Reserved Instances<\/td>\r\n<td>1 year or 3 year commitment from your side.<br \/>You save money from that commitment.<br \/>Costs per plan.<\/td>\r\n<\/tr>\r\n<tr>\r\n<td>60%-90%<\/td>\r\n<td>Spot Instances<\/td>\r\n<td>No commitment from AWS side.<br \/>Ridiculously inexpensive.<br \/>Costs based on availability.<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/figure>\r\n\r\n\r\n\r\n<p>With this information you must be thinking to try out spot instances at least once. Since we know that every interesting thing comes with a price, spot instances too have a downside <strong>\u201cAWS can take back spot instances from you anytime\u201d<\/strong>. Upset? Don\u2019t be, cause this blog built with the purpose of overcoming its downside only. After all you won\u2019t mind spending your 5-10 minutes only for saving in dollars.<\/p>\r\n\r\n\r\n\r\n<p><strong>Let\u2019s <\/strong><strong>start<\/strong><strong> then&#8230;<\/strong><\/p>\r\n\r\n\r\n\r\n<p>Now you know that AWS is ready to give their huge spare capacity in the prices of our choice but with a promise to take the capacity back when they want, giving us a warning of two minute before <strong>interruption<\/strong>. We can manage the interruption wisely and the proof is some of the organization are already taking full advantage of the spot instances.<\/p>\r\n\r\n\r\n\r\n<p>Before we go to the core concept let\u2019s build some required concept that will help us to understand core concept with ease.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\"><strong>Spot Instance v\/s Spot <\/strong>Fleet<\/h3>\r\n\r\n\r\n\r\n<p>With normal spot instance request, you place a bid for a specific instance type in anyone or specific Availability Zone and hope you get it.<\/p>\r\n\r\n\r\n\r\n<p>With spot fleets, you can request a number of different instance types that meet your requirements. Additionally, you can spread your spot fleet bid across multiple Availability Zones to increase the likelihood of getting your capacity fulfilled.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\"><strong>Interruption Notice<\/strong><\/h3>\r\n\r\n\r\n\r\n<p>When AWS take our spot instance back they provide interruption notice 2 mins before so that we can perform some actions.<\/p>\r\n\r\n\r\n\r\n<p>Next, it is also necessary to know about <strong>Cloudwatch Rules<\/strong>. AWS provide the event type on the basis of which you can perform actions like triggering lambda function, sending notice over mail or sms etc.<\/p>\r\n\r\n\r\n\r\n<p>One of the event types that you can monitor is <strong>ec2 state change<\/strong> to <strong>running<\/strong>.<\/p>\r\n\r\n\r\n\r\n<p>Now with this much knowledge we are good to go. And I will show you how you can automate the interruption to avoid the risk of downtime.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/GVawdP3MZy_B5-tmDRI0gTJ-q48UlL9i5Mu8n8o290rmw7DwINrkDD03_OpSRqEGG8DT1DHwbxe4r7lqZ9QdXVAAe8edkCQxYMbldwYQO4JnYhDzm_XBgYTmEBC6LhYo6Xq6iysO\" alt=\"\" \/><\/figure>\r\n\r\n\r\n\r\n<p>This is the main diagram stating all the components that are used to automate the interruption. Now suppose one of the spot instances has been interrupted and AWS is going to take that spot\u00a0 instance back. Let\u2019s see what happens then.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/Hn0iQ9E3Xr5FIFnHct_ZBKhCEXQg2NDMv2ZU60f5v98Za2J2NSQ60hwpHcZpZTMASiq5xcSI3bI0DRiiHmlSydrC342yE2wuLHdJlG0YUQHbeqc3VV3UtX6TvuHbi6aCejhnqTtE\" alt=\"\" width=\"715\" height=\"387\" \/><\/figure>\r\n\r\n\r\n\r\n<p>When AWS is to take one spot instance back, AWS will give interruption notice upon which a cloudwatch rule is created to monitor the interruption notice and then;\u00a0<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/IXUIAFvqX6JE3VMu9uCGJQEx5lxke1MM3-k562hIguVbGhDc5IgSv2ITfJiI1YQrIUNN2bjZWvQBTCHKGo5ikbompcrG3EYXwrwgjcSZGAC_5pk6saWmFwlfVRjmTplFfNvppXlc\" alt=\"\" width=\"715\" height=\"387\" \/><\/figure>\r\n\r\n\r\n\r\n<p>Lambda function is triggered. And then;<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/JEWCGmn9KyMDFN8aav2iu1ZP9NI4K-V5KY_qx3zkfET0w02rxMjlbUTRmW6wpgdwenxwtj_xfuissa6t79-Q_RNeH2OEffduoXW4qN1JsuxszQaOHAzcUWKzYYTuByHa3ZgQ5Qu-\" alt=\"\" width=\"715\" height=\"387\" \/><\/figure>\r\n\r\n\r\n\r\n<p>Lambda function increases the desired capacity of Auto scaling group to 1. Due to which an on-demand instance gets launched into Target Group and the interrupted spot instance gets terminated.\u00a0<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/Ne1ck0TZwXhZv-5Yl0decMte6kx79c-Dda5rUP276MocRUSEaOU05GStJYdK2-Z2awFGkL8bs8b_w5PSQ9o_HwwRmm9sbYE_y3SSqtEjmFfra3hYnWRdGZsSBm-O8u2XCtpE-LWI\" alt=\"\" width=\"715\" height=\"387\" \/><\/figure>\r\n\r\n\r\n\r\n<p>Now when on-demand instance is launched and its state changes to running then,<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/TZ5Q3iYnB9wimzh2dgFCRzdTcw0t_y8F7tsua_e_rxrV1lQ5JnSY6IMtxCTINEaWd-a5qH7ARhDfKXA5WNOJpIu4jwZmtBxp2QN3QaY2FZPFRT6lf2X9e_KHz_OH4Tupt2VyEk8e\" alt=\"\" width=\"715\" height=\"387\" \/><\/figure>\r\n\r\n\r\n\r\n<p>Another cloudwatch rule monitor that change and<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/tlUOY0VlsrTeiuJ0tNCKNfcdeYmncCyHs3wO_uSwPfzPWNAcBdezt5ZTI2JLMyz3f17_BFsE9BoQD4Q7tw1b2Q_2gN_NK5wKB_xA6RAnpVT2jquSjqXvznCpWG9ZQtom3dk7bAjq\" alt=\"\" width=\"715\" height=\"387\" \/><\/figure>\r\n\r\n\r\n\r\n<p>Cloudwatch will trigger another lambda function, and then:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/XhUjFDEgnQSTEZgUsopT4v8oU3Qe1hz26_GSfuRTFKPDWlJXbX4us7Ue9flHLGPBmnywhwLPkyR6igOoNvWM9LEPnXpWet8wZmcRCPnsAEUTgAjhLs5LM8yJvL_NFrwNnqiBnkzu\" alt=\"\" width=\"715\" height=\"387\" \/><\/figure>\r\n\r\n\r\n\r\n<p>Lambda function will modify the spot fleet request capacity to 2 which was previously 1, this will launch a spot instance in the same Target Group and now we will have<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/Xh8zLrGj_SWEqXA6iKhkdni0OGqx9t3vf5IxYJDqitG9zsaxJokQqrpXJvgE6EI31DxUbZVnI2nNm1Ew-v4_bp8kt9iFh_HSPvubst_YkrwPSB2OuA6u36NII40AwiZRGyd-82-0\" alt=\"\" width=\"715\" height=\"387\" \/><\/figure>\r\n\r\n\r\n\r\n<p>1 more spot instance is being launched and when its state changes to running\u00a0 then,<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/wFtL6BkV5dWJa_gUbEPxHGIMI4NYb7Ul2sJWjPlc3frWSEC1tlx5IcFwGzU0pYTuMBL_ZtqqAjOdhJahiutPw0wHZYi694f4gR2TTqa5owrGw4w5zwED7YpGz2oQvIB7HHko2dXa\" alt=\"\" width=\"715\" height=\"387\" \/><\/figure>\r\n\r\n\r\n\r\n<p>Again cloudwatch rule comes in action upon state change to running of just launched spot instance. Then<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/fwWc2Adv0Id4fcs3GTPdqyLXWW--GprYSZrZRSiD07Ew88RtQVulvMklzpgS3jDUVtaojFBONywZoHR1c5vQoaVuBXbdEb3dgt8Xm36s7w1y9wgp_YQQux6ZrTLzBceZZEqKdlpU\" alt=\"\" width=\"715\" height=\"387\" \/><\/figure>\r\n\r\n\r\n\r\n<p>This will again trigger associated lambda function and<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/tcp7d3c-gE1OyGwzLbOWiJz2l3B6IoryrF2fayfV2BgXwIqDPq5nnSdCXoHfNzAmkxKuG68UUitsJUS4rnm7-aHelpXZ0zqvuHOXyHViRUB0e-qfSdPD6gTu_7XSeMgYSW-fCRPI\" alt=\"\" width=\"715\" height=\"387\" \/><\/figure>\r\n\r\n\r\n\r\n<p>Lambda function will set the desired capacity of ASG to 0 again due to which the on-demand instance under the target group will get terminated. And finally we will be left with the following:<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/mIiPOipocnGlI_JZ7xa7vaVvq8TBbNKZFrn4z9Jtt3JEpnRt67RYro3AaOekQGIIPV1rJt6p5It5dlWQpi-zPUwE6CTscwwry2HVy9q3V9Da8UOzdDrKAEwULMdJDKk9p4ePPsM4\" alt=\"\" width=\"715\" height=\"387\" \/><\/figure>\r\n\r\n\r\n\r\n<p>Again we are at the same place i.e, 2 spot instances get maintained under the Target Group always.<\/p>\r\n\r\n\r\n\r\n<p><strong>Note:<\/strong> For the purpose of demonstration, I have taken two instances initially, however you can have any number of\u00a0 instances. You can customize this according to your needs and constraint. The whole infra is automated with<strong> terraform<\/strong> which will create and link everything presented above. Link to clone the repo is provided at the second part of this article.<\/p>\r\n\r\n\r\n\r\n<p>Are you excited to implement this concept? I am equally excited to share the real implementation with you. With the next part coming very soon, I want you to try the implementation by yourself. In the second part I will help you to implement the whole concept. See you soon&#8230;<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>In this blog I am going to share my opinion on spot instances and why we should go for it. While I was going thorough the category(on-demand, reserved, and spot) that AWS provides to launch our instances into, I found spot instances very fascinating and a little challenging.<\/p>\n","protected":false},"author":159902151,"featured_media":29900,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[144203,28070474],"tags":[303515361,688840701,688840641,392947966,532068219],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/11\/DevSecOps-1.jpg","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pfDBOm-mf","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/1379"}],"collection":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/users\/159902151"}],"replies":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/comments?post=1379"}],"version-history":[{"count":25,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/1379\/revisions"}],"predecessor-version":[{"id":2382,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/1379\/revisions\/2382"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media\/29900"}],"wp:attachment":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media?parent=1379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/categories?post=1379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/tags?post=1379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}