Help Center> FunctionGraph> Best Practices> Periodically Starting or Stopping HUAWEI CLOUD ECSs

Periodically Starting or Stopping HUAWEI CLOUD ECSs

Introduction

If you need to start or stop your ECSs at specified time, you can use FunctionGraph to call the corresponding ECS APIs.

Preparation

Obtain the program package for periodically starting or stopping ECSs.

Building a Program

  1. Create an agency named EcsOperation that delegates FunctionGraph to access other cloud services.

    An agency is required if FunctionGraph accesses other cloud services. For details on how to create an agency, see Creating an Agency.

  2. Create a function.

    Create a function for periodically starting or stopping ECSs. You need to upload the program package (for starting or stopping ECSs) as shown in Figure 1 and select the agency created in 1. For details, see Creating a Function.

    Figure 1 Creating a function for periodically stopping ECSs

  3. Set environment variables.

    On the Configuration tab page, set environment variables according to Table 1.

    Table 1 Environment variables

    Environment Variable

    Description

    region

    Region where your ECSs are located, for example, cn-north-4.

    domain

    Endpoint of the ECS service in the format of "{region}.{domain}", for example, cn-north-4.myhuaweicloud.com. To obtain the endpoint information, see Regions and Endpoints.

    projectId

    ID of the project to which the ECSs belong.

    whiteLists

    • If you want to periodically start certain ECSs, specify the names of the ECSs that do not need to be started and separate them with commas (,).
    • If you want to periodically stop certain ECSs, specify the names of the ECSs that do not need to be stopped and separate them with commas (,).

    Set the environment variables (Figure 2) by following the procedure in Environment Variables.

    Figure 2 Setting environment variables
    • This practice does not have limitations on the region for function execution. For example, if your function runs in CN North-Beijing1 and you want to start or stop ECSs deployed in CN North-Beijing4, change the projectId, region, and domain parameters to those of the CN North-Beijing4 region.
    • If a large number of ECSs need to be started or stopped, increase the execution timeout for your function.
    • In Table 1, all environment variables except domain and whiteLists are mandatory. If you do not specify a domain, the default value myhuaweicloud.com of the domain parameter in the program package will be used. For whiteLists, specify the names of the ECSs to be started or stopped and separate them with commas (,).

  4. Add a dependency.

    On the Code tab page, select the openstacksdk-1.0.6 dependency, as shown in Figure 3. To download the openstacksdk-1.0.6 dependency, visit HUAWEI CLOUD SDKs.
    Figure 3 Selecting a dependency

    For more information, see Configuring Dependencies for a Function.

Adding an Event Source

Create a timer trigger and set the trigger parameters according to Figure 4.
Figure 4 Creating a timer trigger