All Products
Search
Document Center

Resource Orchestration Service:Fn::Replace

Last Updated:Jun 17, 2026

Fn::Replace replaces all occurrences of a substring in a string with a new string.

Function declaration

  • JSON

    {
      "Fn::Replace": [
        {
          "object_key1": "object_value1",
          "object_key2": "object_value2"
        },
        "object_string"
      ]
    }
  • YAML

    • Full syntax.

      Fn::Replace:
       - object_oldValue1: object_newValue1
         object_oldValue2: object_newValue2
       - object_string
    • Shorthand syntax.

      !Replace [{'object_oldValue1': 'object_newValue1', 'object_oldValue2': 'object_newValue2'}, object_string]

Parameters

  • object_oldValue: The substring to be replaced.

  • object_newValue: The replacement string.

  • object_string: The source string in which object_oldValue is replaced.

Return value

The modified string.

Examples

Fn::Replace is commonly used to edit UserData and CommandContent scripts. The following example replaces print with echo in a script.

ROSTemplateFormatVersion: '2015-09-01'
Resources:
  WebServer:
    Type: ALIYUN::ECS::Instance
    Properties:
      ImageId: centos_7_2_64_40G_base_20170222****
      InstanceType: ecs.n1.medium
      SecurityGroupId: sg-94q49****
      Password: MytestPassword****
      IoOptimized: optimized
      VSwitchId: vsw-94vdv****
      VpcId: vpc-949uz****
      SystemDiskCategory: cloud_ssd
      UserData:
        !Replace
          - print: echo
          - !Join
              - ''
              - - |
                  #!/bin/sh
                - |
                  mkdir ~/test_ros
                - |
                  print hello > ~/1.txt
{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Resources": {
    "WebServer": {
      "Type": "ALIYUN::ECS::Instance",
      "Properties": {
        "ImageId": "centos_7_2_64_40G_base_20170222****",
        "InstanceType": "ecs.n1.medium",
        "SecurityGroupId": "sg-94q49****",
        "Password": "MytestPassword****",
        "IoOptimized": "optimized",
        "VSwitchId": "vsw-94vdv****",
        "VpcId": "vpc-949uz****",
        "SystemDiskCategory": "cloud_ssd",
        "UserData": {
          "Fn::Replace": [
            {
              "print": "echo"
            },
            {
              "Fn::Join": [
                "",
                [
                  "#!/bin/sh\n",
                  "mkdir ~/test_ros\n",
                  "print hello > ~/1.txt\n"
                ]
              ]
            }
          ]
        }
      }
    }
  }
}

Supported functions