Instance
A BCC instance refers to an active cloud server instance on Baidu AI Cloud, serving as a management entity for advanced features like billing and permission control.
Create an instance (by specifying instance package specifications)
You can create BCC instances by specification parameters with the following code
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
10 endpoint=HOST)
11 client = bcc_client.BccClient(config)
12 # billing = bcc_model.Billing(paymentTiming='Postpaid', reservationLength=2, reservationTimeUnit='Month')
13 response = client.create_instance_by_spec(
14 spec='bcc.g5.c1m4', # Instance specification
15 image_id='m-***', # Image ID
16root_disk_size_in_gb=20, # Size of system disk for virtual machine instance to be created, in GB. The default is 40 GB, with a range of [40, 2048]GB. Sizes exceeding 40 GB are charged according to cloud disk server prices. Note that the specified size of system disk must meet the minimum disk space requirement of the image used.
17root_disk_storage_type='enhanced_ssd_pl1', # Medium of system disk for virtual machine instance to be created. By default, an SSD cloud disk server is used
18 create_cds_list=[ # List of CDS disks to be created
19 bcc_model.CreateCdsModel(
20cdsSizeInGB=50, # Cloud disk server (CDS) capacity, which must be a positive integer greater than 0, in GB, with a size range of 0 - 5120G
21# CDS disk storage class, defaulting to hp1 (high-performance cloud disk). storageType='enhanced_ssd_pl1')],
22eip_name='eip***', # Length: 1-65 bytes, starting with a letter. It can contain letters, numbers and the characters -_/. If this parameter is not provided, the service will automatically generate a name.
23internet_charge_type='BANDWIDTH_PREPAID', # Charge type for public network bandwidth. If internetChargeType is not specified, it is based on actual bandwidth use by default.
24purchase_count=1, # Count of virtual machine instances to be created (purchased) in batches, which must be an integer greater than 0 (optional). The default is 1.
25name='instance-***', # Virtual machine name. By default, no name is specified. If a name is specified: For batch creation, the name serves as a prefix. The backend will append a suffix, generated as follows: name{-sequence number}.
26hostname='hostn***', # Virtual machine hostname. By default, no hostname is specified. If a hostname is specified: For batch creation, the hostname serves as a prefix. The backend will append a suffix, generated as follows: name{-sequence number}.
27admin_pass='***', # Instance administrator password to be specified: 8-16 characters, it must contain English letters, numbers, and symbols (limited to !@#$%^*()) simultaneously.
28zone_name='cn-bj-a', # Specify zone information. By default, it is empty and automatically selected by the system. The list of availability zones can be queried by calling the Query Availability Zone List API. The naming convention for zoneName is lowercase "country-region-availability zone sequence", such as "cn-bj-a" for Beijing availability zone A.
29subnet_id='sbn-***', # Specify subnet information. If empty, it is the default subnet
30security_group_id='g-***', # Specify security group information. If empty, it is the default security group
31asp_id='asp-***', # Automatic snapshot policy ID
32tags=[bcc_model.TagModel('key***', 'value***')], # List of tags to be created
33 )
34 print response
-
Mandatory parameters:
- Specification name (for example, bcc.g4.c2m8)
- Image ID
- Optional parameters: Refer to standard [instance creation interface](BCC/API Reference/Instance Related Interfaces/Create an instance (by specifying instance package specifications))
Create an instance (deprecated; it is recommended to use the Create Instance interface instead (by specifying instance package specifications))
The following code can be used to create BCC instances, including dedicated instances, general-purpose type I instances, general-purpose type II instances, general-purpose type III instances, GPU instances, compute-optimized type I instances, compute-optimized type II instances, and storage-optimized type I instances:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
10 endpoint=HOST)
11 client = bcc_client.BccClient(config)
12 # billing = bcc_model.Billing(paymentTiming='Postpaid', reservationLength=2, reservationTimeUnit='Month')
13 response = client.create_instance(
14instance_type='N6', # Type of virtual machine instance to be created
15cpu_count=1, # CPU core count for the VM instance to be created
16 memory_capacity_in_gb=4, # Memory capacity for virtual machine instance to be created, in GB
17image_id='m-***', # Image ID
18root_disk_size_in_gb=20, # Size of system disk for virtual machine instance to be created, in GB. The default is 40 GB, with a range of [40, 2048]GB. Sizes exceeding 40 GB are charged according to cloud disk server prices. Note that the specified size of system disk must meet the minimum disk space requirement of the image used.
19root_disk_storage_type='enhanced_ssd_pl1', # Medium of system disk for virtual machine instance to be created. By default, an SSD cloud disk server is used
20 create_cds_list=[ # List of CDS disks to be created
21 bcc_model.CreateCdsModel(
22cdsSizeInGB=50, # Cloud disk server (CDS) capacity, which must be a positive integer greater than 0, in GB, with a size range of 0 - 5120G
23storageType='enhanced_ssd_pl1')], # CDS disk storage class, defaulting to hp1 (high-performance cloud disk).
24eip_name='eip***', # Length: 1-65 bytes, starting with a letter. It can contain letters, numbers and the characters -_/. If this parameter is not provided, the service will automatically generate a name.
25internet_charge_type='BANDWIDTH_PREPAID', # Charge type for public network bandwidth. If internetChargeType is not specified, it is based on actual bandwidth use by default.
26purchase_count=1, # Count of virtual machine instances to be created (purchased) in batches, which must be an integer greater than 0 (optional). The default is 1.
27name='instance-***', # Virtual machine name. By default, no name is specified. If a name is specified: For batch creation, the name serves as a prefix. The backend will append a suffix, generated as follows: name{-sequence number}.
28hostname='hostn***', # Virtual machine hostname. By default, no hostname is specified. If a hostname is specified: For batch creation, the hostname serves as a prefix. The backend will append a suffix, generated as follows: name{-sequence number}.
29admin_pass='***', # Instance administrator password to be specified: 8-16 characters, it must contain English letters, numbers, and symbols (limited to !@#$%^*()) simultaneously.
30zone_name='cn-bj-a', # Specify zone information. By default, it is empty and automatically selected by the system. The list of availability zones can be queried by calling the Query Availability Zone List API. The naming convention for zoneName is lowercase "country-region-availability zone sequence", such as "cn-bj-a" for Beijing availability zone A.
31subnet_id='sbn-***', # Specify subnet information. If empty, it is the default subnet
32security_group_id='g-***', # Specify security group information. If empty, it is the default security group
33asp_id='asp-***', # Automatic snapshot policy ID
34tags=[bcc_model.TagModel('key***', 'value***')], # List of tags to be created
35eni_ids=["eni-***"] # List of short IDs for elastic network interface cards to be mounted. During batch creation, elastic network interface cards will be evenly distributed among instances based on the count provided. Ensure that the count of elastic network interface card IDs provided is an integer multiple of the count of instances and that the count of ENIs assigned to each instance does not exceed the maximum count that can be mounted by the instance. Elastic baremetal compute (EBC) instances are not currently supported. The maximum number of eniIds is 5000.
36 )
37 print response
Please refer to [List](BCC/API Reference/Appendix.md#Regional models and optional configurations) for the list of instance types and GPU type definitions.
Create instances on dedicated server (support plaintext password encryption)
You can create BCC instances in the specific server with the following code:
1 def create_instance_from_dedicated_host_with_encrypted_password(self):
2#If the user does not specify the client_token parameter, use uuid4 to generate a random string to client_token
3 generate_client_token = str(uuid.uuid4())
4 client_token = generate_client_token()
5#Set the image ID
6 image_id = 'your-choose-image-id'
7
8# Set the dedicated server ID
9 dedicated_host_id = 'your-choose-dedicated-host-id'
10#Set the instance name
11 instance_name = 'your-choose-instance-name' + client_token
12#Set the administrator password for the instance (8-16 characters; it must contain English letters, numbers, and symbols (limited to !@#$%^*()) simultaneously)
13 admin_pass = 'your-admin-pass'
14#Specify subnet information. If empty, it is the default subnet
15 subnet_id = 'your-choose-subnet-id'
16#Specify security group information. If empty, it is the default security group
17 security_group_id = 'your-choose-security-group-id'
18#DCC dedicated server instances can create multiple local disks, requiring specifying the disk type and size
19#Other types can only use one local disk with the default type, only requiring specifying the disk type and size
20#Set the size of the local disk to be created
21 ephemeral_disk = EphemeralDisk(6144)
22#Set the local disk type to the default type
23 ephemeral_disks = [ephemeral_disk.__dict__, ephemeral_disk.__dict__]
24 self.assertEqual(
25type(self.client.create_instance_from_dedicated_host_with_encrypted_password (1, #Set the number of CPU cores for creating the BCC
262, #Set the memory size (GB) for creating the BCC
27image_id, #Set the image for creating the BCC
28dedicated_host_id, #Set the dedicated server for creating the BCC
29purchase_count=1,# Used when creating BCC instances in batches, it must be an integer greater than 0
30name=instance_name,# Set the name for creating the BCC
31admin_pass=admin_pass,# Set the instance administrator password
32subnet_id=subnet_id,# Specify the subnet information. If empty, the default subnet is used.
33security_group_id=security_group_id,# Specify the security group information. If empty, it is the default security group
34 client_token=client_token
35ephemeral_disks) #Set the local disk for creating the BCC
36 baidubce.bce_response.BceResponse)
Create an instance on a dedicated server.
You can create BCC instances in the specific server with the following code:
1 def create_instance_from_dedicated_host(self):
2#If the user does not specify the client_token parameter, use uuid4 to generate a random string to client_token
3 generate_client_token = str(uuid.uuid4())
4 client_token = generate_client_token()
5#Set the image ID
6 image_id = 'your-choose-image-id'
7
8# Set the dedicated server ID
9 dedicated_host_id = 'your-choose-dedicated-host-id'
10#Set the instance name
11 instance_name = 'your-choose-instance-name' + client_token
12#Set the administrator password for the instance (8-16 characters; it must contain English letters, numbers, and symbols (limited to !@#$%^*()) simultaneously)
13 admin_pass = 'your-admin-pass'
14#Specify subnet information. If empty, it is the default subnet
15 subnet_id = 'your-choose-subnet-id'
16#Specify security group information. If empty, it is the default security group
17 security_group_id = 'your-choose-security-group-id'
18#DCC dedicated server instances can create multiple local disks, requiring specifying the disk type and size
19#Other types can only use one local disk with the default type, only requiring specifying the disk type and size
20#Set the size of the local disk to be created
21 ephemeral_disk = EphemeralDisk(6144)
22#Set the local disk type to the default type
23 ephemeral_disks = [ephemeral_disk.__dict__, ephemeral_disk.__dict__]
24 self.assertEqual(
25type(self.client.create_instance_from_dedicated_host(1, #Set the number of CPU cores for creating the BCC
262, #Set the memory size (GB) for creating the BCC
27image_id, #Set the image for creating the BCC
28dedicated_host_id, #Set the dedicated server for creating the BCC
29purchase_count=1,# Used when creating BCC instances in batches, it must be an integer greater than 0
30name=instance_name,# Set the name for creating the BCC
31admin_pass=admin_pass,# Set the instance administrator password
32subnet_id=subnet_id,# Specify the subnet information. If empty, the default subnet is used.
33security_group_id=security_group_id,# Specify the security group information. If empty, it is the default security group
34 client_token=client_token
35ephemeral_disks) #Set the local disk for creating the BCC
36 baidubce.bce_response.BceResponse)
Query instance list
You can query the list of BCC virtual machine instances with the following code, supporting filtering by intranet IP, dedicated server ID and availability zone name:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 `AK = `b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 res = client.list_instances(marker='i-***', # Starting position of batch list query, which is a system-generated string
15 Max_keys=1, # Maximum number of items per page, with a maximum not exceeding 1000, default: 1,000
16 internal_ip='192.168.***.***', # Intranet IP
17 dedicated_host_id='d-***', # Dedicated server ID
18 zone_name='cn-bj-d', # Availability zone information
19 instance_ids='i-***', # Multiple instance IDs, separated by English commas, maximum 100 IDs
20 instance_ids='i-***', # Multiple instance names, separated by English commas, maximum 100 IDs
21 cds_ids='v-***', # Multiple CDS disk IDs, separated by English commas, maximum 100 IDs
22 deployset_ids='dset-***', # Multiple deployment group IDs, separated by English commas, maximum 100 IDs
23 security_group_ids='g-***', # Multiple security group IDs, separated by English commas, maximum 100 IDs
24 payment_timing='Postpaid', # Payment method (Prepaid/Postpaid)
25 status='Running', # Instance status (Recycled/Running/Stopped/Stopping/Starting)
26 tags='***', # Multiple tags, separated by commas, format: tagKey:tagValue or tagKey
27 vpc_id='vpc-***', # vpcId, which can only be used in combination with the privateIps query parameter
28 private_ips='192.168.***.***', # Multiple Intranet IPs, separated by English commas, maximum 100 IPs, which must be used in combination with vpcId
29 )
30 print(res)
Query instance details
You can query the detailed information of the specified BCC virtual machine with the following code:
1 def get_instance(self):
2 # Set instance_id to be operated
3 instance_id = 'your-choose-instance-id'
4 #Set whether to return creation failure information; True means to return creation failure information; default: False
5 contains_failed = True
6 self.assertEqual(
7 type(self.client.get_instance(instance_id, contains_failed=contains_failed)),
8 baidubce.bce_response.BceResponse)
Query the instance list by the instance ID in batch
You can query the instance list by the instance ID in batch with the following code
1 from baidubce.auth.bce_credentials import BceCredentials
2 from baidubce.bce_client_configuration import BceClientConfiguration
3 from baidubce.services.bcc import bcc_client
4 if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 resp = client.list_instance_by_instance_ids(instance_ids=['i-***', 'i-***', 'i-***'], # List of instance IDs to be queried
15 marker='123', # Starting position of the batch list query
16 max_keys=1000, # Maximum number of items per page,
17 )
18 print(resp)
Start an instance
You can start a specified BCC virtual machine instance with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Set instance_id to be operated
15 instance_id = 'i-***'
16 resp = client.start_instance(instance_id)
17 print(resp)
Stop an instance
You can stop a specified BCC virtual machine instance with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 resp = client.stop_instance(
15 instance_id='i-***', # Instance ID to be stopped
16 force_stop=False # Specify whether to forcibly stop instance. Options: true, false. The default is false
17 stopWithNoCharge=True # Specify whether to perform no charge for stopped instances. Options: true, false. The default is false.
18 )
19 print(resp)
Reboot an instance
You can reboot a specified BCC virtual machine instance with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Set instance_id to be operated
15 instance_id = 'i-***'
16# Restart instance
17 resp = client.reboot_instance(instance_id)
18 print(resp)
Modify the instance password
You can modify the administrator password of a specified BCC virtual machine instance with the following code:
1 from baidubce.auth.bce_credentials import BceCredentials
2 from baidubce.bce_client_configuration import BceClientConfiguration
3 from baidubce.services.bcc import bcc_client, bcc_model
4 if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Set instance_id to be operated
15 instance_id = 'i-***'
16# Set new password
17 admin_pass = '***
18 resp = client.modify_instance_password(instance_id, admin_pass)
19 print(resp)
Modify instance attributes
The following code can be used to modify the name or network interface card queue count of a specified BCC virtual machine instance:
1 def modify_instance_attributes(self):
2 # Set instance_id to be operated
3 instance_id = 'your-choose-instance-id'
4 name = 'name_modify'
5 neteth_queuecount = 3 # New network interface card queue count
6 self.assertEqual(
7 type(self.client.modify_instance_attributes(instance_id,
8 name=name,
9 neteth_queuecount= neteth_queuecount)),
10 baidubce.bce_response.BceResponse)
Reinstall an instance
You can rebuild a specified BCC virtual machine instance using an image with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create BCC client
13 client = bcc_client.BccClient(config)
14res = client.rebuild_instance(instance_id="i-***", # Specified instance ID
15image_id="m-***", # Specified image ID
16admin_pass="***", # Machine password (8-16 characters, it must contain English letters, numbers and symbols (limited to !@#$%^*() simultaneously)). The password must be encrypted for transmission
17key_pair_id="k-***", # Key pair ID to be bound to the instance being reinstalled. Either adminPass or keypairId must be specified
18 )
19 print(res)
Reinstall instance (in batch)
You can reinstall instances in batches with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configurations
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14res = client.batch_rebuild_instances(instance_ids=["i-***", "i-***"], # The list of specified instance IDs, maximum 100
15image_id="m-***", # Specified image ID
16admin_pass="***", # Machine password (8-16 characters, it must contain English letters, numbers and symbols (limited to !@#$%^*() simultaneously)). The password must be encrypted for transmission
17keypair_id="k-***", # Key pair ID to be bound to the instance being reinstalled. Either adminPass or keypairId must be specified
18 )
19 print(res)
Release an instance
For BCC virtual machine instances with expired postpay (Postpaid) or prepay (Prepaid) billing modes, you can release them with the following code:
1 from baidubce.auth.bce_credentials import BceCredentials
2 from baidubce.bce_client_configuration import BceClientConfiguration
3 from baidubce.services.bcc import bcc_client
4
5 if __name__ == '__main__':
6# Set your AK, SK, and the region to be accessed
7 HOST = b'http://bcc.bj.baidubce.com'
8 AK = b'ak'
9 SK = b'sk'
10# Set default configuration
11 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12 endpoint=HOST)
13# Create a BCC client
14 client = bcc_client.BccClient(config)
15
16 resp = client.release_instance(instance_id='i-sxb2****') # Release Instance ID
17 print (resp)
Instance scaling
Support scaling the CPU and memory of BCC virtual machines. Refer to the following code for details:
1 from baidubce.auth.bce_credentials import BceCredentials
2 from baidubce.bce_client_configuration import BceClientConfiguration
3 from baidubce.services.bcc import bcc_client
4
5 if __name__ == '__main__':
6# Set your AK, SK, and the region to be accessed
7 HOST = b'http://bcc.bj.baidubce.com'
8 AK = b'ak'
9 SK = b'sk'
10# Set default configuration
11 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12 endpoint=HOST)
13# Create a BCC client
14 client = bcc_client.BccClient(config)
15
16 resp = client.resize_instance(instance_id="i-sxb2****", # Instance ID
17 cpu_count=4, # CPU core count
18 memory_capacity_in_gb=16, # Memory size
19 live_resize=True, # Resize on line or not
20 enable_jumbo_frame=False) # Whether to enable Jumbo frame. True: Enable Jumbo frame; False: Disable Jumbo frame. The default is empty. When empty: If resizing to a specification that does not support Jumbo frames, Jumbo frames will not be enabled;
21# When resizing from a specification that does not support Jumbo frames to one that does, Jumbo frames will not be enabled; when resizing between specifications that both support Jumbo frames, if Jumbo frames are enabled before resizing, they will remain enabled after resizing; if they are not enabled before resizing, they will not be enabled after resizing.
22 print (resp)
Add an instance to a security group
You can add a specified BCC virtual machine instance to a designated security group with the following code:
1 def bind_instance_to_security_group(self):
2 #Specify the information of security_group_id
3 security_group_id = 'your-choose-security-group-id'
4 # Set instance_id to be operated
5 instance_id = 'your-choose-instance-id'
6 self.assertEqual(type(self.client.bind_instance_to_security_group(instance_id, security_group_id)),
7 baidubce.bce_response.BceResponse)
Remove an instance from a security group
You can remove a specified instance from a designated security group with the following code:
1 def unbind_instance_from_security_group(self):
2 #Specify the information of security_group_id
3 security_group_id = 'your-choose-security-group-id'
4 # Set instance_id to be operated
5 instance_id = 'your-choose-instance-id'
6 self.assertEqual(
7 type(self.client.unbind_instance_from_security_group(instance_id, security_group_id)),
8 baidubce.bce_response.BceResponse)
Bind instance to tag
The following code can be used to bind a specified tag to a specified BCC virtual machine instance:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Enter tag information, supporting binding to multiple tags at once
15 instance_tag1 = bcc_model.TagModel(tagKey='TestKey01',tagValue='TestValue01')
16 instance_tag2 = bcc_model.TagModel(tagKey='TestKey02',tagValue='TestValue02')
17 instance_tag_list = []
18 instance_tag_list.append(instance_tag1)
19 instance_tag_list.append(instance_tag2)
20# Set instance_id to be operated
21 instance_id = 'i-***'
22# Execute the operation of binding instance to tag
23 resp = client.bind_instance_to_tags(instance_id=instance_id, tags=instance_tag_list)
24 print(resp)
Unbind a tag from an instance
The following code can be used to unbind a specified tag from a specified BCC virtual machine instance:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Enter tag information, supporting binding to multiple tags at once
15 instance_tag1 = bcc_model.TagModel(tagKey='Key***',tagValue='Value***')
16 instance_tag2 = bcc_model.TagModel(tagKey='Key***',tagValue='Value***')
17 instance_tag_list = []
18 instance_tag_list.append(instance_tag1)
19 instance_tag_list.append(instance_tag2)
20# Set instance_id to be operated
21 instance_id = 'i-***'
22# Execute the operation of unbinding a tag from an instance
23 resp = client.unbind_instance_from_tags(instance_id=instance_id, tags=instance_tag_list)
24 print(resp)
Query the address of instance
You can query the VNC URL of a specified BCC virtual machine instance with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 print(client.get_instance_vnc("i-***")) # i-*** represents the virtual machine instance ID
Renew an instance
Renewing a BCC virtual machine extends its expiration period. You can renew the disk with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4from baidubce.services.bcc import bcc_model
5if __name__ == '__main__':
6# Set your AK, SK, and the region to be accessed
7 HOST = b'http://bcc.bj.baidubce.com'
8 AK = b'ak'
9 SK = b'sk'
10# Set default configuration
11 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12 endpoint=HOST)
13# Create a BCC client
14 client = bcc_client.BccClient(config)
15 res = client.purchase_reserved_instance(instance_id='i-***', # Virtual machine instance ID
16 billing=bcc_model.Billing('Prepaid', 1), # Order details; this interface is used to renew prepaid instances by month
17 related_renew_flag='CDS_EIP', # Set the instance's associated renewal flag, defaulting to an empty string
18 client_token='random-uuid' # Idempotency Token, an ASCII string with a length not exceeding 64 bits
19 )
20 print(res)
Enable auto-renewal
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 resp = client.batch_create_auto_renew_rules(
15 instance_id='i-***', # Instance ID
16 renew_time=2, # Renewal duration
17 renew_time_unit='year', # Unit of renewal time, month/year
18 renew_eip=True, # Whether to enable auto-renewal for EIP. Default: true
19 renew_cds=True, # Whether to enable auto-renewal for EIP. Default: true
20 )
21 print(resp)
Delete auto-renewal
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 resp = client.batch_delete_auto_renew_rules(
15 instance_id='i-***', # Instance ID
16 renew_eip=True, # Whether to disable auto-renewal for EIP. Default: true
17 renew_cds=True, # Whether to disable auto-renewal for EIP. Default: true
18 )
19 print(resp)
Change the instance subnet
You can change the instance subnet with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 res = client.instance_change_subnet(instance_id="i-***", # Virtual machine instance ID
15 subnet_id="sbn-***", # Changed subnet ID
16 internal_ip="10.59.***.***", # Specify Intranet IP; if not specified, it will be randomly generated
17 reboot=True, # Whether to restart after change
18 client_token='random-uuid' # Idempotency Token, an ASCII string with a length not exceeding 64 bits
19 )
20 print(res)
Add secondary IPs to the primary network interface card in batch
You can add secondary IP address to the primary network interface card in batches with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 private_ips = ["192.168.***.***", "192.168.***.***"]
15 res = client.batch_add_ip(instance_id="i-***", # Instance ID
16 private_ips=private_ips, # Secondary IP addresses, up to 100 IPs, which cannot be used simultaneously with SecondaryPrivateIpAddressCount
17# secondary_private_ip_address_count=2, # Number of automatically allocated IPs, which cannot be used simultaneously with PrivateIps
18 allocate_multi_ipv6_addr=True # Whether to allocate IPV6. Creating IPV6 must be true
19 )
20 print(res)
Delete secondary IP address from network interface card in batches
You can delete secondary IP address to the network interface card in batches with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 private_ips = ["192.168.***.***", "192.168.***.***"]
15 res = client.batch_delete_ip(instance_id="i-***", # Instance ID
16 private_ips=private_ips, # Secondary IP addresses, maximum 100 IPs
17 )
18 print(res)
Release an instance (POST)
You can release a single postpaid cloud server instance with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 res = client.release_instance_with_related_resources(instance_id="i-***", # Instance ID
15 related_release_flag=True, # Whether to release both EIPs and data disks currently attached on instance
16 delete_cds_snapshot_flag=True, # Whether to release cloud disk server snapshots
17 delete_related_enis_flag=True, # Whether to delete the associated ENI upon instance release
18 bcc_recycle_flag=True, # Whether to move the instance to the recycle bin upon release
19 )
20 print(res)
Release prepaid instance (POST)
Release prepaid cloud server instance. After release, the physical resources used by the instance are reclaimed, and all related data is lost and cannot be recovered. By default, only the cloud server instance and system disk are released during the release process. Users can select whether to release the EIP and data disks attached on the cloud server instance in association. The associated EIP and data disks must either be released entirely or not at all; partial release is not supported. When selecting to release the EIP and data disks in association, cloud disk server snapshots can also be selected for associated release. If data disks are not released in association, cloud disk server snapshots cannot be released in association either.
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 res = client.release_prepaid_instance_with_related_resources(instance_id="i-***", # Instance ID
15 related_release_flag=True, # Whether to release the EIPs and data disks currently attached on the instance at the same time (the delete_cds_snapshot_flag field will only be valid if this field is true. If this field is false, the value of the delete_cds_snapshot_flag field is invalid).
16 delete_cds_snapshot_flag=True, # Whether to release cloud disk server snapshots
17 delete_related_enis_flag=True, # Whether to delete the associated ENI upon instance release
18 print(res)
Retrieve instance user data information (POST)
Retrieve instance user data information with the following codes
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 res = client.get_instance_user_data(instance_id='i-b34ycow2')
15 print(res)
