Aliyun VPC SDK for Python

raw JSON →
3.0.48 verified Fri May 01 auth: no python

The official Alibaba Cloud VPC (Virtual Private Cloud) SDK for Python, version 3.0.48, released on 2024-01-01. Provides APIs to manage VPC resources such as VPCs, vSwitches, route tables, and NAT gateways. Part of the Alibaba Cloud Python SDK family with monthly releases.

pip install aliyun-python-sdk-vpc
error ModuleNotFoundError: No module named 'aliyunsdkvpc'
cause The package is not installed or the import path is incorrect.
fix
Install: pip install aliyun-python-sdk-vpc. Then import from aliyunsdkvpc.
error AttributeError: module 'aliyunsdkvpc.request' has no attribute 'v20160428'
cause Trying to import from the package without specifying the correct submodule path.
fix
Use the full import: from aliyunsdkvpc.request.v20160428.CreateVpcRequest import CreateVpcRequest
breaking In version 3.0.0+, all request classes must be imported from version-specific modules (e.g., v20160428). Direct imports from aliyunsdkvpc.request are deprecated and will raise ImportError.
fix Use from aliyunsdkvpc.request.v20160428.<ApiName> import <ApiName>Request
deprecated The method do_action() is deprecated in favor of do_action_with_exception(), which raises exceptions on API errors instead of returning error codes.
fix Replace do_action() with do_action_with_exception()
gotcha The region ID must be specified when creating the client, not in each request. Setting region on the request object is ignored for VPC.
fix Pass region to AcsClient constructor; do not use set_RegionId on request

Creates a VPC in cn-hangzhou region. Requires Alibaba Cloud credentials set in environment variables.

from aliyunsdkcore.client import AcsClient
from aliyunsdkvpc.request.v20160428.CreateVpcRequest import CreateVpcRequest
import os

client = AcsClient(
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', ''),
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', ''),
    'cn-hangzhou'
)
request = CreateVpcRequest()
request.set_VpcName('my-vpc')
request.set_CidrBlock('10.0.0.0/16')
response = client.do_action_with_exception(request)
print(response.decode('utf-8'))