Create an Anycast EIP by calling the AllocateAnycastEipAddress operation with the Python SDK.
Step 1: View the API references
The List of operations by function shows that AllocateAnycastEipAddress creates an Anycast EIP. Check the API documentation for required parameters and permissions.
Step 2: Create and authorize a RAM user
An Alibaba Cloud account has full API access. Use a RAM user for day-to-day API calls and O&M.
Call the operation with an Alibaba Cloud account, RAM user, or RAM role. The Identity topic explains the differences.
This example uses a RAM user.
-
Create a RAM user.
-
Log on to the RAM console.
-
In the left-side navigation pane, choose Identities > Users.
-
On the Users page, click Create User.
-
On the Create User page, set the User Login Name and Display Name. For Access Configuration, select Console Access.
-
Click OK.
Record the RAM user's logon name and password. You need these credentials to log on to OpenAPI Explorer.
-
-
Grant permissions to the RAM user.
-
Access the RAM user list.
-
Find the target RAM user and click Attach Policy in the Actions column.
-
Enter
AliyunAnycastEIPin the search box and select the AliyunAnycastEIPFullAccess policy.Available Anycast EIP permissions. To create custom policies, use Authorization.
-
AliyunAnycastEIPReadOnlyAccess: read-only access to Anycast EIPs.
-
AliyunAnycastEIPFullAccess: the permission to manage Anycast EIPs.
-
-
Click OK to grant the permissions.
-
-
Go to the details page of the RAM user. On the AccessKey tab, click Create AccessKey.
ImportantThe AccessKey secret is displayed only during creation. You cannot retrieve it afterward. Download the CSV file and store it securely.
Step 3: Prepare the environment
-
Download and install Python.
-
Build a Python development environment.
This example uses PyCharm on Windows. Build a Python development environment on Windows.
-
Configure environment variables.
Configure environment variables so the SDK can read your access credentials. Configure environment variables in Linux, macOS, and Windows.
ImportantRestart your IDE or development tools after configuring environment variables.
-
Install environment dependencies.
Install the Anycast EIP SDK and dependencies:
pip install alibabacloud_eipanycast20200309==1.1.2 pip install alibabacloud_tea_console
Step 4: Download the sample code
-
Log on to the API Debugging page in the OpenAPI Portal as the RAM user.
-
Select the Anycast EIP cloud service.
-
In the left-side navigation pane, find and click the AllocateAnycastEipAddress operation. The region is China (Hangzhou) by default. For the ServiceLocation parameter, enter international.
-
Click Initiate Call.
The Call Result section shows the response details. A status code of 200 indicates success.
-
Obtain the sample code of the SDK.
OpenAPI Explorer provides SDKs for Java, Go, Python, Node.js, TypeScript, PHP, and C++. SDKs handle signing, timeouts, retries, and return structured responses.
This example uses the Python SDK. Other languages follow the same workflow.
-
On the SDK Sample tab, select your preferred SDK version and language. V2.0 is recommended. Differences between V1.0 SDK and V2.0 SDK.
-
Click Download Project to download the complete SDK project, and then extract the files.
Note-
In the upper-right corner of the code editor, you can click the
icon to copy the SDK sample code. -
Click SDK Installation Information for package name, version, and installation commands.
-
-
Step 5: Run the SDK sample code
-
Open PyCharm, choose File > Open, and select the extracted project folder. In the Creating Virtual Environment dialog box, click OK and wait for dependencies to download.
-
Run the sample code.
In the PyCharm file tree, expand alibabacloud_sample and double-click sample.py. Click the Run button to execute the code.
A response with
"statusCode":200indicates success.After the script runs successfully, the terminal displays a response similar to the following:
[LOG] {"headers":{"date":"Fri, 28 Jun 2024 06:57:00 GMT","content-type":"application/json;charset=utf-8","content-length":"121","connection":"keep-alive","keep-alive":"timeout=25","access-control-allow-origin":"*","access-control-expose-headers":"*","x-acs-request-id":"1CB825B4-3E78-563D-A6BC-799844E1E2A7","x-acs-trace-id":"51ca6d471e36724cd0bfd1be5b81567b","etag":"1twpU5vKwy5avZiRACIRhOA1"},"statusCode":200,"body":{"AnycastId":"aeip-bp1f1hxxx","OrderId":"5xxx","RequestId":"1CB825B4-3E78-563D-A6BC-799844E1E2A7"}} Process finished with exit code 0