簡體   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