This topic describes how to create a preemptible instance by using the Elastic Compute Service (ECS) console, by calling an API operation, or by using Terraform.
Prerequisites
- An Alibaba Cloud account is created and its AccessKey pair is obtained.
When you use Terraform, the AccessKey pair of an Alibaba Cloud account is required. For more information about how to obtain an AccessKey pair, see Obtain an AccessKey pair.
- Terraform is installed and configured.
For more information, see Install and configure Terraform in the local PC and Use Terraform in Cloud Shell.
- The version of the Alibaba Cloud provider alicloud is upgraded to 1.198.0.
Background information
- Specify a suitable bidding price based on estimated market price fluctuations. A suitable bidding price can increase the probability of creating a preemptible instance and ensure that the instance is not released due to small changes in price. You must submit the bidding price based on the requirements of your workloads. Note If 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 bidding 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 Overview of ECS instance user data.
- We recommend that you store important data in storage mediums that are not affected by the release of preemptible instances. Storage mediums include separately created cloud disks, Object Storage Service (OSS) buckets, or ApsaraDB RDS instances.
- Break your jobs down into smaller tasks by using grids, Hadoop, or 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 five 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 to verify whether your applications can automatically modify configurations when the instance is released.
Procedures
Create a preemptible instance in the ECS console
To create a preemptible instance in the ECS console, perform the following steps. For more information about how to configure an instance, see Create an instance by using the wizard.
- Log on to the ECS console.
- In the left-side navigation pane, choose .
- On the Instances page, click Create Instance.
- Set Billing Method to Preemptible Instance.
- In the Usage Duration section, specify a protection period for the preemptible instance.
- One Hour: Specify a protection period for the preemptible instance. Currently, the period can be set only to one hour. More options are about to be available in later updates. After the protection period is elapsed, changes in your bidding price and the inventory of your selected resource are checked every five minutes to determine whether you can continue using the resources. This option is suitable for scenarios such as large-scale rendering tasks and simulations.
- 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.
- In the Maximum Price for Instance Type section, specify your bidding price.
The preemptible instance that you request is created at the market price only if your bidding price is higher than or equal to the market price and the resources are sufficient. You can bid for a preemptible instance only once. The following bidding modes are supported:
- Use Automatic Bid: The market price at the time of purchase is used as the bidding price.
- Set Maximum Price: You must specify the maximum price that you can pay for the instance type. Note In the displayed price range, the maximum price is equal to the price for the pay-as-you-go instance of the same instance type. Your bidding price must be specified based on the displayed price range, your business requirements, and the estimated price fluctuations. If you determine all estimated price fluctuations before you specify the bidding price for a preemptible instance, the instance can be retained for an extended period of time. If you do not determine all estimated price fluctuations, the instance may be released at any time after the protection period is elapsed.
- Select or enter the number of instances that you want to purchase.
- Configure other parameters.
- Confirm the order information and click Create Order.
Create a preemptible instance by calling an API operation
Create a preemptible instance by using Terraform
To create a preemptible instance by using Terraform, perform the following steps:
- Open your browser and enter https://shell.aliyun.com in the address bar to access Cloud Shell.
For more information, see Use Cloud Shell.
- Log on to Cloud Shell.
- Run the
vim terraform.tf
command to create the terraform.tf file. - Enter the following content in the terraform.tf file and enter
wq
to save configurations.Note For information about parameters in the following sample code, see alicloud_instance.provider "alicloud" {} resource "alicloud_vpc" "vpc" { name = "tf_test_foo" cidr_block = "172.16.0.0/12" } resource "alicloud_vswitch" "vsw" { vpc_id = alicloud_vpc.vpc.id cidr_block = "172.16.0.0/21" availability_zone = "cn-hangzhou-b" } resource "alicloud_security_group" "default" { name = "default" vpc_id = alicloud_vpc.vpc.id } resource "alicloud_instance" "instance" { # The zone in which you want to create the preemptible instance. availability_zone = "cn-hangzhou-b" # The security group with which you want to associate the preemptible instance. security_groups = alicloud_security_group.default.*.id # The instance type of the preemptible instance. instance_type = "ecs.n2.small" # The category of the system disk. system_disk_category = "cloud_efficiency" # The operating system of the image that you want to use to create the preemptible instance. image_id = "ubuntu_140405_64_40G_cloudinit_20161115.vhd" # The name of the preemptible instance. instance_name = "test_foo" # The vSwitch to which you want to connect the preemptible instance. vswitch_id = alicloud_vswitch.vsw.id # The public bandwidth of the preemptible instance. internet_max_bandwidth_out = 10 # The billing method of the preemptible instance. instance_charge_type = "PostPaid" # The bidding policy of the preemptible instance. spot_strategy = "SpotAsPriceGo" # The protection period of the preemptible instance. spot_duration = 0 } resource "alicloud_security_group_rule" "allow_all_tcp" { type = "ingress" ip_protocol = "tcp" nic_type = "intranet" policy = "accept" port_range = "1/65535" priority = 1 security_group_id = alicloud_security_group.default.id cidr_ip = "0.0.0.0/0" }
- Run the
terraform init
command to initialize the configurations. - Run the
terraform plan
command to preview the configurations. - Run the
terraform apply
command to create the preemptible instance. - Run the
terraform show
command to check the preemptible instance.The following sample command output indicates that the preemptible instance is created.