How to Automatically Start and Stop a Tencent Cloud Virtual Machine (CVM)?

 

Introduction

If you have an operation that you know that will only be operating for a set period of time within a week. If you could schedule the Cloud Virtual Machine (CVM) to start and stop automatically, this could result in tremendous cost savings for you. I will be writing this guide using Tencent Cloud's Python API. The other languages should more or less be the same.

What will you need?

1. Your target CVM ID. You can get it by clicking on the small copy icon beside the ID column of your CVM listings as shown in the picture below.  

How to get Tencent CVM's Instance ID






2. Your Tencent SecretId and SecretKey. You can create it through your manage API key page or the steps shown in the picture below. 







Step by step

Creating the start instance serverless cloud function

Creating the serverless cloud function

1. Go to the Function Service of your Serverless Cloud Function.



 









2. Click create












3. Choose your template. I generally create mine from scratch. 





4. Fill in the basic configurations. Since we want to start and stop an instance at a specified time so we are going to choose the Event-triggered function.








5. Generating the code from the StartInstances API Explorer. For more details about the API, please refer to the StartInstances API documentation. For more details about the code generation, please refer to the picture below. 


6. Copying the generated code into your serverless cloud function. The copying needed to be done in two parts. The import part on top and the actual code under the main_handler function. The reason is that when we create a serverless function, we will also need to define the function to be called during execution. This is why our generated code should be under this function. Please ensure that the indentation is correct. 












7. Configure the event trigger time. Refer to the time trigger description documentation for details. In the example below, I've programmed the start instance serverless cloud function to trigger from Monday to Friday at 10am Beijing standard time. Once the setting is complete, click create.











8. Ensuring that the needed dependencies are installed. If you run your serverless cloud function now, you will probably see the ModuleNotFoundError: No module named 'tencentcloud' error. To ensure this doesn't happen, please follow the following pictures. 














Once you are at the terminal, please enter the following commands. Please do not forget the dot in the second command. Use pip if you are using Python 2. 

cd src
pip3 install tencentcloud-sdk-python -t .

If you see the error of ERROR: THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE., try running any other commands then rerun the above command. It will somehow resolve itself. Your installation is correct if you see other files other than index.py under your src folder.

9. Click deploy and then click test. If everything is correct, you should see the following. 



Creating the stop instance serverless cloud function

The steps here are the same as the start instances with one difference. When you are generating your code with the API explorer, you will need to put in the STOP_CHARGING flag in the StoppedMode so that you will not get charged when your instance has been stopped. 




Comments

Popular posts from this blog

How to Auto Convert Malaysian IC Number to Birthdays Using Excel (Updated to Support Millennials)

How to Prepare for a Table Topics Compeition?

How to Solve ModuleNotFoundError: No module named 'tencentcloud' on Tencent Serverless Cloud Function?