All Products
Search
Document Center

Elastic Compute Service:Query the price of a preemptible instance

Last Updated:Aug 15, 2023

The price of preemptible instances varies based on the supply and demand of instance types. This topic describes how to query the latest price of a preemptible instance.

Prerequisites

  • An Alibaba Cloud account is created and the AccessKey pair of the account is obtained.

    You need to configure the AccessKey pair of your Alibaba Cloud account to use Alibaba Cloud Elastic Compute Service (ECS) SDK for Java. For more information about how to obtain the AccessKey pair, see Create an AccessKey pair.

  • ECS SDK for Java is installed in the development environment.

    You need to add the following dependencies to the Maven project. For more information, see Install ECS or EBS SDK for Java.

    <dependencies>
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-ecs</artifactId>
                <version>4.23.10</version>
            </dependency>
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-core</artifactId>
                <version>4.0.8</version>
            </dependency>
            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>2.6</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.83</version>
            </dependency>
    </dependencies>

Sample code

In this topic, the QuerySpotLatestPrice class is used. The following code provides an example on how to query the latest price of a preemptible instance by calling the DescribePrice operation.

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ecs.model.v20140526.DescribePriceRequest;
import com.aliyuncs.ecs.model.v20140526.DescribePriceResponse;
import com.aliyuncs.profile.DefaultProfile;

/**
 * Call the DescribePrice operation to query the latest price of a preemptible instance. 
 */
public class QuerySpotLatestPrice {
    private static IAcsClient client;
    // Specify the region ID of the preemptible instance. 
    static String regionId = "cn-hangzhou";
    // Set the network type of the preemptible instance to Virtual Private Cloud (VPC). 
    static String resourceType = "instance";
    static String instanceNetworkType = "vpc";
    // Specify the instance type of the preemptible instance. 
    static String instanceType = "ecs.g6.8xlarge";
    // Set the preemption policy to SpotAsPriceGo. 
    static String spotStrategy = "SpotAsPriceGo";
    // Set the spotDuration parameter to specify the protection period of the preemptible instance. If you cannot determine the protection period, set the value to 0. 
    static Integer spotDuration = 1;
    // Specify the zone ID of the preemptible instance. 
    static String zoneId = "cn-hangzhou-i";

    public static void main(String[] args) throws Exception {
        client = Initialization();
        describePrice(client);
    }

    public static void describePrice(IAcsClient client) throws Exception {
        // Configure the parameters of the DescribePrice operation and send the request. 
        DescribePriceRequest request = new DescribePriceRequest();
        request.setRegionId(regionId);
        request.setResourceType(resourceType);
        request.setInstanceType(instanceType);
        request.setInstanceNetworkType(instanceNetworkType);
        request.putQueryParameter("spotStrategy",spotStrategy);
        request.putQueryParameter("spotDuration",spotDuration);
        request.putQueryParameter("zoneId",zoneId);
        // Obtain the response and view the latest price of the preemptible instance. 
        DescribePriceResponse describePriceResponse = client.getAcsResponse(request);
        System.out.println("Preemptible instance price:"+describePriceResponse.getPriceInfo().getPrice().getTradePrice()+ "Yuan");
    }

    private static IAcsClient Initialization() {
        // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured for the runtime environment. 
        // If the project code is leaked, the AccessKey pair may be leaked and security issues may occur on all resources of your account. The following sample code obtains an AccessKey pair based on environment variables. We recommend that you use Security Token Service (STS). 
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        return new DefaultAcsClient(profile);
    }
}

The following figure shows a sample response.抢占式实例价格结果