You can use Resource Orchestration Service (ROS) to create stacks, create resources
in the stacks, and then add tags to the resources. This facilitates subsequent O&M
on the resources. ROS allows you to add tags to or update tags for multiple cloud
resources at a time to improve O&M efficiency.
Background information
In this topic, ROS SDK for Python is used to create stacks. For more information,
see Use ROS SDK for Python.
Add the same tag to multiple cloud resources
In the following example, a virtual private cloud (VPC) named mytest-vpc
and a vSwitch named mytest-vsw-h
are created by using a stack and the app:test
tag is added to the VPC and vSwitch.
- Create a template.
{
"ROSTemplateFormatVersion": "2015-09-01",
"Resources": {
"VPC": {
"Type": "ALIYUN::ECS::VPC",
"Properties": {
"VpcName": "mytest-vpc"
}
},
"VSwitch": {
"Type": "ALIYUN::ECS::VSwitch",
"Properties": {
"VpcId": { "Ref": "VPC" },
"ZoneId": "cn-hangzhou-h",
"CidrBlock": "172.16.0.0/24",
"VSwitchName": "mytest-vsw-h"
}
}
}
}
You can use the template to create a VPC and a vSwitch.
- Create a stack and add the
app:test
tag to the VPC and vSwitch. # pip install alibabacloud_ros20190910
from alibabacloud_ros20190910.client import Client as ROS20190910Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_ros20190910 import models as ros20190910_models
AK = '<AccessKeyId>'
SECRET = '<AccessKeySecret>'
REGION = '<RegionId>' # The region ID, such as 'cn-beijing' and 'cn-hangzhou'.
template = '''
<Template>
'''
def create_client(
access_key_id: str,
access_key_secret: str,
) -> ROS20190910Client:
"""
Use your AccessKey pair to initialize the client.
@param access_key_id:
@param access_key_secret:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
# Your AccessKey ID.
access_key_id=access_key_id,
# Your AccessKey secret.
access_key_secret=access_key_secret,
)
# The domain name that you want to access.
config.endpoint = 'ros.aliyuncs.com'
return ROS20190910Client(config)
def create_stack(region_id, stack_name, template_body, timeout_in_minutes=40, parameters=[], tags=[]):
"""create stack"""
create_stack_request = ros20190910_models.CreateStackRequest(
region_id=region_id,
stack_name=stack_name,
# If the length of the template body exceeds the upper limit, we recommend that you use the TemplateURL parameter to prevent request failures caused by excessively long URLs.
# You can also add parameters to the HTTP POST request body.
template_body=template_body,
timeout_in_minutes=timeout_in_minutes,
parameters=parameters,
tags=tags,
disable_rollback=True
)
response = client.create_stack(create_stack_request)
return response.body
client = create_client(AK, SECRET)
tags = [
ros20190910_models.CreateStackRequestTags(
key='app',
value='test'
)
]
print(create_stack(REGION, 'MyStack', template, tags=tags, timeout_in_minutes=60))
Parameters:
- Replace <AccessKeyId> and <AccessKeySecret> with your AccessKey ID and AccessKey secret.
- Replace <Template> with the template that you created in Step 1.
- Optional:Log on to the VPC console and check whether
the app:test
tag is added to the VPC and vSwitch.
Add different tags to multiple cloud resources
In the following example, a VPC named
mytest-vpc
and a vSwitch named
mytest-vsw-h
are created by using a stack, the
app:test
tag is added to the VPC and vSwitch, and the
group:test
tag is added to the vSwitch. The following results are expected to return:
- The
app:test
tag is added to the mytest-vpc
VPC.
- The
app:test
and group:test
tags are added to the mytest-vsw-h
vSwitch.
- Create a template.
{
"ROSTemplateFormatVersion": "2015-09-01",
"Resources": {
"VPC": {
"Type": "ALIYUN::ECS::VPC",
"Properties": {
"VpcName": "mytest-vpc"
}
},
"VSwitch": {
"Type": "ALIYUN::ECS::VSwitch",
"Properties": {
"VpcId": { "Ref": "VPC" },
"ZoneId": "cn-hangzhou-h",
"CidrBlock": "172.16.0.0/24",
"VSwitchName": "mytest-vsw-h",
"Tags": [{ "Key": "group", "Value": "test" }]
}
}
}
}
You can use the template to create a VPC and a vSwitch, and add the group:test
tag to the vSwitch.
- Create a stack and add the
app:test
tag to the VPC and vSwitch. # pip install alibabacloud_ros20190910
from alibabacloud_ros20190910.client import Client as ROS20190910Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_ros20190910 import models as ros20190910_models
AK = '<AccessKeyId>'
SECRET = '<AccessKeySecret>'
REGION = '<RegionId>' # The region ID, such as 'cn-beijing' and 'cn-hangzhou'.
template = '''
<Template>
'''
def create_client(
access_key_id: str,
access_key_secret: str,
) -> ROS20190910Client:
"""
Use your AccessKey pair to initialize the client.
@param access_key_id:
@param access_key_secret:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
# Your AccessKey ID.
access_key_id=access_key_id,
# Your AccessKey secret.
access_key_secret=access_key_secret,
)
# The domain name that you want to access.
config.endpoint = 'ros.aliyuncs.com'
return ROS20190910Client(config)
def create_stack(region_id, stack_name, template_body, timeout_in_minutes=40, parameters=[], tags=[]):
"""create stack"""
create_stack_request = ros20190910_models.CreateStackRequest(
region_id=region_id,
stack_name=stack_name,
# If the length of the template body exceeds the upper limit, we recommend that you use the TemplateURL parameter to prevent request failures caused by excessively long URLs.
# You can also add parameters to the HTTP POST request body.
template_body=template_body,
timeout_in_minutes=timeout_in_minutes,
parameters=parameters,
tags=tags,
disable_rollback=True
)
response = client.create_stack(create_stack_request)
return response.body
client = create_client(AK, SECRET)
tags = [
ros20190910_models.CreateStackRequestTags(
key='app',
value='test'
)
]
print(create_stack(REGION, 'MyStack', template, tags=tags, timeout_in_minutes=60))
Parameters:
- Replace <AccessKeyId> and <AccessKeySecret> with your AccessKey ID and AccessKey secret.
- Replace <Template> with the template that you created in Step 1.
- Optional:Log on to the VPC console and check whether
the app:test
tag is added to the VPC and whether the app:test
and group:test
tags are added to the vSwitch.
Update tags for multiple cloud resources
In the following example, the app:test
tag that is added to the mytest-vpc
VPC and the mytest-vsw-h
vSwitch in the Add the same tag to multiple cloud resources section is updated to the app:normal
tag.
- Create a stack and add the
app:normal
tag to the VPC and vSwitch. # pip install alibabacloud_ros20190910
from alibabacloud_ros20190910.client import Client as ROS20190910Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_ros20190910 import models as ros20190910_models
AK = '<AccessKeyId>'
SECRET = '<AccessKeySecret>'
REGION = '<RegionId>' # The region ID, such as 'cn-beijing' and 'cn-hangzhou'.
template = '''
<Template>
'''
def create_client(
access_key_id: str,
access_key_secret: str,
) -> ROS20190910Client:
"""
Use your AccessKey pair to initialize the client.
@param access_key_id:
@param access_key_secret:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
# Your AccessKey ID.
access_key_id=access_key_id,
# Your AccessKey secret.
access_key_secret=access_key_secret,
)
# The domain name that you want to access.
config.endpoint = 'ros.aliyuncs.com'
return ROS20190910Client(config)
def create_stack(region_id, stack_name, template_body, timeout_in_minutes=40, parameters=[], tags=[]):
"""create stack"""
create_stack_request = ros20190910_models.CreateStackRequest(
region_id=region_id,
stack_name=stack_name,
# If the length of the template body exceeds the upper limit, we recommend that you use the TemplateURL parameter to prevent request failures caused by excessively long URLs.
# You can also add parameters to the HTTP POST request body.
template_body=template_body,
timeout_in_minutes=timeout_in_minutes,
parameters=parameters,
tags=tags,
disable_rollback=True
)
response = client.create_stack(create_stack_request)
return response.body
client = create_client(AK, SECRET)
tags = [
ros20190910_models.CreateStackRequestTags(
key='app',
value='normal'
)
]
print(create_stack(REGION, 'MyStack', template, tags=tags, timeout_in_minutes=60))
Parameters:
- Replace <AccessKeyId> and <AccessKeySecret> with your AccessKey ID and AccessKey secret.
- Replace <Template> with the template that you created in Step 1
- Optional:Log on to the VPC console and check whether
the app:test tag of the VPC and vSwitch is updated to the app:normal
tag.