amazon-web-services - 如何替换 AWS CloudFormation 中的某些字符串

标签 amazon-web-services aws-cloudformation

我有 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/

相关文章:

bash - 如何在CloudFormation中传入UserData作为参数

amazon-web-services - 是否可以拆分字符串并仅取出 Cloudformation Template 中的单个字符?

amazon-web-services - 标签事件规则 AWS - Cloudformation

amazon-web-services - AWS Cloudwatch 心跳警报

amazon-web-services - AWS Interface VPC 终端节点如何将流量实际路由到区域服务?

git - 管理 key

aws-cloudformation - 使用jq替换云形成参数值

amazon-web-services - 在 cloudformation 中部署我的堆栈后如何安装软件

amazon-web-services - AWS RDS 备份到 S3 失败

amazon-web-services - 由于配置错误,执行失败:API Gateway 无权承担提供的角色 arn:aws:iam::XXXXXXXXXXXX:role/auth