繁体   English   中英

我如何替换 cloudformation aws 中的一些字符串

[英]How can i replace some string in cloudformation aws

我有 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 资源来部署脚本。

如果我正确理解您的查询,您可以使用的一种方法是您可以使用 Fn::Split 函数按冒号拆分字符串并使用您想要使用的数组元素。

CloudFormation 模板中没有字符串操作功能。

最坏的情况是,您可以创建一个可以转换参数的Lambda 支持的自定义资源

我认为您不能为此目的使用任何现有的Cloudformation 方法 您可以利用 AWS 提供的 cloudformation 模板示例。 这是示例模板字符串操作,它提供字符串转换实用程序功能。 您可以轻松地将python 方法扩展到您想要的任何操作。

用于创建堆栈的 CLI 命令(在本地下载后)

aws cloudformation create-stack --stack-name testString --template-body file://string.yaml  --profile your_profile --capabilities CAPABILITY_IAM
arn:aws:cloudformation:us-east-1:1234:stack/testString/ec34d8c0-9fc9-11e9-a0ed-0aa1af63e98c

aws cloudformation create-stack --stack-name testStringExample --template-body file://string_example.yaml  --profile your_profile --capabilities CAPABILITY_AUTO_EXPAND
arn:aws:cloudformation:us-east-1:1234:stack/testStringExample/2047d720-9fca-11e9-ab63-12989ba5c57e

它创建了一个 s3 存储桶并添加了各种转换后的标签。 验证命令。

aws s3api get-bucket-tagging --bucket teststringexample-s3bucket-1dgnx05oslymu --profile your_profile --output json

{
    "TagSet": [
        {
            "Value": "ring",
            "Key": "ShortenLeft"
        },
        {
            "Value": "his is a test input strin",
            "Key": "Strip"
        },
        {
            "Value": "THIS IS A TEST INPUT STRING",
            "Key": "Upper"
        },
        {
            "Value": "This_is_a_test_input_string",
            "Key": "Replace"
        },
        {
            "Value": "testStringExample",
            "Key": "aws:cloudformation:stack-name"
        },
        {
            "Value": "this is a test input string",
            "Key": "Lower"
        },
        {
            "Value": "This is a test input string",
            "Key": "Capitalize"
        },
        {
            "Value": "This Is A Test Input String",
            "Key": "Title"
        },
        {
            "Value": "S3Bucket",
            "Key": "aws:cloudformation:logical-id"
        },
        {
            "Value": "This",
            "Key": "ShortenRight"
        }
    ]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM