Help Center> FunctionGraph> Best Practices> Periodically Starting or Stopping Huawei Cloud ECSs
Updated on 2024-03-26 GMT+08:00

Periodically Starting or Stopping Huawei Cloud ECSs

Application Scenario

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

  • Startup node: VM that needs to be started periodically.
  • Shutdown node: VM that needs to be stopped periodically.

Prerequisites

  1. Obtain the program package for periodically starting or stopping ECSs.
  2. Create the EcsOperation agency and grant it the ECS FullAccess permission. For details, see Creating an Agency.

Creating an Agency

  1. Log in to the IAM console.
  2. On the IAM console, choose Agencies from the navigation pane, and click Create Agency in the upper right corner.
    Figure 1 Creating an agency
  3. Configure the agency.
    • For Agency Name, enter EcsOperation.
    • For Agency Type, select Cloud service.
    • For Cloud Service, select FunctionGraph.
    • For Validity Period, select Unlimited.
    • Description: Enter the description.
  4. Click Next. On the displayed page, search for ECS FullAccess in the search box on the right and select it.
    Figure 2 Selecting the permission
  5. Click Next and select the application scope of the permission based on service requirements.

Building a Program

  1. Create a function.

    To create a function for periodically starting or stopping ECSs, upload the program package (for starting or stopping ECSs) and select the EcsOperation agency. For details, see Creating a Function.

    Select the Python 3.6 runtime and the agency EcsOperation created in the previous step.

  2. 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, ap-southeast-1.

    projectId

    ID of the project to which the ECS belongs. For details about how to obtain the project ID, see Obtaining a Project ID.

    whiteLists

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

    type

    Stop type, which needs to be configured when you want to periodically start ECSs. Options:

    SOFT: normal ECS stop (default)

    HARD: forcible ECS stop

    Set the environment variables by following the procedure in Configuring Environment Variables.

    • In the current example, there are no requirements on the region where the function is executed. If the function and the ECS to be started or stopped are in the same region, perform the preceding operations. If they are in different regions, for example, the function is running in CN-Hong Kong and the ECS to be started or stopped is located in AP-Bangkok, change the values of projectId and region to those of AP-Bangkok, obtain an AK/SK and add them to the environment variables, and then delete the configured agency.
      • In AK/SK-based authentication, AK/SK is used to sign requests and the signature is then added to the request headers for authentication.
      • AK: access key ID, which is a unique identifier used in conjunction with a secret access key to sign requests cryptographically.
      • SK: secret access key used together with an AK to sign requests cryptographically. It identifies a request sender and prevents the request from being modified.

    • If a large number of ECSs need to be started or stopped, increase the execution timeout for your function.
    • Environment variables in Table 1 except whiteLists are mandatory. whiteLists indicates the comma-separated IDs of the ECSs to be started or stopped.
    • Endpoint of the ECS service in the format of "{region}.{domain}", for example, ap-southeast-1.myhuaweicloud.com. To obtain the endpoint information, see Regions and Endpoints.

  3. Add a dependency.

    On the Code tab, add dependency huaweicloudsdk_ecs_core_py3.6.

    For more information, see Configuring Dependencies for a Function.

    If huaweicloudsdk_ecs_core_py3.6 is not available in your region, contact customer service.

Adding an Event Source

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