Instance
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 response = client.create_instance_by_spec(
13 spec='bcc.g5.c1m4', # Instance specification
14 image_id='m-***', # Image ID
15root_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.
16root_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
17 create_cds_list=[ # List of CDS disks to be created
18 bcc_model.CreateCdsModel(
19cdsSizeInGB=50, # Cloud disk server (CDS) capacity, which must be a positive integer greater than 0, in GB, with a size range of 0 - 5120G
20storageType='enhanced_ssd_pl1')], # CDS disk storage class, defaulting to hp1 (high-performance cloud disk).
21eip_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.
22internet_charge_type='BANDWIDTH_PREPAID', # Charge type for public network bandwidth. If internetChargeType is not specified, it is based on actual bandwidth use by default.
23purchase_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.
24name='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}.
25hostname='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}.
26admin_pass='***', # Instance administrator password to be created: 8-16 characters, it must contain English letters, numbers, and symbols (limited to !@#$%^*()) simultaneously.
27zone_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.
28subnet_id='sbn-***', # Specify subnet information. If empty, it is the default subnet
29security_group_id='g-***', # Specify security group information. If empty, it is the default security group
30asp_id='asp-***', # Automatic snapshot policy ID
31tags=[bcc_model.TagModel('key***', 'value***')], # List of tags to be created
32eni_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.
33 )
34 print(response)
-
Mandatory parameters:
- Specification name (for example, bcc.g4.c1m1)
- Image ID
- Optional parameters: Refer to standard instance creation interface
Create an instance (deprecated; it is recommended to use the "Create Instance (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
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 created: 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
35 )
36 print(response)
For type code, please refer to [Instance Type](BCC/API Reference/Appendix.md#InstanceType), [Disk Type](BCC/API Reference/Appendix.md#StorageType) and [GPU Type](BCC/API Reference/Appendix.md#GpuType)
Create sub-instances for 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# Set the dedicated server ID
8 dedicated_host_id = 'your-choose-dedicated-host-id'
9#Set the instance name
10 instance_name = 'your-choose-instance-name' + client_token
11#Set the administrator password for the instance (8-16 characters; it must contain English letters, numbers, and symbols (limited to !@#$%^*()) simultaneously)
12 admin_pass = 'your-admin-pass'
13#Specify subnet information. If empty, it is the default subnet
14 subnet_id = 'your-choose-subnet-id'
15#Specify security group information. If empty, it is the default security group
16 security_group_id = 'your-choose-security-group-id'
17#DCC dedicated server instances can create multiple local disks, requiring specifying the disk type and size
18#Other types can only use one local disk with the default type, only requiring specifying the disk type and size
19#Set the size of the local disk to be created
20 ephemeral_disk = EphemeralDisk(6144)
21#Set the local disk type to the default type
22 ephemeral_disks = [ephemeral_disk.__dict__, ephemeral_disk.__dict__]
23 self.assertEqual(
24type(self.client.create_instance_from_dedicated_host(1, #Set the number of CPU cores for creating the BCC
252, #Set the memory size (GB) for creating the BCC
26image_id, #Set the image for creating the BCC
27dedicated_host_id, #Set the dedicated server for creating the BCC
28purchase_count=1,# Used when creating BCC instances in batches, it must be an integer greater than 0
29name=instance_name,# Set the name for creating the BCC
30admin_pass=admin_pass,# Set the instance administrator password
31subnet_id=subnet_id,# Specify the subnet information. If empty, the default subnet is used.
32security_group_id=security_group_id,# Specify the security group information. If empty, it is the default security group
33 client_token=client_token
34ephemeral_disks) #Set the local disk for creating the BCC
35 baidubce.bce_response.BceResponse)
Create instances on dedicated server (support plaintext password encryption)
1Use the following code to create a BCC instance in a specific server:
2 def create_instance_from_dedicated_host_with_encrypted_password(self):
3#If the user does not specify the client_token parameter, use uuid4 to generate a random string for client_token.
4 generate_client_token = str(uuid.uuid4())
5 client_token = generate_client_token()
6#Set the image ID.
7 image_id = 'your-choose-image-id'
8#Set the dedicated host 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 instance administrator password (8-16 characters, must include English letters, numbers, and symbols. Symbols are limited to !@#$%^*()).
13 admin_pass = 'your-admin-pass'
14#Specifies the subnet information. If empty, it is the default subnet.
15 subnet_id = 'your-choose-subnet-id'
16#Specifies the security_group_id information. If empty, the default security group will be used.
17 security_group_id = 'your-choose-security-group-id'
18#DCC dedicated host instances can create multiple local disks. You need to specify the disk type and size.
19#Other types can only use one local disk with the default type. Only the size needs to be specified.
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 the BCC to be created.
262, #Set the memory size (GB) for the BCC to be created.
27image_id, #Set the image for the BCC to be created.
28dedicated_host_id, #Set the dedicated host for the BCC to be created.
29purchase_count=1,# Used when creating BCC instances in batches, must be an integer greater than 0.
30name=instance_name,# Set the name for the BCC to be created.
31admin_pass=admin_pass,# Set the instance administrator password.
32subnet_id=subnet_id,# Specifies the subnet information. If empty, the default subnet will be used.
33security_group_id=security_group_id,# Specifies the security group information. If empty, the default security group will be used.
34 client_token=client_token
35ephemeral_disks) #Set the local disk for the BCC to be created.
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 IDs, 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)),
8
9 baidubce.bce_response.BceResponse)
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
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# Execute the operation of modifying instance password
19 resp = client.modify_instance_password(instance_id, admin_pass)
20 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)
Modify instance description (new)
You can modify the description 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# Set instance_id to be operated
15 instance_id = 'i-***'
16# Modify instance description
17 desc = 'your_new_description'
18 resp = client.modify_instance_desc(instance_id, desc)
19 print(resp)
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)
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:
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 resp = client.release_instance(instance_id='i-sxb2****') # Release Instance ID
15 print (resp)
Instance scaling
Support expand the CPU and memory of BCC virtual machines. Refer to the following code for details:
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 resp = client.resize_instance(instance_id="i-sxb2****", # Instance ID
15 cpu_count=4, # CPU core count
16 memory_capacity_in_gb=16, # Memory size
17 live_resize=True, # Resize on line or not
18 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;
19# 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.
20 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(
7 type(self.client.bind_instance_to_security_group(instance_id,security_group_id)),
8 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 (new)
The following code can be used to bind a specified tag to a specified BCC virtual machine instance:
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# 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 (new)
The following code can be used to unbind a specified tag from a specified BCC virtual machine instance:
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# 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)
Create spot instances
You can create spot instances 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 response = client.create_instance_of_bid(
13bid_model='market', # Spot instance bidding model; market price: "market" custom: "custom".
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='eipN***', # 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='host***', # 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 created: 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
35 )
36 print(response)
37
Query recycle bin instance list
The following code can be used to query the list of instances in the recycle bin
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 resp = client.list_recycled_instances(payment_timing="postpaid", recycle_begin='2023-11-21T20:00:00Z')
15 print(resp)
Scheduled release (limited to postpaid instances)
Postpaid instances can be set for scheduled release using the following code. At the specified time, the BCC instance will be automatically released. You can query the automatic release time in the instance details under ReleaseTime.
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.auto_release_instance(
15 instance_id="i-***", # Instance ID
16 release_time='2023-12-15T14:20:00Z') # Release time, format: yyyy-MM-dd'T'HH:mm:ss'Z'
17 print(resp)
Query the details of instance associated with deployment group
The following code can be used to query the details of instance associated with deployment group
1def test_get_instance_with_deploy_set(self):
2 instance_id = "i-oUXBvdIx"
3 self.assertEqual(
4 type(self.client.get_instance_with_deploy_set(instance_id)),
5 baidubce.bce_response.BceResponse)
6 print(self.client.get_instance(instance_id))
Query the details of instance associated with deployment group and failure field
You can query the details of instance associated with deployment group and failure field with the following code
1def test_get_instance_with_deploy_set_and_failed(self):
2 """
3 test case for get_instance
4 """
5 instance_id = "i-oUXBvdIx"
6 self.assertEqual(
7 type(self.client.get_instance_with_deploy_set_and_failed(instance_id)),
8 baidubce.bce_response.BceResponse)
9 print(self.client.get_instance(instance_id))
Remove the relationship between an instance and its deployment group
You can delete the relationship between an instance and a deployment group 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.del_instance_deploy(instance_id_list=['i-***', 'i-***'], # List of instance IDs
15 deploy_set_id='dset-***' # Deployment group ID
16 )
Modify instance hostname
The following code can be used to modify the instance hostname
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.modify_instance_hostname(instance_id="i-***", # Specified instance ID
15 hostname="new-hostname", # Instance hostname
16 reboot=True, # Whether to restart automatically
17 is_open_hostname_domain=True, # Whether to enable hostname domain
18 )
Billing for postpaid resources resumed from recycle bin
The following code can be used to resume billing for postpaid resources from recycle bin
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# ID of instance with billing resumed from recycle bin
15 instance_ids = ["i-***", "i-***"]
16 resp = client.recovery_instances(instance_ids)
17 print(resp)
Query the market price of spot instances
You can query the market price of spot instances with the following code
1def test_get_bid_instance_price(self):
2 """
3 test case for get_bid_instance_price
4 """
5 resp = self.client.get_bid_instance_price(instance_type='N3', cpu_count=1, memory_cap_in_gb=1,
6 root_disk_size_in_gb=20)
7 self.assertEqual(
8 type(resp),
9 baidubce.bce_response.BceResponse)
10 if resp is not None and resp.content is not None:
11 print(json.loads(resp.content.decode('utf-8')))
12 else:
13 print(resp)
Query spot instance packages
You can query spot instance packages with the following code
1def test_list_bid_flavor(self):
2 """
3 test case for list_bid_flavor
4 """
5 resp = self.client.list_bid_flavor()
6 self.assertEqual(
7 type(resp),
8 baidubce.bce_response.BceResponse)
9 if resp is not None and resp.content is not None:
10 print(json.loads(resp.content.decode('utf-8')))
11 else:
12 print(resp)
Release protection
You can set release protection for a BCC instance with the following code. The current setting can be queried in the instance details under DeletionProtection, with the default of 0 (no protection) and 1 (release protection enabled):
1def test_modify_deletion_protection(self):
2 """
3 test case for modify_deletion_protection
4 """
5 resp = self.client.modify_deletion_protection("i-XS7Db00e", 1)
6 self.assertEqual(
7 type(resp),
8 baidubce.bce_response.BceResponse)
9 if resp is not None and resp.content is not None:
10 print(json.loads(resp.content.decode('utf-8')))
11 else:
12 print(resp)
Query instance package price
You can query the price corresponding to instance resource package specifications 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.get_price_by_spec(spec_id="g5", # Instance family ID
15 spec="bcc.g5.c2m8", # Instance package specification
16 payment_timing="prepay", # Bill type, including Postpaid and Prepaid
17 zone_name="cn-bj-d", # Availability zone name
18 purchase_num=2, # Instance count
19 purchase_length=2 # Purchase period, unit: month
20 )
21 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 security_group_ids=["g-p8x***"], # Regular security group, not specified simultaneously with enterprise security group, only effective when changing VPC
19 enterprise_security_group_ids=["esg-***"], # Enterprise security group, not specified simultaneously with regular security group, only effective when changing VPC
20 client_token='random-uuid' # Idempotency Token, an ASCII string with a length not exceeding 64 bits
21 )
22 print(res)
Change the instance VPC
You can change the instance VPC 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# 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 instance_id = "i-oUXB****" # Instance ID
15 subnet_id = "sbn-5k3wawcr****" # Subnet ID
16 internal_ip = "192.168.*.*" # Intranet IP
17 resp = client.instance_change_vpc(instance_id=instance_id, subnet_id=subnet_id, internal_ip=internal_ip)
18 print (resp)
Query the list of elastic network interfaces bound to the instance
You can query the list of elastic network interfaces bound to the instance with the following code
1 def test_list_instance_enis(self):
2 """
3 test case for list_instance_enis
4 """
5 resp = self.client.list_instance_enis(instance_id="i-oUXBvdIx")
6 self.assertEqual(
7 type(resp),
8 baidubce.bce_response.BceResponse)
9 if resp is not None and resp.content is not None:
10 print(json.loads(resp.content.decode('utf-8')))
11 else:
12 print(resp)
Query instance package specifications (new)
You can query the list information of instance resource package specifications 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_flavor_spec(zone_name='cn-bj-d')
15 print(resp)
Resize an instance (via instance package specifications)
You can select CPU, MemoryCapacityInGB and EphemeralDisks to resize a specified BCC instance with the following code:
1 def test_resize_instance_by_spec(self):
2 """
3 test case for resize_instance_by_spec
4 """
5 resp = self.client.resize_instance_by_spec(instance_id="i-oUXBvdIx", spec='bcc.ic1.c1m1')
6 self.assertEqual(
7 type(resp),
8 baidubce.bce_response.BceResponse)
9 if resp is not None and resp.content is not None:
10 print(json.loads(resp.content.decode('utf-8')))
11 else:
12 print(resp)
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)
Shift charge - convert to prepay
You can shift bill type for instances to prepay 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 resp = client.change_to_prepaid(instance_id="i-sxb2****", # Instance ID
15 duration=2, # Renewal duration, unit: month
16 relation_cds=True) # Whether to associate CDS for conversion to prepay
17 print (resp)
Query the list of BCC instances eligible for no charge for stopped instances
Query the list of BCC instances eligible for no charge for stopped instances:
1 def test_list_instance_no_charge(self):
2 """
3 test case for list_instance_no_charge
4 """
5 resp = self.client.list_instance_no_charge(keypair_id='k-Mk1c8QPE', zone_name='cn-bj-a')
6 self.assertEqual(
7 type(resp),
8 baidubce.bce_response.BceResponse)
9 if resp is not None and resp.content is not None:
10 print(json.loads(resp.content.decode('utf-8')))
11 else:
12 print(resp)
Cancel a spot instance order
You can cancel a spot instance order with the following code
1 def test_cancel_bid_order(self):
2 """
3 test case for cancel_bid_order
4 """
5 resp = self.client.cancel_bid_order(order_id='test_id')
6 self.assertEqual(
7 type(resp),
8 baidubce.bce_response.BceResponse)
9 if resp is not None and resp.content is not None:
10 print(json.loads(resp.content.decode('utf-8')))
11 else:
12 print(resp)
Enable automatic renewal (including associated services)
Automatic renewal is limited to prepaid products
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_eip=True, # Whether to enable auto-renewal for EIP. Default: true
17 renew_cds=True, # Whether to enable auto-renewal for CDS. Default: true.
18 renew_time=2, # Renewal duration
19 renew_time_unit='year' # Unit of renewal time, month/year
20 )
21 print(resp)
Disable auto-renewal (including associated services)
Automatic renewal is limited to prepaid products
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)
Release recycle bin instances
Instances in the recycle bin are automatically released after 7 days to clean up recycle bin resources. You can manually release them with the following code:
1# !/usr/bin/env python
2# coding=utf-8
3from baidubce.services.bcc import bcc_client
4from baidubce.auth.bce_credentials import BceCredentials
5from baidubce.bce_client_configuration import BceClientConfiguration
6if __name__ == '__main__':
7# Set your AK, SK, and the region to be accessed
8 HOST = b'http://bcc.bj.baidubce.com'
9 AK = b'ak'
10 SK = b'sk'
11# Set default configuration
12 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
13 endpoint=HOST)
14# Create a BCC client
15 client = bcc_client.BccClient(config)
16# Instance ID
17 instanceId = "i-***"
18# Request to delete instance from recycle bin
19 response = client.delete_recycled_instance(instance_id=instanceId)
20 print(response)
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)
Delete instances in batches
The following code can be used to delete instances in batches together with the associated resources.
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.batch_delete_instance_with_related_resource(instance_ids=['i-***'], # Instance IDs, maximum 100 IDs
15 related_release_flag=True, # Whether to delete the associated resources upon instance deletion
16 )
17 print(resp)
Restart instances in batches
You can restart 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 configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Instance IDs, maximum 100 IDs
15 resp = client.batch_start_instance(instance_ids=['i-***', 'i-***'])
16 print(resp)
Stop instances in batch
You can stop 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, 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_stop_instance(
15 instance_ids=['i-***', 'i-***'], # Collection of instance IDs, maximum 100 IDs
16 force_stop=False, # Specify whether to forcibly stop instance. Options: true, false
17 stop_with_no_charge=True # Whether to charge when stopped
18 )
19 print(resp)
Resize instances in batches
You can resize instances in batches 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# Execute the operation of resizing instances in batches
15 resp = client.batch_resize_instance(instance_ids=['i-***'], # Collection of instance IDs, maximum 30 IDs
16 spec='bcc.g5.c8m32', # Specifications of instances resized in batches
17 subnet_id='sbn-***', # Subnet ID
18 logical_zone='cn-bj-d', # Availability zone
19 internal_ip_v4='ipv4', Intranet IP
20 )
21 print(resp)
Query instance specifications for resizing
You can query instance specifications for resizing 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# Execute the operation of querying resizable instance specifications
15 resp = client.list_available_resize_specs(instance_ids=['i-***'], # Instance ID. Only one of instance ID, instance specification or specification family ID can be specified at one time.
16# spec='bcc.g5.c2m8', # Instance specification
17# spec_id='subnet_id', # Specification family ID
18# logical_zone='zone_name', # Instance availability zone. If spec or specId is specified, this parameter is required.
19 )
20 print(resp)
Convert to prepay in batch
You can batch convert the billing type of instances to prepay using the following code. To change the billing type of associated CDS resources, you must specify all relevant CDS IDs in the CdsList to be converted to prepay. If converting all CDS resources, specify "all".
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.bcc_model import PayTimingChangeReqModel
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 req1 = PayTimingChangeReqModel('i-***', cdsList=['all'], autoPay=True, duration=1)
16 req2 = PayTimingChangeReqModel('i-***', cdsList=['all'], duration=1)
17 req = [req1, req2]
18# req collection elements, maximum 100
19 resp = client.batch_change_instance_to_prepay(req)
20 print(resp)
Convert to postpay in batch
You can batch convert the billing type of instances to postpay using the following code. To change the billing type of associated CDS resources, you must specify all relevant CDS IDs in the CdsList to be converted to postpay. If converting all CDS resources, specify "all".
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.bcc_model import PayTimingChangeReqModel
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 req1 = PayTimingChangeReqModel('i-***', cdsList=['all'])
16 req2 = PayTimingChangeReqModel('i-***', cdsList=['all'])
17 req = [req1, req2]
18# req collection elements, maximum 100
19 resp = client.batch_change_instance_to_postpay(req)
20 print(resp)
Retrieve a list of instance roles
You can retrieve a list of instance roles 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# Execute the operation of retrieving the list of instance roles
15 resp = client.list_instance_roles()
16 print(resp)
Bind a role
You can bind a role to a BCC 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# Execute the operation of binding a role
15 resp = client.bind_instance_role(['i-***', 'i-***'], # List of Instance IDs
16 role_name='BCC_Test', # Role name
17 )
18 print(resp)
Unbind a role
You can unbind a role from a BCC 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# Execute the operation of unbinding a role
15 resp = client.unbind_instance_role(['i-***', 'i-***'], # List of instance IDs
16 role_name='BCC_Test' # Role name
17 )
18 print(resp)
Add IPv6
You can add IPv6 to a BCC 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.add_ipv6(
15 instance_id='i-***', # Instance ID
16 ipv6_address='240c:4081:8005:ca0b:***:***', # IPv6 address; an empty value indicates automatic IPv6 assignment
17 reboot=True) # Whether to restart. True indicates a restart; empty indicates no restart
18 print(resp)
Release IPv6
You can release IPv6 from a BCC 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.delete_ipv6(
15 instance_id='i-***', # Instance ID
16 reboot=True) # Whether to restart. True indicates a restart; empty indicates no restart
17 print(resp)
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 from 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_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 )
19 print(res)
