Cloudformation-coverage-roadmap: Way to check for index is out of bounds for !Select function

Created on 5 Jan 2021  路  6Comments  路  Source: aws-cloudformation/cloudformation-coverage-roadmap

!Select function documentation mentions following:

Fn::Select does not check for null values or if the index is out of bounds of the array. Both conditions will result in a stack error, so you should be certain that the index you choose is valid, and that the list contains non-null values.

Without the ability to check for the provided array length CloudFormation is impossible to implement some very simple solutions.

For example, I need to create an AWS::EFS::MountTarget for each user-provided subnet, but it is impossible because I can't know how many subnets user will provide.

For example:

Parameters:
  Subnets:
    Type: List<AWS::EC2::Subnet::Id>
Resources:
  EFSMountTarget1:
    Type: AWS::EFS::MountTarget
    Properties:
      FileSystemId: !Ref EFSVolume
      SecurityGroups:
        - !Ref EFSSecurityGroup
      SubnetId: !Select [0, !Ref Subnets] 
  EFSMountTarget2:
    Type: AWS::EFS::MountTarget
    Properties:
      FileSystemId: !Ref EFSVolume
      SecurityGroups:
        - !Ref EFSSecurityGroup
      SubnetId: !Select [1, !Ref Subnets]

Will fail if the user will provide only one subnet.

Attempt to add the following condition:

Conditions:
   IsSubnet1: !Not [!Equals [!Select [ 0, !Ref Subnets], !Ref AWS::NoValue ]] 
   IsSubnet2: !Not [!Equals [!Select [ 1, !Ref Subnets], !Ref AWS::NoValue ]] 

Fails with the error: ValidationError: Template error: every Fn::Equals object requires a list of 2 string parameters.

Another attempt:

Conditions:
   IsSubnet1: !Not [!Equals [!Select [ 0, !Ref Subnets], "" ]] 
   IsSubnet2: !Not [!Equals [!Select [ 1, !Ref Subnets], "" ]] 

Fails with the error: ValidationError: Template error: Fn::Select cannot select nonexistent value at index 1

enhancement

Most helpful comment

This is, I think, a strong use case for a Fn::Map function.

All 6 comments

Yes, as one of the ways to resolve the issue. Thanks.

This is, I think, a strong use case for a Fn::Map function.

I never considered asking for Fn:Map. It would solve a lot of my problems 馃く

If they are not adding the easier Fn::Size, no way in hell are they adding Fn::Map.

I'm persistent in leaving feedback :). I think Fn:Map fits a lot better with the declarative model in CloudFormation. It would solve more problems than Fn:Size and in a cleaner way (which is in line with how AWS likes to address things).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

grauj-aws picture grauj-aws  路  3Comments

ghost picture ghost  路  4Comments

tortila picture tortila  路  3Comments

baxang picture baxang  路  3Comments

rjpereira picture rjpereira  路  4Comments