cloudformation-coverage-roadmap icon indicating copy to clipboard operation
cloudformation-coverage-roadmap copied to clipboard

Missing instrinsic function for prefix matching string function (Fn::StartsWith?)

Open autumnjolitz opened this issue 3 years ago • 2 comments

Name of the resource

Other

Resource name

AWS::CloudFormation

Description

Emulating a string prefix checking function looks like this:

Condition:
    ItemStartsWith: !Equals [ !Select [ 0, !Split [ "prefix", !Ref ... ]], "" ]

like a real world use case:

Conditions:
  # Starts-with macro for checking if the cluster has a custom redis host protocol
  # (CacheRedisUrl may be `new:elasticache` to specify making a new cluster
  HasCustomRedisUrl: !And [ 
    !Not [ !Equals [ !Ref CacheRedisUrl, "" ]],
    !Equals [ !Select [ 0, !Split [ "redis://", !Ref CacheRedisUrl ]], "" ]
  ]
  CreateElasticCache: !Equals [!Ref CacheRedisUrl, 'new:elasticache']
  OverrideRedisClusterEnvironment: !Or [ !Condition HasCustomRedisUrl, !Condition HasCustomRedisUrl]

However, Fn::Split and Fn::Select cannot be used inside any Assertions. This means that it is not possible to make rules that require a stack have a specific prefix for compliance. It is barely possible to do Fn::If statements with the above Conditions.

Other Details

An alternative approach would be to allow Fn::Split and Fn::Select on Assertions inside Rules. Or allow passing Condition: items to Rules:

autumnjolitz avatar Aug 04 '22 00:08 autumnjolitz

I'm sure I could find uses for Fn::StartsWith, but regex support might be a more robust solution, in that it would cover not only this use case but any number of others.

I have not checked whether an issue has already been opened suggesting that.

rhbecker avatar Aug 04 '22 04:08 rhbecker

I agree that a regex solution would also be helpful. I do understand the reluctance to add multiple functions, even if it does make exceedingly common string operations a little easier.

in either case, a startswith or a regex would be exceptionally helpful, especially in the Assertion Rules and Conditions.

autumnjolitz avatar Aug 04 '22 14:08 autumnjolitz