All Products
Search
Document Center

Use SDK for Python

Last Updated: Aug 16, 2021

This topic describes how to use Elastic Container Instance SDK for Python and provides an example.

Install the SDK

You can use Python Package Index (PyPI) to install Elastic Container Instance SDK for Python by running the following command:

pip install aliyun-python-sdk-eci

To update the SDK for Python to the latest version, run the following command:

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

You can also visit the PyPI website to download aliyun-python-sdk-eci.

Sample code

Sample code used to create an elastic container instance

#!/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,
    "VolumeMounts": [
      "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))

Sample success response:

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

Sample code used to delete an elastic container instance

#!/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))

Sample success response:

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

If your SDK version does not support specific input parameters, you can use the following method to add the parameters and make them take effect: request.add_query_param("key", "value").