The built-in function Fn::Replace replaces a substring in a string with another substring.
Declaration
JSON
{ "Fn::Replace": [ { "object_key1": "object_value1", "object_key2": "object_value2" }, "object_string" ] }
YAML
Syntax for the full function name:
Fn::Replace: - object_oldValue1: object_newValue1 object_oldValue2: object_newValue2 - object_string
Syntax for the short form:
!Replace [{'object_oldValue1': 'object_newValue1', 'object_oldValue2': 'object_newValue2'}, object_string]
Parameters
object_oldValue
: the existing substring that you want to replace.object_newValue
: the new substring that you want to use to replace the existing substring.object_string
: the string that contains the existing substring specified byobject_oldValue
.
Return value
The string whose substring is replaced.
Examples
The Fn::Replace function is commonly used to modify UserData and CommandContent scripts. In the following examples, "print" in a script is replaced with "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"
]
]
}
]
}
}
}
}
}