Function

Last Updated: Feb 08, 2018

Resource Orchestration Service provides multiple built-in functions to help you manage your stacks. You can use built-in functions when you are specifying Resources and Outputs.

The built-in functions can be used in template editing: Fn::Base64, Fn::FindInMap, Fn::GetAtt, Fn::Join, Fn::Select, Ref, Fn::GetAZs, Fn::Replace, Fn::Split, Fn::Equals, Fn::And, Fn::Or, Fn::Not, Fn::If, Fn::ListMerge, Fn::GetJsonValue, and Fn::MergeMapToList.

Fn::Base64

The built-in function Fn::Base64 returns the Base64-encoded results of entered strings.

Statement

  1. "Fn::Base64" : stringToEncode

Parameters

stringToEncode: the string to be encoded in Base64.

Return values

An original string in Base64.

Example

  1. "Fn::Base64" : "string to encode"

Fn::FindInMap

The built-in function Fn::FindInMap returns the value corresponding to the key in the double layer mapping defined in Mappings.

Statement

  1. "Fn::FindInMap" : [ "MapName", "TopLevelKey", "SecondLevelKey"]

Parameters

MapName: the mapping ID defined in Mappings, including a key and value.

TopLevelKey: the first level key, whose value is a key/value pair list.

SecondLevelKey: the second level key, whose value is a string or number.

Return values

The value allocated to SecondLevelKey.

Example

The following example shows how to use Fn::FindInMap.

In the following example, the ImageId attribute must be specified when the resource named WebServer is created. The Mappings describes the ImageId mappings differentiated according to regions. The Parameters describes the regions that must be specified by template users. Fn::FindInMap finds the corresponding ImageId mapping in RegionMap, according to the region specified by template users. Then, find the ImageId corresponding to 32 in the mapping.

  • MapName can be set according to personal preference. It is "RegionMap" in this example.

  • TopLevelKey is set to the region for creating a stack, which is determined by using {"Ref": "regionParam"} in this example.

  • SecondLevelKey is set to the expected architecture, which is "32" in this example.

  1. {
  2. "ROSTemplateFormatVersion": "2015-09-01",
  3. "Parameters": {
  4. "regionParam": {
  5. "Description": " Select the region for creating an ECS",
  6. "Type": "String",
  7. "AllowedValues": ["hangzhou", "beijing"]
  8. }
  9. },
  10. "Mappings" : {
  11. "RegionMap" : {
  12. "hangzhou" : { "32" : "m-25l0rcfjo", "64" : "m-25l0rcfj1" },
  13. "beijing" : { "32" : "m-25l0rcfj2", "64" : "m-25l0rcfj3" }
  14. }
  15. },
  16. "Resources": {
  17. "WebServer": {
  18. "Type": "ALIYUN::ECS::Instance",
  19. "Properties": {
  20. "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "regionParam" }, "32"]},
  21. "InstanceType": "ecs.t1.small",
  22. "SecurityGroupId": "sg-25zwc3se0",
  23. "ZoneId": "cn-beijing-b",
  24. "Tags": [{
  25. "Key": "key1",
  26. "Value": "value1"
  27. },{
  28. "Key": "key2",
  29. "Value": "value2"
  30. }
  31. ]
  32. }
  33. }
  34. }
  35. }

Supported functions

You can use the following functions in the Fn::FindInMap function:

  • Fn::FindInMap

  • Ref

Fn::GetAtt

The built-in function Fn::GetAtt returns the attribute values of resources.

Statement

  1. "Fn::GetAtt": [ "resourceID", "attributeName" ]

Parameters

resourceID: the ID of the target resource.

attributeName: the attribute name of the target resource.

Return values

Attribute value.

Example

In this example, the returned Resource ID is the ImageId attribute of MyEcsInstance.

  1. "Fn::GetAtt" : [ "MyEcsInstance" , "ImageID" ]

Fn::Join

The built-in function Fn::Join joins a group of values, which are separated using specific separators.

