Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

 "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

like image 324
rgd Avatar asked Jul 04 '19 06:07

rgd


2 Answers

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.

like image 154
geoff.weatherall Avatar answered Nov 18 '22 15:11

geoff.weatherall


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.

like image 34
krunal shimpi Avatar answered Nov 18 '22 14:11

krunal shimpi