Function

Last Updated: Jun 13, 2017

Internal function

The Resource Orchestration Service provides multiple built-in functions to help manage your stack. You can use internal functions in resources and outputs.

Fn::Base64

Internal function Fn::Base64 returns the Base64-encoded result of the entered string.

Statement

  1. "Fn::Base64" : stringToEncode

Parameters

stringToEncode

  1. String to be converted to Base64.

Return values

Original string using the Base64 representation method.

Example

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

Fn::FindInMap

Internal function Fn::FindInMap returns the value corresponding to the key in the double layer mapping stated in the Mappings part.

Statement

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

Parameters

MapName

  1. The mapping ID stated in the Mappings part, including a key and value.

TopLevelKey

  1. The first level key, whose value is a key/value pair list.

SecondLevelKey

  1. The second level key, whose value is a string or number.

Return values

The value allocated to the SecondLevelKey.

Example

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

  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. }

In the preceding example, the ImageId attribute needs to be specified when the resource named WebServer is created. The Mappings part describes the ImageId mappings differentiated according to regions. The Parameters part describes the regions that need to be specified by the user. Fn::FindInMap will find the corresponding ImageId mapping in RegionMap according to the region specified by the user. 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 the user using {“Ref”: “regionParam”}.

  • SecondLevelKey is set to the desired architecture, which is “32” in this example.

Supported functions

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

  • Fn::FindInMap

  • Ref

Fn::GetAtt

The internal function Fn::GetAtt returns the attribute value of resource in the template.

Statement

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

Parameters

resourceID

  1. ID of the target resource.

attributeName

  1. 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 internal function Fn::Join joins a group of values, which are separated using specific separators.

Statement

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

Parameters

delimiter

  1. Separator. The separator can be blank so that all the values are joined directly.

[ “string1”, “string2”, … ]

  1. List of joined values.

Return values

Joined strings.

Example

“Fn::Join” : [ “,”, [ “a”, “b”, “c” ] ]

Returned: “a,b,c”.

Supported functions

  • Fn::Base64

  • Fn::GetAtt

  • Fn::Join

  • Fn::Select

  • Ref

Fn::Select

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

Statement

The data element list can be an array:

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

The data element list can also be a mapping table:

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

Parameters

index

  1. 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, where N indicates the number of elements in the array. If the data element list is a mapping table, the index is a key in the mapping table.
  2. 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:

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

The returned value in this example: “grapes”.

If the data element list is a mapping table:

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

The returned value in this example: “grapes”.

If the data element list is a CommaDelimitedList:

  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 internal 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 will deem the specified parameter to be a parameter and will attempt to return the parameter value.

Statement

  1. "Ref" : "logicalName"

Parameters

logicalName

  1. 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

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 resource logical ID.

Fn::GetAZs

The internal function Fn::GetAZs returns the available region list of the specified Region.

Statement

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

Parameters

region

  1. region ID.

Return values

List of available regions under the specified Region.

Example

In this example, an ECS instance is created in the first available region 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

Thank you! We've received your feedback.