Statement

  1. { "Fn::Join" : [ "delimiter", [ "string1", "string2", ... ] }

Parameter

delimiter: separator. The separator can be left blank, so that all the values are joined directly.

[ "string1", "string2", ... ]: a list of joined values.

Return values

Joined strings.

Example

  1. "Fn::Join" : [ ",", [ "a", "b", "c" ] ]

Returned value: "a,b,c".

Supported functions

  • Fn::Base64

  • Fn::GetAtt

  • Fn::Join

  • Fn::Select

  • Ref

Fn::Select

The built-in function Fn::Select returns the single data element in the data element list through the index.

Statement

The data element list is an array:

  1. "Fn::Select" : [ "index", [ "value1", "value2", ... ] ]

The data element list is a mapping table:

  1. "Fn::Select" : [ "index", { "key1": "value1", ... } ]

Parameters

index: the index of data elements to be retrieved. If the data element list is an array, the index is a value between 0 and N-1 (N indicates the amount of elements in the array). If the data element list is a mapping table, the index is a key in the mapping table.

If the corresponding value of index cannot be found, the system returns a blank string.

Return values

Selected data element.

Example

If the data element list is an array, edit as the following:

  1. { "Fn::Select" : [ "1", [ "apples", "grapes", "oranges", "mangoes" ] ] }

The returned value in this example: "grapes".

If the data element list is a mapping table, edit as the following:

  1. { "Fn::Select" : [ "key1", [ "key1": "grapes", "key2": "mangoes" ] ] }

The returned value in this example: "grapes".

If the data element list is a CommaDelimitedList, edit as the following:

  1. "Parameters" : {
  2. "userParam": {
  3. "Type": "CommaDelimitedList",
  4. "Default": "10.0.100.0/24, 10.0.101.0/24, 10.0.102.0/24"
  5. }
  6. }
  7. "Resources": {
  8. "resourceID": {
  9. "Properties": {
  10. "CidrBlock": { "Fn::Select" : [ "0", {"Ref": "userParam"} ] }
  11. }
  12. }
  13. },

Supported functions

For the Fn::Select index value, you can use the Ref function.

For the Fn::Select list of an object, you can use the following functions:

  • Fn::Base64

  • Fn::FindInMap

  • Fn::GetAtt

  • Fn::Join

  • Fn::Select

  • Ref

Ref

The built-in function Ref returns the value of a specified parameter or resource.

If the specified parameter is a Resource ID, the system returns the value of the resource. Otherwise, the system deems the specified parameter to be a parameter and attempts to return the parameter value.

Statement

  1. "Ref" : "logicalName"

Parameters

logicalName: the logical name of a resource or parameter to which you want to make a reference.

Return values

The value of the resource or parameter.

Example

The following example shows how to use the Ref function to specify regionParam as the region parameter for RegionMap of WebServer.

  1. {
  2. "ROSTemplateFormatVersion": "2015-09-01",
  3. "Parameters": {
  4. "regionParam": {
  5. "Description": " Select the region for creating an ECS",
  6. "Type": "String",
  7. "AllowedValues": ["hangzhou", "beijing"]
  8. }
  9. },
  10. "Mappings" : {
  11. "RegionMap" : {
  12. "hangzhou" : { "32" : "m-25l0rcfjo", "64" : "m-25l0rcfj1" },
  13. "beijing" : { "32" : "m-25l0rcfj2", "64" : "m-25l0rcfj3" }
  14. }
  15. },
  16. "Resources": {
  17. "WebServer": {
  18. "Type": "ALIYUN::ECS::Instance",
  19. "Properties": {
  20. "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "regionParam" }, "32"]},
  21. "InstanceType": "ecs.t1.small",
  22. "SecurityGroupId": "sg-25zwc3se0",
  23. "ZoneId": "cn-beijing-b",
  24. "Tags": [{
  25. "Key": "tiantt",
  26. "Value": "ros"
  27. },{
  28. "Key": "tiantt1",
  29. "Value": "ros1"
  30. }
  31. ]
  32. }
  33. }
  34. }
  35. }

Supported functions

No function can be used in the Ref function. A string must be specified as the logical ID of the resource.

Fn::GetAZs

The built-in function Fn::GetAZs returns the available zone list of a specified region.

Statement

  1. "Fn::GetAZs": "region"

Parameters

region: the region ID.

Return values

The list of available zones under the specified region.

Example

In this example, an ECS instance is created in the first available zone of the specified region.

  1. {
  2. "ROSTemplateFormatVersion" : "2015-09-01",
  3. "Resources" : {
  4. "WebServer": {
  5. "Type": "ALIYUN::ECS::Instance",
  6. "Properties": {
  7. "ImageId" : "centos7u2_64_40G_cloudinit_20160728.raw",
  8. "InstanceType": "ecs.n1.tiny",
  9. "SecurityGroupId": "sg-2zedcm7ep5quses05fs4",
  10. "Password": "Ros12345",
  11. "AllocatePublicIP": true,
  12. "InternetChargeType": "PayByTraffic",
  13. "InternetMaxBandwidthIn": 100,
  14. "InternetMaxBandwidthOut": 100,
  15. "SystemDiskCategory": "cloud_efficiency",
  16. "IoOptimized": "optimized",
  17. "ZoneId": {"Fn::Select": ["0", {"Fn::GetAZs": {"Ref": "ALIYUN::Region"}}]}
  18. }
  19. }
  20. },
  21. "Outputs": {
  22. "InstanceId": {
  23. "Value" : {"Fn::GetAtt": ["WebServer","InstanceId"]}
  24. },
  25. "PublicIp": {
  26. "Value" : {"Fn::GetAtt": ["WebServer","PublicIp"]}
  27. }
  28. }
  29. }

Supported functions

  • Fn::Base64

  • Fn::FindInMap

  • Fn::GetAtt

  • Fn::Join

  • Fn::Select

  • Ref

Fn::Replace

The built-in function Fn::Replace replaces the specified sub-string in a string with a new sub-string.

Statement

  1. { "Fn::Replace" : [ {"object_key": "object_value"}, "object_string"]

Parameters

{"object_key": "object_value"}:

  • object_key: the string to be replaced.
  • object_value: the replaced new string.

object_string: the target string whose object_key is to be replaced.

Return values

The string after replacement.

Example

The following example show that replace "print" in the specified script in UserData with "echo".

  1. {
  2. "ROSTemplateFormatVersion" : "2015-09-01",
  3. "Resources" : {
  4. "WebServer": {
  5. "Type": "ALIYUN::ECS::Instance",
  6. "Properties": {
  7. "ImageId" : "centos_7_2_64_40G_base_20170222.vhd",
  8. "InstanceType": "ecs.n1.medium",
  9. "SecurityGroupId": "sg-94q49gota",
  10. "Password": "MytestPassword1234",
  11. "IoOptimized": "optimized",
  12. "VSwitchId": "vsw-94vdvonyi",
  13. "VpcId": "vpc-949uzr8c9",
  14. "SystemDiskCategory": "cloud_ssd",
  15. "UserData": {"Fn::Replace": [{"print": "echo"},
  16. {"Fn::Join": ["\\n", [
  17. "#!/bin/sh",
  18. "mkdir ~/test_ros",
  19. "print hello > ~/1.txt"
  20. ]]}]}
  21. }
  22. }
  23. },
  24. "Outputs": {
  25. "InstanceId": {
  26. "Value" : {"Fn::GetAtt": ["WebServer","InstanceId"]}
  27. },
  28. "PublicIp": {
  29. "Value" : {"Fn::GetAtt": ["WebServer","PublicIp"]}
  30. }
  31. }
  32. }

Supported functions

  • Fn::Base64

  • Fn::GetAtt

  • Fn::Join

  • Fn::Select

  • Ref

Fn::Split

The built-in function Fn::Split splits strings by the specified separator, and returns the list consisting of all splits.

Statement

  1. "Fn::Split" : [ "delim", "original_string" ]

Parameters

delim: separators, such as comas (,), semicolons (;), \n , and \t.

original_string: the string to be split.

Return values

The list consisting of all split strings.

Example

If a metadata list is an array, edit as the following:

  1. {"Fn::Split": [";", "foo; bar; achoo"]}

The returned value is ["foo", " bar", "achoo "].

In the following template, split InstanceIds using Fn::Split.

  1. "Parameters" : {
  2. "InstanceIds": {
  3. "Type": "String",
  4. "Default": "instane1_id,instance2_id,instance2_id"
  5. }
  6. },
  7. "Resources": {
  8. "resourceID": {
  9. "Type": "ALIYUN::SLB::BackendServerAttachment":
  10. "Properties": {
  11. "BackendServerList": { "Fn::Split" : [ ",", {"Ref": "InstanceIds"} ] }
  12. }
  13. }
  14. }

Supported functions

  • Fn::Base64

  • Fn::FindInMap

  • Fn::GetAtt

  • Fn::Join

  • Fn::Select

  • Fn::Replace

  • Fn::GetAZs

  • Fn::If

  • Ref

Fn::Equals

Fn::Equals compares two values. If the two values are equal, returns true. If the two values are unequal, returns false.

Statement

  1. {"Fn::Equals": ["value_1", "value_2"]}

Parameters

value: the vales in any type to be compared.

Return values

true or false.

Example

Use Fn::Equals to define a condition in Conditions:

  1. {
  2. "ROSTemplateFormatVersion" : "2015-09-01",
  3. "Parameters":{
  4. "EnvType":{
  5. "Default":"pre",
  6. "Type":"String"
  7. }
  8. },
  9. "Conditions": {
  10. "TestEqualsCond": {"Fn::Equals": ["prod", {"Ref": "EnvType"}]}
  11. }
  12. }

Supported functions

  • Fn::Or

  • Fn::Not

  • Fn::Equals

  • Fn::FindInMap

  • Fn::And

  • Ref

Fn::And

Fn::And indicates the AND operator. At least, two conditions are contained. If all the specified conditions are calculated as true, true is returned. If any condition is calculated as false, false is returned.

Statement

  1. {"Fn::And": ["condition", {...}]}

Parameters

condition: the conditions to be calculated.

Return values

true or false.

Example

Use Fn::And to define a condition in Conditions:

  1. {
  2. "ROSTemplateFormatVersion" : "2015-09-01",
  3. "Parameters":{
  4. "EnvType":{
  5. "Default":"pre",
  6. "Type":"String"
  7. }
  8. },
  9. "Conditions": {
  10. "TestEqualsCond": {"Fn::Equals": ["prod", {"Ref": "EnvType"}]},
  11. "TestAndCond": {"Fn::And": ["TestEqualsCond", {"Fn::Equals": ["pre", {"Ref": "EnvType"}]}]}
  12. }
  13. }

Supported functions

  • Fn::Or

  • Fn::Not

  • Fn::Equals

  • Fn::FindInMap

  • Fn::And

  • Ref

Fn::Or

This function indicates the OR operator. It contains at least two conditions. If any specified condition is calculated as true, true is returned. If all the conditions are calculated as false, false is returned.

Statement

  1. {"Fn::Or": ["condition", {...}]}

Parameters

condition: the conditions to be calculated.

Return values

true or false.

Example

Use Fn::Or to define a condition in Conditions:

  1. {
  2. "ROSTemplateFormatVersion" : "2015-09-01",
  3. "Parameters":{
  4. "EnvType":{
  5. "Default":"pre",
  6. "Type":"String"
  7. }
  8. },
  9. "Conditions": {
  10. "TestEqualsCond": {"Fn::Equals": ["prod", {"Ref": "EnvType"}]},
  11. "TestOrCond": {"Fn::And": ["TestEqualsCond", {"Fn::Equals": ["pre", {"Ref": "EnvType"}]}]}
  12. }
  13. }

Supported functions

  • Fn::Or

  • Fn::Not

  • Fn::Equals

  • Fn::FindInMap

  • Fn::And

  • Ref

Fn::Not

This function indicates the NOT operator. If a condition is calculated as false, true is returned. If a condition is calculated as true, false is returned.

Statement

  1. {"Fn::Not": "condition"}

Parameters

condition: the condition to be calculated.

Return values

true or false.

Example

Use Fn::Not to define a condition in Conditions:

  1. {
  2. "ROSTemplateFormatVersion" : "2015-09-01",
  3. "Parameters":{
  4. "EnvType":{
  5. "Default":"pre",
  6. "Type":"String"
  7. }
  8. },
  9. "Conditions": {
  10. "TestNotCond": {"Fn::Not": {"Fn::Equals": ["pre", {"Ref": "EnvType"}]}}
  11. }
  12. }

Supported functions

  • Fn::Or

  • Fn::Not

  • Fn::Equals

  • Fn::FindInMap

  • Fn::And

  • Ref

Fn::If

The Resources and Outputs attribute values in the template support the built-in function Fn::If. If the specified condition is calculated as true, a value is returned. If the specified condition is calculated as false, another value is returned. You can use the pseudo parameter ALIYUN::NoValueas the return value to delete the corresponding attribute.

Statement

  1. {"Fn::If": ["condition_name", "value_if_true", "value_if_false"]}

Parameters

condition_name: the name of the condition in Conditions. A condition is referenced by the condition name.

value_if_true: If the specified condition is calculated as true, this value is returned.

value_if_false: If the specified condition is calculated as false, this value is returned.

Example

The following example show how to determine whether to create a data disk based on the input parameters.

  1. {
  2. "ROSTemplateFormatVersion":"2015-09-01",
  3. "Parameters":{
  4. "EnvType":{
  5. "Default":"pre",
  6. "Type":"String"
  7. }
  8. },
  9. "Conditions":{
  10. "CreateDisk":{
  11. "Fn::Equals":[
  12. "prod",
  13. {
  14. "Ref":"EnvType"
  15. }
  16. ]
  17. }
  18. },
  19. "Resources":{
  20. "WebServer":{
  21. "Type":"ALIYUN::ECS::Instance",
  22. "Properties":{
  23. "DiskMappings":{
  24. "Fn::If":[
  25. "CreateDisk",
  26. [
  27. {
  28. "Category":"cloud_efficiency",
  29. "DiskName":"FirstDataDiskName",
  30. "Size":40
  31. },
  32. {
  33. "Category":"cloud_ssd",
  34. "DiskName":"SecondDataDiskName",
  35. "Size":40
  36. }
  37. ],
  38. {
  39. "Ref":"ALIYUN::NoValue"
  40. }
  41. ]
  42. },
  43. "VpcId":"vpc-2zew9pxh2yirtzqxdboi1",
  44. "SystemDiskCategory":"cloud_efficiency",
  45. "SecurityGroupId":"sg-2zece6wcqriejf1v91sr",
  46. "SystemDiskSize":40,
  47. "ImageId":"centos_6_8_64_40G_base_20170222.vhd",
  48. "IoOptimized":"optimized",
  49. "VSwitchId":"vsw-2zed9txvy7h2srqo6jmgq",
  50. "InstanceType":"ecs.n1.medium"
  51. }
  52. }
  53. },
  54. "Outputs":{
  55. "InstanceId":{
  56. "Value":{
  57. "Fn::GetAtt":[
  58. "WebServer",
  59. "InstanceId"
  60. ]
  61. }
  62. },
  63. "ZoneId":{
  64. "Value":{
  65. "Fn::GetAtt":[
  66. "WebServer",
  67. "ZoneId"
  68. ]
  69. }
  70. }
  71. }
  72. }

Supported functions

  • Fn::Or

  • Fn::Not

  • Fn::Equals

  • Fn::FindInMap

  • Fn::And

  • Ref

Fn::ListMerge

This function merges multiple lists to one.

Statement

  1. {"Fn::ListMerge": [[list_1_item_1, list_1_imte_2, ...], [list_2_item_1, list_2_imte_2, ...]]}

Parameters

[list_1_item_1, list_1_imte_2, ...]: the first list to be merged.

[list_2_item_1, list_2_imte_2, ...]: a list to be merged to the first list.

Example

The following example shows how to attach two ECS instance groups to the same Server Load Balancer instance.

  1. {
  2. "ROSTemplateFormatVersion" : "2015-09-01",
  3. "Resources" : {
  4. "LoadBalancer": {
  5. "Type": "ALIYUN::SLB::LoadBalancer",
  6. "Properties": {
  7. "LoadBalancerName": "ros",
  8. "AddressType": "internet",
  9. "InternetChargeType": "paybybandwidth",
  10. }
  11. },
  12. "BackendServer1": {
  13. "Type": "ALIYUN::ECS::InstanceGroup",
  14. "Properties": {
  15. "ImageId" : "m-2ze9uqi7wo61hwep5q52",
  16. "InstanceType": "ecs.t1.small",
  17. "SecurityGroupId": "sg-2ze8yxgempcdsq3iucsi",
  18. "MaxAmount": 1,
  19. "MinAmount": 1
  20. }
  21. },
  22. "BackendServer2": {
  23. "Type": "ALIYUN::ECS::InstanceGroup",
  24. "Properties": {
  25. "ImageId" : "m-2ze9uqi7wo61hwep5q52",
  26. "InstanceType": "ecs.t1.small",
  27. "SecurityGroupId": "sg-2ze8yxgempcdsq3iucsi",
  28. "MaxAmount": 1,
  29. "MinAmount": 1
  30. }
  31. },
  32. "Attachment": {
  33. "Type": "ALIYUN::SLB::BackendServerAttachment",
  34. "Properties": {
  35. "LoadBalancerId": {"Ref": "LoadBalancer"},
  36. "BackendServerList": { "Fn::ListMerge": [
  37. {"Fn::GetAtt": ["BackendServer1", "InstanceIds"]},
  38. {"Fn::GetAtt": ["BackendServer2", "InstanceIds"]}
  39. ]
  40. }
  41. }
  42. }
  43. }
  44. }

Supported functions

  • Fn::Base64

  • Fn::GetAtt

  • Fn::Join

  • Fn::Select

  • Ref

  • Fn::Join

  • Fn::If

Fn::GetJsonValue

This function resolves a JSON string and obtains the value of the specified Key at the first layer.

Statement

  1. {"Fn::GetJsonValue": ["key", "json_string"]}

Parameters

key: the key value.

json_string: the JSON string to be resolved.

Example

The following example shows that after the WebServer instance executes UserData and returns a JSON string, the WebServer2 instance obtains the corresponding value from the string.

  1. {
  2. "ROSTemplateFormatVersion" : "2015-09-01",
  3. "Resources" : {
  4. "WebServer": {
  5. "Type": "ALIYUN::ECS::Instance",
  6. "Properties": {
  7. "ImageId" : "m-2ze45uwova5fedlufpz7",
  8. "InstanceType": "ecs.n1.medium",
  9. "SecurityGroupId": "sg-2ze7pxymaix640qrg3vu",
  10. "Password": "Wenqiao1234",
  11. "IoOptimized": "optimized",
  12. "VSwitchId": "vsw-2zei67xd9nhcqxzec7qt7",
  13. "VpcId": "vpc-2zevx9ios1rszqv0azijb",
  14. "SystemDiskCategory": "cloud_ssd",
  15. "UserData": {"Fn::Join": ["", [
  16. "#!/bin/sh\n",
  17. "mkdir ~/test_ros\n",
  18. "print hello > ~/1.txt\n",
  19. "Fn::GetAtt": ["WaitConHandle", "CurlCli"],
  20. "\n",
  21. "Fn::GetAtt": ["WaitConHandle", "CurlCli"],
  22. " -d '{\\"id\\" : \\"1\\", \\"data\\": [\\"1111\\", \\"2222\\"]}'\n"
  23. ]]},
  24. "PrivateIpAddress": "192.168.2.110",
  25. "HostName": "userdata-1"
  26. }
  27. },
  28. "WaitConHandle": {
  29. "Type": "ALIYUN::ROS::WaitConditionHandle"
  30. },
  31. "WaitCondition": {
  32. "Type": "ALIYUN::ROS::WaitCondition",
  33. "Properties": {
  34. "Handle": {"Ref": "WaitConHandle"},
  35. "Timeout": 900
  36. }
  37. },
  38. "WebServer2": {
  39. "Type": "ALIYUN::ECS::Instance",
  40. "Properties": {
  41. "ImageId" : "m-2ze45uwova5fedlufpz7",
  42. "InstanceType": "ecs.n1.medium",
  43. "SecurityGroupId": "sg-2ze7pxymaix640qrg3vu",
  44. "Password": "Wenqiao1234",
  45. "IoOptimized": "optimized",
  46. "VSwitchId": "vsw-2zei67xd9nhcqxzec7qt7",
  47. "VpcId": "vpc-2zevx9ios1rszqv0azijb",
  48. "SystemDiskCategory": "cloud_ssd",
  49. "UserData":
  50. {"Fn::Join": ["", [
  51. "#!/bin/sh\n",
  52. "mkdir ~/test_ros\n",
  53. "echo hello > ~/1.txt\n",
  54. "server_1_token=",
  55. {"Fn::GetJsonValue": ["1", { "Fn::GetAtt": ["WaitCondition", "Data"]}]},
  56. "\n"
  57. ]]},
  58. "PrivateIpAddress": "192.168.2.111",
  59. "HostName": "userdata-2"
  60. }
  61. },
  62. },
  63. "Outputs": {
  64. "InstanceId": {
  65. "Value" : {"Fn::GetAtt": ["WebServer","InstanceId"]}
  66. },
  67. "PublicIp": {
  68. "Value" : {"Fn::GetAtt": ["WebServer","PublicIp"]}
  69. }
  70. }
  71. }

Supported functions

  • Fn::Base64

  • Fn::GetAtt

  • Fn::Join

  • Fn::Select

  • Ref

  • Fn::Join

  • Fn::If

Fn::MergeMapToList

This function merge multiple Mappings to a list.

Statement

  1. {"Fn::MergeMapToList": [{"key_1": ["key_1_item_1", "key_1_item_2", ...]}, {"key_2":["key_2_item_1", "key_2_item_2", ...]}, ... ]}

Parameters

{"key_1": ["key_1_item_1", "key_1_item_2", ...]}: In the first Mapping to be merged, the "key_1" value must be a list. "key_1" is a key of each Mapping in the list of merged Mappings. The "key_1" value is "key_1_item_1" in the first Mapping, "key_1_item_2" in the second Mapping, and the like. The length of the final list of merged Mappings is the length of the longest list "key_x" in all Mappings to be merged. If a "key_y" list is shorter, the last element of the list is repeated, to make the list the longest.

{"key_2": ["key_2_item_1", "key_2_item_2", ...]}: In the second Mapping to be merged, the "key_2" value must be a list. "key_2" is a key of each Mapping in the merged list. The "key_2" value is "key_2_item_1" in the first Mapping, "key_2_item_2" in the second Mapping, and the like.

Examples

The following example shows how to merge three Mappings. The length of the list corresponding to the key value in each Mapping is the same.

  1. {
  2. "Fn::MeregMapToList": [
  3. {"key_1": ["kye_1_item_1", "kye_1_item_2"]},
  4. {"key_2": ["kye_2_item_1", "kye_2_item_2"]},
  5. {"key_3": ["kye_3_item_1", "kye_3_item_2"]}
  6. ]
  7. }

The result is:

  1. [
  2. {
  3. "key_1": "kye_1_item_1",
  4. "key_2": "kye_2_item_1",
  5. "key_3": "kye_3_item_1"
  6. },
  7. {
  8. "key_1": "kye_1_item_2",
  9. "key_2": "kye_2_item_2",
  10. "key_3": "kye_3_item_2"
  11. }
  12. ]

In the following example, the length of the list corresponding to the key value in each Mapping varies.

  1. {
  2. "Fn::MeregMapToList": [
  3. {"key_1": ["kye_1_item_1", "kye_1_item_2"]},
  4. {"key_2": ["kye_2_item_1", "kye_2_item_2", "key_2_item_3"]},
  5. {"key_3": ["kye_3_item_1", "kye_3_item_2"]}
  6. ]
  7. }

The result is:

  1. [
  2. {
  3. "key_1": "kye_1_item_1",
  4. "key_2": "kye_2_item_1",
  5. "key_3": "kye_3_item_1"
  6. },
  7. {
  8. "key_1": "kye_1_item_2",
  9. "key_2": "kye_2_item_2",
  10. "key_3": "kye_3_item_2"
  11. },
  12. {
  13. "key_1": "kye_1_item_2",
  14. "key_2": "kye_2_item_3",
  15. "key_3": "kye_3_item_2"
  16. }
  17. ]

The following example shows that add all instances created in the WebServer into the Virtual Server Group of one Server Load Balancer.

  1. {
  2. "ROSTemplateFormatVersion": "2015-09-01",
  3. "Resources": {
  4. "WebServer": {
  5. "Type": "ALIYUN::ECS::InstanceGroupClone",
  6. "Properties": {
  7. "SourceInstanceId": "i-xxxxx",
  8. "Password": "Hello1234",
  9. "MinAmount": 1,
  10. "MaxAmount": 1
  11. }
  12. },
  13. "CreateVServerGroup": {
  14. "Type": "ALIYUN::SLB::VServerGroup",
  15. "Properties": {
  16. "LoadBalancerId": "lb-yyyy",
  17. "VServerGroupName": "VServerGroup-test",
  18. "BackendServers": {
  19. "Fn::MergeMapToList": [
  20. {"Port": [6666, 9090, 8080]},
  21. {"ServerId": {"Fn::GetAtt": ["WebServer", "InstanceIds"]}},
  22. {"Weight": [20, 100]}
  23. ]
  24. }
  25. }
  26. }
  27. }
  28. }

Supported functions

  • Fn::Base64

  • Fn::GetAtt

  • Fn::Join

  • Fn::Select

  • Ref

  • Fn::Join

  • Fn::If

  • Fn::ListMerge

  • Fn::GetJsonValue

Thank you! We've received your feedback.