All Products
Search
Document Center

Resource Orchestration Service:Fn::Replace

Last Updated:Jun 26, 2025

Fungsi bawaan Fn::Replace mengganti substring dalam sebuah string dengan substring lainnya.

Deklarasi

  • JSON

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

    • Sintaks untuk nama fungsi lengkap:

      Fn::Replace:
       - object_oldValue1: object_newValue1
         object_oldValue2: object_newValue2
       - object_string
    • Sintaks untuk bentuk pendek:

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

Parameter

  • object_oldValue: Substring yang ingin diganti.

  • object_newValue: Substring baru untuk menggantikan substring lama.

  • object_string: String yang berisi substring lama seperti ditentukan oleh object_oldValue.

Nilai kembali

String dengan substring yang telah diganti.

Contoh

Fungsi Fn::Replace sering digunakan untuk memodifikasi skrip UserData dan CommandContent. Dalam contoh berikut, "print" dalam skrip diganti dengan "echo":

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"
                ]
              ]
            }
          ]
        }
      }
    }
  }
}

Fungsi yang didukung