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
Call an API operation
Use Terraform
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.
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.
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.
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": "*"
}
]
}
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:
Run the following code snippet to create a configuration file named main.tf.
provider "alicloud" {
region = var.region
}
variable "region" {
type = string
default = "cn-beijing"
}
variable "vpc_name" {
type = string
default = "tf_test_fofo"
}
variable "vpc_cidr_block" {
type = string
default = "172.16.0.0/12"
}
variable "vswitch_cidr_block" {
type = string
default = "172.16.0.0/21"
}
variable "availability_zone" {
type = string
default = "cn-beijing-b"
}
variable "security_group_name" {
type = string
default = "default"
}
variable "instance_type" {
type = string
default = "ecs.n2.small"
}
variable "system_disk_category" {
type = string
default = "cloud_efficiency"
}
variable "image_id" {
type = string
default = "ubuntu_140405_64_40G_cloudinit_20161115.vhd"
}
variable "instance_name" {
type = string
default = "test_fofo"
}
variable "internet_max_bandwidth_out" {
type = number
default = 10
}
variable "instance_charge_type" {
type = string
default = "PostPaid"
}
variable "spot_strategy" {
type = string
default = "SpotAsPriceGo"
}
variable "spot_duration" {
type = number
default = 0
}
variable "port_range" {
type = string
default = "1/65535"
}
variable "priority" {
type = number
default = 1
}
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.![image](https://help-static-aliyun-doc.aliyuncs.com/assets/img/en-US/8859005371/p872082.png)
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](https://help-static-aliyun-doc.aliyuncs.com/assets/img/en-US/8859005371/p872083.png)
Verify the result
Run the terraform show command
Verify in the ECS console
To view the details of the instance created by Terraform, run the following command:
![image](https://help-static-aliyun-doc.aliyuncs.com/assets/img/en-US/8859005371/p872093.png)
Log on to the ECS console and view the preemptible instance that you created.
![image](https://help-static-aliyun-doc.aliyuncs.com/assets/img/en-US/8859005371/p872094.png)
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.
Example
Sample code
provider "alicloud" {
region = var.region
}
variable "region" {
type = string
default = "cn-beijing"
}
variable "vpc_name" {
type = string
default = "tf_test_fofo"
}
variable "vpc_cidr_block" {
type = string
default = "172.16.0.0/12"
}
variable "vswitch_cidr_block" {
type = string
default = "172.16.0.0/21"
}
variable "availability_zone" {
type = string
default = "cn-beijing-b"
}
variable "security_group_name" {
type = string
default = "default"
}
variable "instance_type" {
type = string
default = "ecs.n2.small"
}
variable "system_disk_category" {
type = string
default = "cloud_efficiency"
}
variable "image_id" {
type = string
default = "ubuntu_140405_64_40G_cloudinit_20161115.vhd"
}
variable "instance_name" {
type = string
default = "test_fofo"
}
variable "internet_max_bandwidth_out" {
type = number
default = 10
}
variable "instance_charge_type" {
type = string
default = "PostPaid"
}
variable "spot_strategy" {
type = string
default = "SpotAsPriceGo"
}
variable "spot_duration" {
type = number
default = 0
}
variable "port_range" {
type = string
default = "1/65535"
}
variable "priority" {
type = number
default = 1
}
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
}