Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bug: CloudFormation fails to process nested Fn::Select with Fn::FindInMap in conditions #12775

Open
@da-luce

Description

@da-luce

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

When deploying a CloudFormation template with nested intrinsic functions in conditions (specifically Fn::Select with Fn::FindInMap), Localstack fails with a KeyError: '0'. This prevents testing of common CloudFormation patterns locally.

Error: Failed to create changeset for the stack: test, An error occurred (InternalError) when calling the CreateChangeSet operation: exception while calling cloudformation.CreateChangeSet: ... KeyError: '0'

Expected Behavior

The template should process successfully as it does in real AWS.

How are you starting LocalStack?

With a docker-compose file

Steps To Reproduce

How are you starting localstack (e.g., bin/localstack command, arguments, or docker-compose.yml)

docker-compose.yml:

services:
  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME:-localstack-main}"
    image: localstack/localstack
    ports:
      - "127.0.0.1:4566:4566"            # LocalStack Gateway
      - "127.0.0.1:4510-4559:4510-4559"  # external services port range
    environment:
      # LocalStack configuration: https://docs.localstack.cloud/references/configuration/
      - DEBUG=${DEBUG:-1}
    volumes:
      - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

Example Template

template.yml

AWSTemplateFormatVersion: '2010-09-09'
Description: 'Minimal template to demonstrate Localstack condition issue'

Mappings:
  RegionMap:
    us-east-1:
      AZs:
      - "empty"

Conditions:
  UseFnGetAZs:
    Fn::Equals:
    - "empty"
    - Fn::Select:
      - "0"
      - Fn::FindInMap:
        - "RegionMap"
        - Ref: "AWS::Region"
        - "AZs"

Resources:
  TestBucket:
    Type: 'AWS::S3::Bucket'
    Condition: UseFnGetAZs
    Properties:
      BucketName: test-bucket-3492340

Client commands (e.g., AWS SDK code snippet, or sequence of "awslocal" commands)

Deploying to real AWS account is fine
sam deploy --stack-name test --region us-east-1

results in normal sam deployment:

...
Successfully created/updated stack - test in us-east-1
Deploying to LocalStack fails
samlocal deploy --stack-name test --region us-east-1

fails with

Error: Failed to create changeset for the stack: test, An error occurred (InternalError) when calling the CreateChangeSet operation (reached max retries: 4): exception while calling cloudformation.CreateChangeSet: Traceback (most recent call last):
  File "/opt/code/localstack/.venv/lib/python3.11/site-packages/rolo/gateway/chain.py", line 166, in handle
    handler(self, self.context, response)
  File "/opt/code/localstack/localstack-core/localstack/aws/handlers/service.py", line 113, in __call__
    handler(chain, context, response)
  File "/opt/code/localstack/localstack-core/localstack/aws/handlers/service.py", line 83, in __call__
    skeleton_response = self.skeleton.invoke(context)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/code/localstack/localstack-core/localstack/aws/skeleton.py", line 154, in invoke
    return self.dispatch_request(serializer, context, instance)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/code/localstack/localstack-core/localstack/aws/skeleton.py", line 168, in dispatch_request
    result = handler(context, instance) or {}
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/code/localstack/localstack-core/localstack/aws/skeleton.py", line 118, in __call__
    return self.fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/code/localstack/localstack-core/localstack/aws/api/core.py", line 178, in operation_marker
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/code/localstack/localstack-core/localstack/services/cloudformation/provider.py", line 731, in create_change_set
    resolved_stack_conditions = resolve_stack_conditions(
                                ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/code/localstack/localstack-core/localstack/services/cloudformation/engine/template_utils.py", line 112, in resolve_stack_conditions
    result[condition_name] = resolve_condition(
                             ^^^^^^^^^^^^^^^^^^
  File "/opt/code/localstack/localstack-core/localstack/services/cloudformation/engine/template_utils.py", line 291, in resolve_condition
    right = resolve_condition(
            ^^^^^^^^^^^^^^^^^^
  File "/opt/code/localstack/localstack-core/localstack/services/cloudformation/engine/template_utils.py", line 336, in resolve_condition
    return options[index]
           ~~~~~~~^^^^^^^
KeyError: '0'

Environment

- OS: macOS 15.5 (24F74)
- LocalStack: 
  LocalStack version: 4.4.1.dev66
  LocalStack Docker image sha: 28a7fe23332dd8180089cad651548e996bdccc67ece26d66de4e06fcf6d7d7f8
  LocalStack build date: 2025-06-02
  LocalStack build git hash: 374183c42

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions