All Products
Search
Document Center

Elastic Compute Service:Create a preemptible instance

Last Updated:Dec 25, 2024

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.

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

  1. Go to the ECS 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 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.

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

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

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

Note

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

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. Create a directory named terraform to organize your Terraform resources.

  4. Run the following command to access the project directory:

    cd terraform
  5. 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
}
  1. Run the terraform init command to initialize the Terraform configuration.

    The following sample command output is returned.image

  2. Run the terraform apply command. Enter yes 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.image

Verify the result

Run the terraform show command

To view the details of the instance created by Terraform, run the following command:

terraform show

image

Verify in the ECS console

Log on to the ECS console and view the preemptible instance that you created.

image

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

Note

You can also click One-click execution to run the following sample code.

Sample code

provider "alicloud" {
  region = var.region
}

# Region
variable "region" {
  type    = string
  default = "cn-beijing"
}

# VPC Name
variable "vpc_name" {
  type    = string
  default = "tf_test_fofo"
}

# VPC CIDR Block
variable "vpc_cidr_block" {
  type    = string
  default = "172.16.0.0/12"
}

# VSwitch CIDR Block
variable "vswitch_cidr_block" {
  type    = string
  default = "172.16.0.0/21"
}

# Zone
variable "availability_zone" {
  type    = string
  default = "cn-beijing-b"
}

# Security Group Name
variable "security_group_name" {
  type    = string
  default = "default"
}

# Instance Type
variable "instance_type" {
  type    = string
  default = "ecs.n2.small"
}

# System Disk Type
variable "system_disk_category" {
  type    = string
  default = "cloud_efficiency"
}

# Operating System Image
variable "image_id" {
  type    = string
  default = "ubuntu_140405_64_40G_cloudinit_20161115.vhd"
}

# Instance Name
variable "instance_name" {
  type    = string
  default = "test_fofo"
}

# Public Bandwidth
variable "internet_max_bandwidth_out" {
  type    = number
  default = 10
}

# Billing Type
variable "instance_charge_type" {
  type    = string
  default = "PostPaid"
}

# Preemptible Instance Bidding Strategy
variable "spot_strategy" {
  type    = string
  default = "SpotAsPriceGo"
}

# Preemptible Instance Protection Period
variable "spot_duration" {
  type    = number
  default = 0
}

# Inbound Rule Port Range
variable "port_range" {
  type    = string
  default = "1/65535"
}

# Inbound Rule Priority
variable "priority" {
  type    = number
  default = 1
}

# Inbound Rule CIDR
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
}

References