简体   繁体   English

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

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

I have cloudformation Template where i have one parameter passed from previous step with value like this我有 cloudformation 模板,其中我有一个从上一步传递的参数,其值是这样的

 "test1.example.org"

 "example.org"

 "org"

now i want to basically remove the .org part from that parameter and get现在我想基本上从该参数中删除 .org 部分并获取

test1.example

example

There can be many subdomain as well like也可以有很多子域,例如

test1.test2.test3.test4.example.org

I just need to remove .org from the end我只需要从最后删除 .org

With a caveats you can achieve what you want - assuming that .org will appear only on the end of your string.有一个警告,你可以实现你想要的 - 假设.org只会出现在你的字符串的末尾。

Here's a complete template (test.cfn.yaml) that shows this method working:这是一个完整的模板 (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 ] ]

You can test this out with the aws cli by running:您可以通过运行以下命令使用 aws cli 进行测试:

aws cloudformation deploy --template-file test.cfn.yaml --stack-name test1 --parameter-overrides Input=apples.bananas.org

The output of this stack will contain apples.bananas .此堆栈的输出将包含apples.bananas

Within your script you can use !Join [ '', !Split [ '.org', !Ref Input ] ] to strip down your string as you wanted, replacing Input with the value you need changed.在您的脚本中,您可以使用!Join [ '', !Split [ '.org', !Ref Input ] ]根据需要剥离字符串,将Input替换为您需要更改的值。

Note that there is a DummyBucket in the stack as you need to have at least one resource for cloudformation to deploy the script.请注意,堆栈中有一个DummyBucket ,因为您需要至少有一个 cloudformation 资源来部署脚本。

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

There is no string manipulation capability in a CloudFormation template. CloudFormation 模板中没有字符串操作功能。

Worst case, you could create a Lambda-backed Custom Resource that can transform parameters.最坏的情况是,您可以创建一个可以转换参数的Lambda 支持的自定义资源

I don't think you can use any of the existing Cloudformation method s for this purpose.我认为您不能为此目的使用任何现有的Cloudformation 方法 You can leverage the cloudformation template sample provided by AWS.您可以利用 AWS 提供的 cloudformation 模板示例。 Here is the example template String manipulation which provides string transformation utility functions.这是示例模板字符串操作,它提供字符串转换实用程序功能。 You can easily extend the python method to whatever operation you want.您可以轻松地将python 方法扩展到您想要的任何操作。

CLI Command to create the stacks (after downloading them locally)用于创建堆栈的 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

It creates a s3 bucket and adds various transformed tags.它创建了一个 s3 存储桶并添加了各种转换后的标签。 Command for vaidation.验证命令。

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.

相关问题 如何在 AWS Lambda 中加载 AWS Cloudformation 的 Output? - How Can I Load The Output of AWS Cloudformation In AWS Lambda? 如何在 CloudWatch 中查看 AWS CloudFormation 日志? - How can I see AWS CloudFormation logs in CloudWatch? 如果已经发生更新,我该如何重试AWS CloudFormation更新? - How can I retry a AWS CloudFormation update if an update is already happening? AWS CloudFormation:如何使用参数来形成字符串? - AWS CloudFormation: how can I use parameters to form strings? 如何使用 AWS CloudFormation 连接到现有 RDS 数据库? - How can I connect to an existing RDS database using AWS CloudFormation? 我如何在Ansible中触发多个AWS Cloudformation任务? - How can i trigger multiple aws cloudformation tasks in ansible? aws cloudformation 如何在映射块中使用条件? - aws cloudformation how can I use conditions in the mapping block? 如何在 cloudformation 中为“AWS::RDS::DBInstance”提供变量? - How can I provide a variable for `AWS::RDS::DBInstance` in cloudformation? 如何在 cloudformation lambda 中为 aws lambda 设置最大重试次数? - How can i set the maximumRetryAttempt for aws lambda in the cloudformation lambda? 如何为特定 CloudFormation 堆栈创建“aws.cloudformation”CloudWatch 事件类型? - How can I create an "aws.cloudformation" CloudWatch event type for a specific CloudFormation stack?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM