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

If you want to use Terraform to create a preemptible instance, make sure that the following requirements are met:

Background information

To create and use a preemptible instance, take note of the following items:
  • 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.

  1. Log on to the ECS console.
  2. In the left-side navigation pane, choose Instances & Images > Instances.
  3. On the Instances page, click Create Instance.
  4. Set Billing Method to Preemptible Instance.
  5. 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.
  6. 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.
  7. Select or enter the number of instances that you want to purchase.
  8. Configure other parameters.
  9. Confirm the order information and click Create Order.

Create a preemptible instance by calling an API operation

You can use developer tools such as Alibaba Cloud CLI, OpenAPI Explorer, and Alibaba Cloud SDKs to call the RunInstances operation and create a preemptible instance. For more information, see RunInstances.
Note You can set the SpotStrategy parameter to SpotAsPriceGo to use the market price at the time of purchase. You also can set the SpotStrategy parameter to SpotWithPriceLimit to use a suitable maximum price.

Create a preemptible instance by using Terraform

To create a preemptible instance by using Terraform, perform the following steps:

  1. Open your browser and enter https://shell.aliyun.com in the address bar to access Cloud Shell.

    For more information, see Use Cloud Shell.

  2. Log on to Cloud Shell.
  3. Run the vim terraform.tf command to create the terraform.tf file.
  4. 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"
    }
  5. Run the terraform init command to initialize the configurations. Initialize configurations
  6. Run the terraform plan command to preview the configurations. Preview configurations
  7. Run the terraform apply command to create the preemptible instance. Create an ECS instance
  8. Run the terraform show command to check the preemptible instance.

    The following sample command output indicates that the preemptible instance is created.

    View the created preemptible instance

Result

After a preemptible instance is created, you can view the information of the preemptible instance in the instance list on the Instances page. A preemptible instance is labeled as Pay-As-You-Go Preemptible Instance in the Billing Method column. Click the instance ID to go to the Instance Details page. In the Billing Information section, you can view the bidding policy that you configured when you create the instance.