rgd
rgd

Reputation: 406

How can I replace some string in AWS CloudFormation

I have CloudFormation template where i have one parameter passed from previous step with value like this:

 "test1.example.org"

 "example.org"

 "org"

Now I want to basically remove the .org part from that parameter and get:

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

Upvotes: 13

Views: 19202

Answers (4)

geoff.weatherall
geoff.weatherall

Reputation: 657

With a caveats you can achieve what you want - assuming that .org will appear only on the end of your string.

Here's a complete template test.cfn.yaml that shows this method working:

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 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.

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.

Note that there is a DummyBucket in the stack as you need to have at least one resource for CloudFormation to deploy the script.

Upvotes: 27

krunal shimpi
krunal shimpi

Reputation: 1

If I am understanding your query correctly, one way you could do it is you could use Fn::Split function to split the string by colon and use the array element that you want to use.

Upvotes: 1

Ankit Gupta
Ankit Gupta

Reputation: 760

I don't think you can use any of the existing Cloudformation methods for this purpose. You can leverage the cloudformation template sample provided by AWS. 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.

CLI Command to create the stacks (after downloading them locally)

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. 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"
        }
    ]
}

Upvotes: 0

John Rotenstein
John Rotenstein

Reputation: 269550

There is no string manipulation capability in a CloudFormation template.

Worst case, you could create a Lambda-backed Custom Resource that can transform parameters.

Upvotes: 0

Related Questions