前提条件

安装 ECI SDK

ECI Python SDK 的 PyPI 地址:https://pypi.org/project/aliyun-python-sdk-eci/

安装:

pip install aliyun-python-sdk-eci==1.0.3

如果提示超时,则添加超时选项:

pip --default-timeout=100 install aliyun-python-sdk-eci==1.0.3

如果需要升级到最新版本,则使用以下命令:

pip install --upgrade aliyun-python-sdk-eci

Demo示例

创建一个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))

成功返回数据

{"ContainerGroupId":"eci-bp1brvmc6xtpz2ex****","RequestId":"ED7ED3FA-AA2F-47CE-B45A-122B446751D8"}

删除指定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))

成功返回数据

{"RequestId":"81F15B16-B5AD-4081-924A-F91D9FF8E402"}