Skip to content

fix(intrinsics): accept a string index in Fn::Select - #9406

Merged
vicheey merged 1 commit into
aws:developfrom
Shaurya55555:fix/fn-select-string-index
Nov 13, 2026
Merged

vicheey merged 1 commit into
aws:developfrom
Shaurya55555:fix/fn-select-string-index

Conversation

@Shaurya55555

Copy link
Copy Markdown
Contributor

Which issue(s) does this change fix?

None filed. I found it while comparing IntrinsicResolver with the newer Fn::Select resolver in cfn_language_extensions.

Why is this change necessary?

CloudFormation accepts the index of Fn::Select as a string. The example in its documentation is { "Fn::Select" : [ "1", [ "apples", "grapes", "oranges", "mangoes" ] ] }, which returns "grapes" (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-select.html).

IntrinsicResolver.handle_fn_select only accepts an int, so it raises InvalidIntrinsicException for "1". SamBaseProvider.get_template calls the resolver with ignore_errors=True, so the property is silently left unresolved, for example a CodeUri of {"Fn::Select": ["0", {"Fn::Split": [",", {"Ref": "Dirs"}]}]} stays a dict. FnSelectResolver in cfn_language_extensions already accepts a string index, so the two resolvers disagree on the same template.

How does it address the issue?

An index that is a string is converted with int() before the existing type check. A non-numeric string still raises the same error.

Tests: the existing test_fn_select_index_invalid_index_type listed "3" as an invalid index, which encoded the same assumption, so I changed that value to "three". I added test_fn_select_string_index, using the documentation example.

What side effects does this change have?

Templates that pass a quoted index now resolve instead of being left as they are. Integer indexes and all other types behave as before. As in FnSelectResolver, int() also accepts surrounding whitespace and a sign.

Mandatory Checklist

PRs will only be reviewed after checklist is complete

  • Review the generative AI contribution guidelines (I used an AI assistant, Claude Code, to find and draft this. I reviewed the change and can answer questions about it.)
  • Add input/output type hints to new functions/methods (no new functions)
  • Write design document if needed (not needed)
  • Write/update unit tests
  • Write/update integration tests (not needed for this change)
  • Write/update functional tests if needed (not needed)
  • make pr passes (not run. black --check -l 120 passes on both files, and ruff check shows only a long line in an unrelated existing test that is the same on develop)
  • make update-reproducible-reqs if dependencies were changed (no dependencies changed)
  • Write documentation (not needed)

How I checked it

The resolver files are copies of develop before and after this change, with a stub for the one unrelated import (get_template_data, used only by Fn::Transform).

  • {"Fn::Select": ["1", ["apples", "grapes", "oranges", "mangoes"]]} raises InvalidIntrinsicException before and returns grapes after. "three", None, True, "7" and "-1" still raise.
  • resolve_template(ignore_errors=True) on a CodeUri of {"Fn::Select": ["0", {"Fn::Split": [",", {"Ref": "Dirs"}]}]} leaves the dict unresolved before and gives src after (integer index 0 gave src before too).
  • The 22 Fn::Select unit tests pass after the change. Running the new test file against the old code, test_fn_select_string_index fails with The argument to Fn::Select must resolve to a int type.
  • Not run: the full unit suite, integration tests and make pr.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

CloudFormation accepts the index of Fn::Select as a string, and its documentation example is {"Fn::Select": ["1", ["apples", "grapes", "oranges", "mangoes"]]}. IntrinsicResolver rejected it, so with ignore_errors=True the property was left unresolved. The newer Fn::Select resolver in cfn_language_extensions already accepts it. The existing test that asserted a string index fails is changed to use a non-numeric string.
@Shaurya55555
Shaurya55555 requested a review from a team as a code owner November 5, 2026 19:42
@github-actions github-actions Bot added pr/external stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Nov 5, 2026

@vicheey vicheey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution.

@bnusunny bnusunny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is a clean fix and it brings IntrinsicResolver in line with FnSelectResolver and the CloudFormation docs. Negative strings are still caught by verify_in_bounds, and the updated invalid-type case makes sense. Two optional nits: (1) int() also accepts things like " 1 " or "1_0", which is consistent with the language-extensions resolver, so it's fine as-is; (2) a test with the index coming from a Ref to a String parameter would cover the real-world case you described. LGTM.

@vicheey
vicheey merged commit 63cdc3a into aws:develop Nov 13, 2026
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr/external stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants