generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 13
Add RFC for Fn::NumberComparison #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mingxingong
wants to merge
1
commit into
aws-cloudformation:main
Choose a base branch
from
mingxingong:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| # Fn::NumberComparison | ||
|
|
||
| * **Original Author(s)**:: @minggon | ||
| * **Tracking Issue**: [Tracking Issue](https://github.com/aws-cloudformation/cfn-language-discussion/issues/80) | ||
|
|
||
| # Summary | ||
|
|
||
| We will create a numerical comparison function `Fn::NumberComparison` to support comparisons such as `“<”`, `“<=”`, `“==”`, `“>=”`, and `“>”` between numbers. The function would take in 3 parameters, the first number being compared, the comparison operator as a string, and the second number being compared, and output the boolean result of the comparison between the first and third parameter. | ||
|
|
||
| # Motivation | ||
|
|
||
| CloudFormation users may want to do different things depending on the condition of a numeric variable. This RFC proposes to support the use case of comparing two numbers to create a boolean that can be used in the Fn::If intrinsic function. | ||
|
|
||
| * Reference: | ||
| * https://github.com/aws-cloudformation/cfn-language-discussion/issues/80 | ||
|
|
||
| # Examples | ||
|
|
||
| ## Example 1 | ||
|
|
||
| Using `Fn::NumberComparison` in Conditions section to set resource properties | ||
|
|
||
| ## YAML | ||
| ```yaml | ||
| Parameters: | ||
| stage: | ||
| Description: Deployment stage | ||
| Type: String | ||
| Default: gamma | ||
| AllowedValues: | ||
| - gamma | ||
| - prod | ||
| retentionInDays: | ||
| Description: Log retention in days | ||
| Type: Number | ||
| Conditions: | ||
| IsProd: !Equals | ||
| - !Ref 'stage' | ||
| - prod | ||
| NotMeetRetentionRequirement: !NumberComparison | ||
| - !Ref 'retentionInDays' | ||
| - <= | ||
| - 365 | ||
| ShouldEnforceLogRetention: !And | ||
| - !Condition 'IsProd' | ||
| - !Condition 'NotMeetRetentionRequirement' | ||
| Resources: | ||
| cwLogGroup: | ||
| Type: AWS::Logs::LogGroup | ||
| Properties: | ||
| LogGroupName: cwLogGroup | ||
| RetentionInDays: !If | ||
| - ShouldEnforceLogRetention | ||
| - 365 | ||
| - !Ref 'retentionInDays' | ||
| ``` | ||
|
|
||
| ## JSON | ||
| ```json | ||
| { | ||
| "Parameters": { | ||
| "stage": { | ||
| "Description": "Deployment stage", | ||
| "Type": "String", | ||
| "Default": "gamma", | ||
| "AllowedValues": [ | ||
| "gamma", | ||
| "prod" | ||
| ] | ||
| }, | ||
| "retentionInDays": { | ||
| "Description": "Log retention in days", | ||
| "Type": "Number" | ||
| } | ||
| }, | ||
| "Conditions": { | ||
| "IsProd": { | ||
| "Fn::Equals": [ | ||
| { "Ref": "stage"}, | ||
| "prod" | ||
| ] | ||
| }, | ||
| "NotMeetRetentionRequirement": { | ||
| "Fn::NumberComparison": [ | ||
| {"Ref": "retentionInDays"}, | ||
| "<=", | ||
| 365 | ||
| ] | ||
| }, | ||
| "ShouldEnforceLogRetention": { | ||
| "Fn::And": [ | ||
| {"Condition": "IsProd"}, | ||
| {"Condition": "NotMeetRetentionRequirement"} | ||
| ] | ||
| } | ||
| }, | ||
| "Resources": { | ||
| "cwLogGroup": { | ||
| "Type" : "AWS::Logs::LogGroup", | ||
| "Properties" : { | ||
| "LogGroupName" : "cwLogGroup", | ||
| "RetentionInDays" : { | ||
| "Fn::If": [ | ||
| "ShouldEnforceLogRetention", | ||
| 365, | ||
| {"Ref": "retentionInDays"} | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| # Limitation | ||
|
|
||
| The intrinsic function does not support reference to resource properties. It only supports references to Parameters and Mappings. | ||
|
|
||
| # Details | ||
|
|
||
| `Fn::NumberComparison` is an intrinsic function that takes a `Number`, a `String` with a comparison operator, and another `Number` and returns a `Boolean` representing the comparison between the first number and the second number. The number could be: | ||
|
|
||
| * a hardcoded `Number` in the template | ||
| * `Ref` to a Parameter of type `Number` | ||
| * a `Number` outputted by the use of a Function | ||
|
|
||
| The string could be: | ||
|
|
||
| * `"<"` | ||
| * `"<="` | ||
| * `"=="` | ||
| * `">"` | ||
| * `">="` | ||
|
|
||
| Supported Functions: | ||
|
|
||
| * `Fn::Select` | ||
| * `Fn::Length` | ||
| * `Fn::Sub` | ||
| * `Fn::Join` | ||
| * `Fn::FindInMap` | ||
| * `Fn::If` | ||
|
|
||
| As this intrinsic function produces a `Boolean`, it can be used as the conditional argument in the `Fn::If` intrinsic function. `Fn::NumberComparison` can also be used outside the Conditions section to specify `Boolean` resource properties. | ||
|
|
||
| # Alternatives considered | ||
|
|
||
| We considered creating several number comparison functions, i.e. `Fn::LessThan`, `Fn::GreaterThan`, etc. We settled on this approach as it improves readability, simplifies the development process, and allows users to set the comparator using a function. | ||
|
|
||
| # Appendix | ||
|
|
||
| * https://github.com/aws-cloudformation/cfn-language-discussion/issues/80 | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of the Functions below (
Fn::Sub,Fn::Join) only output strings, so with the current wording they shouldn't be supported