raj.andy1
raj.andy1

Reputation: 121

AWS Cloudformation - use AWS::NoValue within Fn::Sub

I am using cloudformation and I want to be able to use the pseudo value AWS::NoValue within the Fn::Sub like this:

!Sub ["ATL_DATASET_URL=${DatasetURL}", 
       DatasetURL: !If [IsURLProvided, 
                        !Ref BitbucketDatasetURL, 
                        !Ref "AWS::NoValue"]]

My Template passes validation but does not deploy. Here is the error message I get when I click Create Stack.

Template error: every value of the context object of every Fn::Sub object must be a string or a function that returns a string

Upvotes: 4

Views: 3351

Answers (2)

Marcin
Marcin

Reputation: 238487

The alternative to @franklinsijo is to swap the If and Sub statements if you want to actually remove the property (e.g. YourPropertyName) if BitbucketDatasetURL is not given.

  YourPropertyName: !If
      - IsURLProvided 
      - !Sub ["ATL_DATASET_URL=${DatasetURL}", DatasetURL: !Ref BitbucketDatasetURL]
      - !Ref "AWS::NoValue"    

Or shorter

  YourPropertyName: !If
      - IsURLProvided 
      - !Sub "ATL_DATASET_URL=${BitbucketDatasetURL}"
      - !Ref "AWS::NoValue"

Upvotes: 2

franklinsijo
franklinsijo

Reputation: 18270

If you want to skip setting a value for DatasetURL, make the !If to return an empty string '' when the condition evaluates to false instead of AWS::NoValue.

Returning AWS::NoValue when false, removes the mapping for DatasetURL.

Upvotes: 2

Related Questions