Course contentsShow
← Cloud and Infrastructure
Lesson 872 of 2,98916. AWS CloudFormationFree lesson

Fn::Select and List Functions

Access elements from lists by index using Select and manipulate arrays with list functions.

What You'll Learn

You'll learn how to access individual elements from a list using Fn::Select and work with array-like structures in CloudFormation templates.

Understanding Fn::Select

Think of Fn::Select like picking a specific item from a grocery list by its position. If your list has ["apples", "bananas", "oranges"], asking for position 1 gives you "bananas" (remember, counting starts at zero!).

In CloudFormation, Fn::Select retrieves a single element from a list by its index position. This is invaluable when you need to extract specific values from arrays returned by other functions or from Parameters.

Basic Syntax

The short form in YAML:

!Select [index, list]

The long form (also works in JSON):

Fn::Select:
  - index
  - list

Common Use Cases

Selecting from a CommaDelimitedList parameter:

Parameters:
  SubnetIds:
    Type: CommaDelimitedList
    Default: "subnet-abc123,subnet-def456,subnet-ghi789"

Resources:
  MyInstance:
    Type: AWS::EC2::Instance
    Properties:
      SubnetId: !Select [0, !Ref SubnetIds]  # Gets first subnet

Combining with Fn::GetAZs:

Resources:
  MySubnet:
    Type: AWS::EC2::Subnet
    Properties:
      AvailabilityZone: !Select [1, !GetAZs '']  # Second AZ in region

Index Boundaries

The index must be within the list's bounds. Index 0 is the first element, 1 is the second, and so on. Going out of bounds causes template errors.

Key Takeaway:
Fn::Select extracts a single element from a list by index position, enabling precise control over array values returned from parameters, functions like Fn::GetAZs, or Fn::Split results.