What are Spot Instance and Spot Instance Requests?
A Spot Instance is an instance that uses spare AWS EC2 capacity that is available for less than the On-Demand price. Because Spot Instances provide the ability to request unused EC2 instances at steep discounts, it can lower your Amazon EC2 costs significantly.
Spot Instances are a cost-effective choice if you can be flexible about when your applications run and whether your applications can be interrupted. For example, Spot Instances are well-suited for data analysis, batch jobs, background processing, and optional tasks.
Managing Spot Instances with Ansible
So you want to manage your Spot Instance Requests with Ansible Automation Platform? When it comes to managing AWS resources, the Ansible Amazon AWS Collection includes a variety of Ansible content to help automate the management of AWS instances. Using Ansible to automate applications in AWS greatly increases the chances that your cloud initiative will be a success.
With the latest addition of new modules to the Ansible Amazon AWS Collection, we have introduced two new modules to help manage Spot Instance Requests efficiently.
The ec2_spot_instance module helps in creating as well as terminating the Spot Instance Requests, while it’s companion module, ec2_spot_instance_info helps in gathering details about the Spot Instance Requests.
Let’s Deep Dive:
EC2 Spot Instance Module
The EC2 Spot Instance module helps create and terminate spot instance requests easily with Ansible Playbooks. With help of this module, we can create a simple Spot Instance Request with minimal options, create a Spot Instance Request by providing a complex set of options and remove or terminate the Spot Instance Requests that we no longer need.
To use this module in playbooks, use the amazon.aws.ec2_spot_instance key and provide the options as required for creating the Spot Instance Request.
Consider the following example for creating a simple Spot Instance Request.
The task below creates a simple Spot Instance Request with the provided launch specifications:
- name: Simple Spot Instance Request Creation
amazon.aws.ec2_spot_instance:
launch_specification:
image_id: ami-123456789
key_name: my-keypair
instance_type: t2.mediumWith this new module, we can provide various options while creating Spot Instance Requests. Some of them are mentioned below (to check the complete options, check out the Spot Instance requests documentation from AWS).
block_device_mappings: A list of hash/dictionaries of volumes to add to the new instance.network_interfaces: One or more network interfaces. If you specify a network interface, you must specify subnet IDs and security group IDs using the network interface.placement: The placement information for the instance.monitoring: Indicates whether basic or detailed monitoring is enabled for the instance.spot_price: Maximum spot price to bid. If not set, a regular on-demand instance is requested.tags: A dictionary of key-value pairs for tagging the Spot Instance request on creation.
Example task for creating a Spot Instance Request with the more options:
- name: Spot Instance Request Creation with more options
amazon.aws.ec2_spot_instance:
launch_specification:
image_id: ami-123456789
key_name: my-keypair
instance_type: t2.medium
subnet_id: subnet-12345678
block_device_mappings:
- device_name: /dev/sdc
ebs:
delete_on_termination: True
volume_type: io2
volume_size: 30
- device_name: /dev/sdc
ebs:
volume_type: io2
volume_size: 30
network_interfaces:
- associate_public_ip_address: False
delete_on_termination: True
device_index: 0
placement:
availability_zone: us-west-2a
monitoring:
enabled: False
spot_price: 0.002
tags:
Environment: TestingBut what do we do when we no longer need a Spot Instance Request? How do we terminate it?
An example of terminating the Spot Instance Request:
- name: Spot Instance Request Termination
amazon.aws.ec2_spot_instance:
spot_instance_request_ids: ['sir-12345678', 'sir-abcdefgh']
state: absentEC2 Spot Instance Info Module
To review with the EC2 Spot Instance module, we can now create and terminate Spot Instance Requests. But wait a minute, we have created a few Spot Instance Requests and we would like to check on them and know more details
No problem! With the new EC2 Spot Instance Info module, we have it covered for all needs with the Spot Instance Requests. We can use the new EC2 Spot Instance Info module to get details of our Spot Instance Requests.
An example to gather details of particular Spot Instance Request(s) based on IDs:
- name: describe the Spot Instance requests based on request IDs
amazon.aws.ec2_spot_instance_info:
spot_instance_request_ids:
- sir-12345678
- sir-87654321What if we have so many Spot Instance Requests that we want to filter based on an image ID, state of the request or instance type? You can use one or more filters supported by the module to filter the results as per your requirements.
Some of the filters supported are:
launch.image-id: The ID of the AMI.state: The state of the Spot Instance request (open | active | closed | cancelled | failed). Spot request status information can help you track your Amazon EC2 Spot Instance requests.launch.instance-type: The type of instance (for example, m3.medium).
Check out Describe Spot Instance Requests for more filters.
An example to gather details of Spot Instance Requests and filter the results:
- name: describe the Spot Instance requests based on request IDs
amazon.aws.ec2_spot_instance_info:
spot_instance_request_ids:
- sir-12345678
- sir-87654321Example:
The playbooks below create two Spot Instance Requests, get the details about the requests, and filter the result and terminate the Spot Instance Requests.
To create Spot Instance Requests:
---
- name: test the spot_instance module creation
hosts: localhost
tasks:
- name: Create a Spot Instance Request
amazon.aws.ec2_spot_instance:
launch_specification:
image_id: ami-1234567890
key_name: my-key
instance_type: t3.micro
subnet_id: subnet-1234567890
- name: Create another Spot Instance Request
amazon.aws.ec2_spot_instance:
launch_specification:
image_id: ami-1234567890
key_name: my-key
instance_type: c5.large
subnet_id: subnet-1234567890To get Spot Instance Request and filter results:
---
- name: test the spot_instance module creation
hosts: localhost
tasks:
- name: Create a Spot Instance Request
amazon.aws.ec2_spot_instance:
launch_specification:
image_id: ami-1234567890
key_name: my-key
instance_type: t3.micro
subnet_id: subnet-1234567890
- name: Create another Spot Instance Request
amazon.aws.ec2_spot_instance:
launch_specification:
image_id: ami-1234567890
key_name: my-key
instance_type: c5.large
subnet_id: subnet-1234567890To terminate the Spot Instance Requests:
---
- name: test the new spot_instance_info module
hosts: localhost
tasks:
- name: Get details about spot instance requests
amazon.aws.ec2_spot_instance_info:
spot_instance_request_ids:
- sir-87654321
- sir-12345678
filters:
launch.instance-type: t3.microMoving forward
For further reading and information, visit the other blogs related to Ansible.
Check out the YouTube playlist for everything about Ansible Collections to know more.
- Check out all our E-Books online here.
- Where can we get a trial?
- Want a trial of Ansible Automation Platform?
please visit http://red.ht/try_ansible
- Want a trial of Ansible Automation Platform?
- Where can I learn Ansible?
Are you new to Ansible automation and want to learn? Check out our getting started guide on developers.redhat.com
Sobre o autor
Mais como este
Master modern hybrid cloud security and scale with Red Hat updates
F5 BIG-IP Virtual Edition is now validated for Red Hat OpenShift Virtualization
Technically Speaking | Platform engineering for AI agents
Technically Speaking | Driving healthcare discoveries with AI
Navegue por canal
Automação
Últimas novidades em automação de TI para empresas de tecnologia, equipes e ambientes
Inteligência artificial
Descubra as atualizações nas plataformas que proporcionam aos clientes executar suas cargas de trabalho de IA em qualquer ambiente
Nuvem híbrida aberta
Veja como construímos um futuro mais flexível com a nuvem híbrida
Segurança
Veja as últimas novidades sobre como reduzimos riscos em ambientes e tecnologias
Edge computing
Saiba quais são as atualizações nas plataformas que simplificam as operações na borda
Infraestrutura
Saiba o que há de mais recente na plataforma Linux empresarial líder mundial
Aplicações
Conheça nossas soluções desenvolvidas para ajudar você a superar os desafios mais complexos de aplicações
Virtualização
O futuro da virtualização empresarial para suas cargas de trabalho on-premise ou na nuvem