alphanumeric
alphanumeric

Reputation: 19339

how to replace character in a string variable using CloudFormation

In my serverless.yml script which uses CloudFormation syntax I have defined a name for my app using the appName variable:

custom:
  appName: my-app-name

Somewhere down the script I use this appName variable to assemble the url path string, such as:

  myEndpoint:
    events:
      - http:
          path: /${{self:custom.appName}}/index

which will result to a path being /my-app-name/index

But I would like to replace the - characters with _ dashes.

What would be the cleanest way of doing it?

Upvotes: 3

Views: 2217

Answers (2)

Michel Larouche
Michel Larouche

Reputation: 31

You can use split and join functions. First, Fn::split your string using the original character, which is "-" in your case, and then Fn::Join back all values from the list returned by Fn::Split with the desired character "_".

{"Fn::Join": ["_", { "Fn::Split" : ["-", {"Ref": "myVar"}]}]}

Upvotes: 3

Marcin
Marcin

Reputation: 238339

You can't do this with plain CloudFormation. You have to use macros, though a custom resource could also be used. Specifically, you can use Replace macro developed my AWS and available at aws-cloudformation / aws-cloudformation-macros. You can also develop your own macros, or modified the one given by AWS as it is open sourced.

Upvotes: 2

Related Questions