我有 CloudFormation 模板,其中有一个从上一步传递的参数,其值如下:
"test1.example.org"
"example.org"
"org"
现在我想基本上从该参数中删除 .org 部分并获取:
test1.example
example
也可以有很多子域,例如
test1.test2.test3.test4.example.org
我只需要从末尾删除 .org
最佳答案
有一个警告,您可以实现您想要的 - 假设 .org
仅出现在字符串的末尾。
这是一个完整的模板test.cfn.yaml
,显示了此方法的工作原理:
AWSTemplateFormatVersion: 2010-09-09
Parameters:
Input:
Type: String
Resources:
DummyBucket:
Type: AWS::S3::Bucket
Outputs:
TheOutput:
Value: !Join [ '', !Split [ '.org', !Ref Input ] ]
您可以通过运行以下命令使用 AWS CLI 对此进行测试:
aws cloudformation deploy --template-file test.cfn.yaml --stack-name test1 --parameter-overrides Input=apples.bananas.org
该堆栈的输出将包含 apples.bananas
。
在脚本中,您可以使用 !Join [ '', !Split [ '.org', !Ref Input ] ]
来根据需要删除字符串,替换 Input 更改您需要的值。
请注意,堆栈中有一个 DummyBucket
,因为您需要至少一个 CloudFormation 资源来部署脚本。
关于amazon-web-services - 如何替换 AWS CloudFormation 中的某些字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56881879/