This topic describes how to use the ROS Python SDK to create and manage stacks.

Background information

You can also use the ROS console or API to create and manage stacks.

Preparations

  1. Download and install the Python SDK.
    1. Use pip to install aliyun-python-sdk-core.
      pip install aliyun-python-sdk-core
      Note aliyun-python-sdk-core is included in all Python SDKs of Alibaba Cloud. If the installation fails and an error message occurs, a likely reason is that you do not have the write access permission to the Python installation path. In this case, modify the preceding command to sudo pip install aliyun-python-sdk-core.
    2. Install the ROS SDK.
      pip install aliyun-python-sdk-ros
  2. Initialize the SDK.
    1. Import the required libraries.
      from aliyunsdkcore.client import AcsClient
      from aliyunsdkros.request.v20190910.CreateStackRequest import CreateStackRequest
      from aliyunsdkros.request.v20190910.GetStackRequest import GetStackRequest
      from aliyunsdkros.request.v20190910.DeleteStackRequest import DeleteStackRequest
      from aliyunsdkros.request.v20190910.DescribeRegionsRequest import DescribeRegionsRequest
      from aliyunsdkros.request.v20190910.ListStacksRequest import ListStacksRequest
    2. Initialize the SDK client.
      AK = '<yourAccessKeyId>'
      SECRET = '<yourAccessKeySecrect>'
      Region = '<yourRegionId>'
      client = AcsClient(AK, SECRET, Region) 
Notice
  • The commands in the example run in Linux Shell. Windows or DOS users need to modify the syntax accordingly.
  • The ROS Python SDK is only compatible with Python 2.7 and later versions.
  • ROS Python SDK 3.0.0 and later versions support the 20150901 and 20190910 versions of the ROS API.

Query the list of available regions

You can use the Python SDK to query the list of available regions.

def describe_region():
    """describe regions list """
    request = DescribeRegionsRequest()
    request.set_connect_timeout(10000)
    request.set_read_timeout(10000)
    request.set_accept_format("json")
    response = client.do_action_with_exception(request)

    return response.decode("utf-8")

Create a stack

To create a stack, you must specify the following parameters:
  • StackName: the name of the stack to be created. The name must be unique in a userspace.
  • TimeoutInMinutes: the timeout period that is specified for the stack creation request. Unit: minutes. A failure will occur if the stack cannot be created within the specified period of time.
  • TemplateBody: the template based on which the stack is created.
  • TemplateURL: the URL for the file that contains the template body. You must specify either the TemplateBody parameter or the TemplateURL parameter. But you cannot specify both of them.
  • Parameters: the parameters required to create the stack. Keys must be defined in the template.
stack_name = "MyStack"
timeout = 10
template_body = """
    {
      "ROSTemplateFormatVersion": "2015-09-01",
      "Parameters": {
        "VpcName": {
          "Type": "String",
          "Description": "Vpc Name",
          "Label": "Vpc Name"
        },
        "CidrBlock": {
          "Type": "String",
          "Description": "Vpc CidrBlock",
          "Label": "Vpc CidrBlock"
         }
        },
      "Resources": {
        "Vpc": {
          "Type": "ALIYUN::ECS::VPC",
          "Properties": {
            "CidrBlock": {
              "Ref": "CidrBlock"
            },
            "VpcName": {
              "Ref": "VpcName"
            }
          }
        }
      }
    }
"""
params = [
    {
        "ParameterValue": "192.168.0.0/16",
        "ParameterKey": "CidrBlock"
    },
    {
        "ParameterValue": "TestVpc",
        "ParameterKey": "VpcName"
    }
]
def create_stack():
    """create stack"""
    request = CreateStackRequest()
    request.set_accept_format("json")
    request.set_StackName(stack_name)
    request.set_TimeoutInMinutes(timeout)
    request.set_TemplateBody(template_body)
    request.set_Parameterss(params)
    response = client.do_action_with_exception(request)

    return response.decode("utf-8")

Query information about a stack

To query information about a stack, you must provide the stack ID.
def get_stack():
    """get descriptions of the stack"""
    request = GetStackRequest()
    request.set_accept_format("json")
    request.set_StackId(stack_id)
    response = client.do_action_with_exception(request)

    return response.decode("utf-8")

Delete a stack

To delete a stack, you must provide the stack ID.
def delete_stack():
    """delete stack"""
    request = DeleteStackRequest()
    request.set_accept_format("json")
    request.set_StackId(stack_id)
    response = client.do_action_with_exception(request)

    return response.decode("utf-8")

Example

import json
from time import sleep
from aliyunsdkcore.client import AcsClient
from aliyunsdkros.request.v20190910.CreateStackRequest import CreateStackRequest
from aliyunsdkros.request.v20190910.GetStackRequest import GetStackRequest
from aliyunsdkros.request.v20190910.DeleteStackRequest import DeleteStackRequest
from aliyunsdkros.request.v20190910.DescribeRegionsRequest import DescribeRegionsRequest
from aliyunsdkros.request.v20190910.ListStacksRequest import ListStacksRequest


AK = '<yourAccessKeyId>'
SECRET = '<yourAccessKeySecret>'
Region = '<yourRegionId>'  # For example, 'cn-beijing' and 'cn-hangzhou'.

client = AcsClient(AK, SECRET, Region)


def describe_region():
    """describe regions list """
    request = DescribeRegionsRequest()
    request.set_connect_timeout(10000)
    request.set_read_timeout(10000)
    request.set_accept_format("json")
    response = client.do_action_with_exception(request)

    return response.decode("utf-8")


def create_stack(stack_name, timeout, template_body, params=[]):
    """create stack"""
    request = CreateStackRequest()
    request.set_accept_format("json")
    request.set_StackName(stack_name)
    request.set_TimeoutInMinutes(timeout)
    request.set_TemplateBody(template_body)
    response = client.do_action_with_exception(request)

    return response.decode("utf-8")


def get_stack(stack_id):
    """get descriptions of the stack"""
    request = GetStackRequest()
    request.set_accept_format("json")
    request.set_StackId(stack_id)
    response = client.do_action_with_exception(request)

    return response.decode("utf-8")


def delete_stack(stack_id):
    """delete stack"""
    request = DeleteStackRequest()
    request.set_accept_format("json")
    request.set_StackId(stack_id)
    response = client.do_action_with_exception(request)

    return response.decode("utf-8")


if __name__ == '__main__':
    test_template = """
        {
          "ROSTemplateFormatVersion": "2015-09-01",
          "Parameters": {
            "VpcName": {
              "Type": "String",
              "Description": "Vpc Name",
              "Label": "Vpc Name"
            },
            "CidrBlock": {
              "Type": "String",
              "Description": "Vpc CidrBlock",
              "Label": "Vpc CidrBlock"
             }
            },
          "Resources": {
            "Vpc": {
              "Type": "ALIYUN::ECS::VPC",
              "Properties": {
                "CidrBlock": {
                  "Ref": "CidrBlock"
                },
                "VpcName": {
                  "Ref": "VpcName"
                }
              }
            }
          }
        }
    """

    parameters = [
        {"ParameterKey": "CidrBlock", "ParameterValue": "192.168.0.0/16"},
        {"ParameterKey": "VpcName", "ParameterValue": "TestVpc"}
    ]

    describe_region()
    stack = create_stack('MyStack', 10, test_template, parameters)
    get_stack(json.loads(stack)["StackId"])
    sleep(3)  # Wait for the stack to be created successfully.
    delete_stack(json.loads(stack)["StackId"])