|
| 1 | +# RFC for Fn::Size |
| 2 | + |
| 3 | +* **Original Author(s):**: @juegong2 |
| 4 | +* **Tracking Issue**: https://github.com/aws-cloudformation/cfn-language-discussion/issues/70 |
| 5 | + |
| 6 | +# Summary |
| 7 | + |
| 8 | +We will support an intrinsic function called `Fn::Size` that will return the number of elements in a given list. |
| 9 | + |
| 10 | +# Motivation |
| 11 | + |
| 12 | +CloudFormation users may want to get the number of elements in a given list to set certain resource parameters or generate conditions. It may also help to check if index is out of bound for !Select function. This RFC proposes to support the use case of getting the number of elements of a given list. |
| 13 | + |
| 14 | +* Reference: |
| 15 | + * https://github.com/aws-cloudformation/cfn-language-discussion/issues/61 |
| 16 | + |
| 17 | +# Example(s) |
| 18 | + |
| 19 | +Use `Ref` on a Parameter of type `CommaDelimitedList` or `List<Number>` |
| 20 | + |
| 21 | +```json |
| 22 | +{ |
| 23 | + "Parameters": { |
| 24 | + "InstanceTypes": { |
| 25 | + "Type": "CommaDelimitedList" |
| 26 | + }, |
| 27 | + "NumberList": { |
| 28 | + "Type": "List<Number>" |
| 29 | + } |
| 30 | + } |
| 31 | + "Conditions": { |
| 32 | + "Has3InstanceTypes": { |
| 33 | + "Fn::Equals": [ |
| 34 | + { |
| 35 | + "Fn::Size": {"Ref": "InstanceTypes"} |
| 36 | + }, |
| 37 | + 3 |
| 38 | + ] |
| 39 | + }, |
| 40 | + "Has2NumbersInList": { |
| 41 | + "Fn::Equals": [ |
| 42 | + { |
| 43 | + "Fn::Size": {"Ref": "NumberList"} |
| 44 | + }, |
| 45 | + 2 |
| 46 | + ] |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Use a hardcoded list |
| 53 | +```json |
| 54 | +{ |
| 55 | + "Conditions": { |
| 56 | + "Has3InstanceTypes": { |
| 57 | + "Fn::Equals": [ |
| 58 | + { |
| 59 | + "Fn::Size": [ "m5.large", "m5.xlarge" ] |
| 60 | + }, |
| 61 | + 3 |
| 62 | + ] |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +# Limitation |
| 69 | + |
| 70 | +`Fn::Size` will not work with lists that are dynamically generated at provisinoing time. For example, assuming MyBucket is the logical id of a AWS::S3::Bucket resource, the following is not supported: |
| 71 | +```json |
| 72 | +{ |
| 73 | + "Fn::Size": { "Fn::Split": [":", { "Fn::GetAtt": [ "MyBucket", "Arn" ] } ] } |
| 74 | +} |
| 75 | +``` |
| 76 | +because the ARN of MyBucket is not known until during provisioning. |
| 77 | + |
| 78 | +**Note:** This is a short-term limitation due to underlying implementation constraints. In the fullness of time, this limitation should be removed. |
| 79 | + |
| 80 | +# Details |
| 81 | + |
| 82 | +`Fn::Size` is an intrinsic function that takes in a list and returns the size of the given list. The list could be: |
| 83 | + |
| 84 | +* hardcoded list in template |
| 85 | +* array generated by intrinsic functions like `Fn::Split` with parameters of hardcoded values or of `Ref` to template parameters |
| 86 | +* `Ref` to a parameter of type `CommaDelimitedList` |
| 87 | +* `Ref` to a parameter of type `List<Number>` |
| 88 | +* `Ref` to a parameter of type `List<AWS-specific parameter types>` |
| 89 | +* `Ref` to a parameter of type |
| 90 | + * `AWS::SSM::Parameter::Value<List<String>>` |
| 91 | + * `AWS::SSM::Parameter::Value<CommaDelimitedList>` |
| 92 | + * `AWS::SSM::Parameter::Value<List<AWS-specific parameter type>>` |
| 93 | + |
| 94 | +Within `Fn::Size`, following intrinsic functions with parameters of hardcoded values or of `Ref` to template parameters will be supported: |
| 95 | +* Condition functions |
| 96 | +* Fn::FindInMap |
| 97 | +* Fn::Base64 |
| 98 | +* Fn::Join |
| 99 | +* Fn::Select |
| 100 | +* Fn::Split |
| 101 | +* Fn::Sub |
| 102 | + |
| 103 | +# FAQ |
| 104 | +1. **Will the CloudFormation Linter (cfn-lint) support validations regarding Fn::Size?** |
| 105 | + |
| 106 | + Yes. cfn-lint will be updated to validate if the parameter of Fn::Size is a list. It will also validate if it is used for integer-type resource properties or function parameters. |
0 commit comments