All Products
Search
Document Center

Elastic Compute Service:Create a preemptible instance

Last Updated:Dec 10, 2024

This topic describes how to create a preemptible instance by using the Elastic Compute Service (ECS) console, an API operation, or 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 biding price increases your chances of creating a preemptible instance and makes the instance less likely to be released due to price changes. 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 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 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.

Procedure

Create a preemptible instance in the ECS console

  1. Go to the instance buy page in the ECS console.

  2. Click the Custom Launch tab.

  3. 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 by using the wizard.

    • 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 one hour after the instance is created. After the period ends, the system checks 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 bidding price.

      • Set Maximum Price: You must specify the maximum price that you are willing to pay for the instance type.

  4. 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 the configurations meet your requirements.

  5. Read and select ECS Terms of Service and Product Terms of Service, 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 to 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 as the bid price. You also can set the SpotStrategy parameter to SpotWithPriceLimit and specify a maximum price that you are willing to pay for the instance type.

Create a preemptible instance by using Terraform

Prerequisites

Procedure

  1. 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.

  2. Log on to Cloud Shell.

  3. Run the vim terraform.tf command to create the terraform.tf file.

  4. Open the terraform.tf file, enter the following content, and then enter wq to save the 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 Terraform configurations.初始化配置

  6. Run the terraform plan command to preview configurations.预览配置

  7. Run the terraform apply command to create a preemptible instance.创建ECS实例

  8. Run the terraform show command to view the created preemptible instance.

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

    查看已创建的实例