Before you begin
Install the ECI Python SDK
You can use Python Package Index (PyPI) to install the Python SDK from https://pypi.org/project/aliyun-python-sdk-eci/.
Run the following command to install the Python SDK:
pip install aliyun-python-sdk-eci==1.0.3
If a message appears indicating that the installation times out, add the default timeout period in the command as follows:
pip --default-timeout=100 install aliyun-python-sdk-eci==1.0.3
Run the following command to upgrade the Python SDK to the latest version:
pip install --upgrade aliyun-python-sdk-eci
Sample code
Create an ECI
#! /usr/bin/env python
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkeci.request.v20180808.CreateContainerGroupRequest import CreateContainerGroupRequest
client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')
request = CreateContainerGroupRequest()
request.set_accept_format('json')
request.set_SecurityGroupId("sg-bp13xej624v0pfqn****")
request.set_VSwitchId("vsw-bp1dmqtg608k99wmb****")
request.set_ContainerGroupName("test")
request.set_Containers([
{
"Image": "nginx",
"Name": "nginx",
"Cpu": 2,
"Memory": 4,
"VolumeMount": [
"emptyVolume"
],
"Commands": [
"/bin/bash"
],
"Args": [
"-c",
"date"
]
}
])
request.set_Tags([
{
"Key": "env",
"Value": "test"
}
])
request.set_Volumes([
{
"Name": "emptyVolume",
"Type": "EmptyDirVolume"
}
])
response = client.do_action_with_exception(request)
print(str(response))
Success response
{"ContainerGroupId":"eci-bp1brvmc6xtpz2ex****","RequestId":"ED7ED3FA-AA2F-47CE-B45A-122B446751D8"}
Delete an ECI
#! /usr/bin/env python
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkeci.request.v20180808.DeleteContainerGroupRequest import DeleteContainerGroupRequest
client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')
request = DeleteContainerGroupRequest()
request.set_accept_format('json')
request.set_ContainerGroupId("eci-bp1brvmc6xtpz2ex****")
response = client.do_action_with_exception(request)
# python2: print(response)
print(str(response))
Success response
{"RequestId":"81F15B16-B5AD-4081-924A-F91D9FF8E402"}