Search code examples
amazon-web-servicesaws-cloudformationserverless

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?


Solution

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