This topic describes how to create a preemptible instance by using the Elastic Compute Service (ECS) console, calling an API operation, or using Terraform.
Usage Notes
To create and use a preemptible instance, take note of the following items:
Make a reasonable bid.
When you make a bid, take into account fluctuations of market prices. A reasonable bid price increases your chances of creating a preemptible instance and reduces the chances of releasing the instance whose bid price becomes lower than market prices. You must submit the bid price based on the requirements of your workloads.
NoteIf you are unable to determine a bidding price for your preemptible instances, we recommend that you use the market price at the time of purchase as the bid price.
Use an image that contains the configurations of the required software to ensure that the instance can be started after it is created. You can also use user data of the instance to run commands when you start the instance. For more information, see Customize initialization configurations for an instance.
We recommend that you store important data in storage media that are not affected by the release of preemptible instances.
Storage media include separately created cloud disks, Object Storage Service (OSS) buckets, and ApsaraDB RDS instances.
Break your tasks down into smaller tasks by using grids, Hadoop, or a queue-based architecture, or use checkpoints to save calculation results.
Check the instance release notifications from ECS to monitor the status of a preemptible instance.
ECS updates the instance metadata 5 minutes before ECS releases a preemptible instance. You can obtain the status of a preemptible instance every minute by checking instance metadata. For more information, see Overview of ECS instance metadata.
Run your applications on a pay-as-you-go instance and release the instance.
You can verify whether your applications can automatically modify configurations when the instance is released.
Procedure
In the ECS console
Go to the ECS instance buy page in the ECS console.
Click the Custom Launch tab.
Configure the settings as prompted based on your business requirements.
Take note of the following parameters. For information about other parameters, see Create an instance on the Custom Launch tab.
Billing Method: Select Preemptible Instance.
Instance Usage Duration:
1 Hour: Specify a 1-hour protection period for the preemptible instance. The instance is not automatically released within 1 hour after the instance is created. After the period ends, the system checks the inventory and market price of the instance type every 5 minutes to determine whether to release the instance.
None: Specify no protection period for the preemptible instance. Preemptible instances without a protection period are more cost-effective than preemptible instances with a protection period.
Highest Price per Instance:
Use Automatic Bid: The market price at the time of purchase is used as the bid price.
Set Maximum Price: You must specify the maximum price at which you are willing to pay for the instance type.
Before you click Create Order to create the preemptible instance, check the instance configurations such as the instance usage duration to make sure that all configurations meet your requirements.
Read and select ECS Terms of Service and Product Terms of Service, and click Create Order.
Call an API operation
You can use developer tools such as Alibaba Cloud CLI, OpenAPI Explorer, and Alibaba Cloud SDKs to call the RunInstances operation to create a preemptible instance. For more information, see RunInstances.
You can set the SpotStrategy parameter to SpotAsPriceGo to use the market price at the time of purchase as the bid price. You also can set the SpotStrategy parameter to SpotWithPriceLimit and specify a maximum price of the preemptible instance for which you are willing to pay.
You can call the DescribeSpotPriceHistory operation to query historical prices of preemptible instances. This helps you set an appropriate maximum price of a preemptible instance for which you are willing to pay.
Use Terraform
The following sample code provides an example on how to create a preemptive instance by using Terraform. You can also click One-click execution to run the following sample code.
Prerequisites
The Alibaba Cloud account has all permissions on resources. To prevent the misuse of the permissions due to an AccessKey pair leakage, we recommend that you create a Resource Access Management (RAM) user and grant the required permissions to access the AccessKey pair to the RAM user based on the principle of least privilege. For more information, see Create a RAM user and Create an AccessKey pair.
The following sample code provide an example on how to create a custom policy attached to the RAM user. The RAM user has the permissions to start ECS instances, view ECS instance details, and check historical prices of preemptive instances. For more information, see Create custom policies.
{
"Version": "1",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecs:RunInstances",
"ecs:DescribeInstances",
"ecs:DescribeSpotPriceHistory"
],
"Resource": "*"
}
]
}
Use one of the following methods to prepare the Terraform runtime:
Use Terraform in Terraform Explorer: An online runtime environment is provided by Alibaba Cloud, without the need for installation. You can log on and user Terraform. This method is suitable for scenarios in which you want to quick use and default Terraform features in a cost-free manner.
Cloud Shell: Terraform is pre-installed and has pre-provisioned identity credentials. This allows you to run Terraform commands in Cloud Shell. This method is suitable for scenarios in which you want to quick use and default Terraform features at low cost.
Install and configure Terraform: This method is suitable for the scenario with poor network connectivity or the scenario in which a custom development environment is required.
Prepare resources
alicloud_vpc: creates a virtual private cloud (VPC).
alicloud_vswitch: creates a vSwitch in the VPC to which the instances belong.
alicloud_security_group: creates a security group named default and add it to the VPC you created.
alicloud_instance: creates an ECS instance.
alicloud_security_group_rule: adds security group rules to the security group that you created.
Procedure
Open your browser and enter https://shell.aliyun.com in the address bar to access Cloud Shell.
For more information about how to use Cloud Shell, see Use Cloud Shell.
Log on to Cloud Shell.
Create a directory named terraform to organize your Terraform resources.
Run the following command to access the project directory:
cd terraform
Run the following code snippet to create a configuration file named main.tf.
provider "alicloud" {
region = var.region
}
# The region ID.
variable "region" {
type = string
default = "cn-beijing"
}
# The VPC name.
variable "vpc_name" {
type = string
default = "tf_test_fofo"
}
# The CIDR block of the VPC.
variable "vpc_cidr_block" {
type = string
default = "172.16.0.0/12"
}
# The CIDR block of the vSwitch.
variable "vswitch_cidr_block" {
type = string
default = "172.16.0.0/21"
}
# The zone ID.
variable "availability_zone" {
type = string
default = "cn-beijing-b"
}
# The security group name.
variable "security_group_name" {
type = string
default = "default"
}
# The instance type.
variable "instance_type" {
type = string
default = "ecs.n2.small"
}
# The type of the system disk.
variable "system_disk_category" {
type = string
default = "cloud_efficiency"
}
# The operating system image.
variable "image_id" {
type = string
default = "ubuntu_140405_64_40G_cloudinit_20161115.vhd"
}
# The instance name.
variable "instance_name" {
type = string
default = "test_fofo"
}
# The Internet bandwidth.
variable "internet_max_bandwidth_out" {
type = number
default = 10
}
# The billing method.
variable "instance_charge_type" {
type = string
default = "PostPaid"
}
# The bidding strategy of the preemptible instance.
variable "spot_strategy" {
type = string
default = "SpotAsPriceGo"
}
# The protection period of the preemptible instance.
variable "spot_duration" {
type = number
default = 0
}
# The port range in the inbound security group rule.
variable "port_range" {
type = string
default = "1/65535"
}
# The priority of the inbound security group rule.
variable "priority" {
type = number
default = 1
}
# The CIDR in the inbound security group rule.
variable "cidr_ip" {
type = string
default = "0.0.0.0/0"
}
resource "alicloud_vpc" "vpc" {
name = var.vpc_name
cidr_block = var.vpc_cidr_block
}
resource "alicloud_vswitch" "vsw" {
vpc_id = alicloud_vpc.vpc.id
cidr_block = var.vswitch_cidr_block
availability_zone = var.availability_zone
}
resource "alicloud_security_group" "default" {
name = var.security_group_name
vpc_id = alicloud_vpc.vpc.id
}
resource "alicloud_instance" "instance" {
availability_zone = var.availability_zone
security_groups = [alicloud_security_group.default.id]
instance_type = var.instance_type
system_disk_category = var.system_disk_category
image_id = var.image_id
instance_name = var.instance_name
vswitch_id = alicloud_vswitch.vsw.id
internet_max_bandwidth_out = var.internet_max_bandwidth_out
instance_charge_type = var.instance_charge_type
spot_strategy = var.spot_strategy
spot_duration = var.spot_duration
}
resource "alicloud_security_group_rule" "allow_all_tcp" {
type = "ingress"
ip_protocol = "tcp"
nic_type = "intranet"
policy = "accept"
port_range = var.port_range
priority = var.priority
security_group_id = alicloud_security_group.default.id
cidr_ip = var.cidr_ip
}
Run the
terraform init
command to initialize the Terraform configuration.The following sample command output is returned.
Run the
terraform apply
command. Enteryes
as prompted and press the Enter key. Wait for the command to finish. If the following command output appears, the preemptible instance is created as expected.The following sample command output is returned.
Verify the result
Run the terraform show command
To view the details of the instance created by Terraform, run the following command:
terraform show
Verify in the ECS console
Log on to the ECS console and view the preemptible instance that you created.
Release resources
When you no longer require the resources created or managed by Terraform, you can release the resources. To release them, run the terraform destroy command. For more information about the terraform destroy
command, see Common commands.
terraform destroy
Example
You can also click One-click execution to run the following sample code.
Sample code
References
For information about Terraform, see What is Terraform?