Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
Help Center/ FunctionGraph/ Best Practices/ Periodically Starting or Stopping Huawei Cloud ECSs

Periodically Starting or Stopping Huawei Cloud ECSs

Updated on 2024-12-05 GMT+08:00

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 an agency name, for example, 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, select an authorization scope that meets your service requirements, and click OK.

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.

    NOTE:
    • 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.
        Figure 3 Setting environment variables
    • 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.

    NOTE:

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

Figure 4 Creating a timer trigger

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback