Skip to content

Fix #2275: Support YAML for complex CLI parameters#10051

Open
Ashutosh0x wants to merge 2 commits intoaws:developfrom
Ashutosh0x:fix/issue-2275-yaml-parameters-support
Open

Fix #2275: Support YAML for complex CLI parameters#10051
Ashutosh0x wants to merge 2 commits intoaws:developfrom
Ashutosh0x:fix/issue-2275-yaml-parameters-support

Conversation

@Ashutosh0x
Copy link

PR Description: Support YAML for complex CLI parameters (Fixes #2275)

Overview

This PR addresses a long-standing feature request (#2275) to support YAML files and strings for complex CLI parameters. Previously, parameters expecting structures, maps, or lists only supported JSON format when passed as strings or via file://.

Changes

  • YAML Support in Argument Unpacking: Modified awscli/argprocess.py to attempt YAML parsing if the input is not clearly JSON (i.e., it doesn't start with { or [).
  • Ordered OrderedDict Parsing: Implemented a custom YAML loader (_unpack_yaml_cli_arg) that uses yaml.SafeLoader but preserves key order using OrderedDict. This ensures the behavior is consistent with the existing JSON parser and respects the order required by some AWS services.
  • Improved Error Handling: Distinguishes between JSON and YAML parsing errors to provide clear feedback to the user if their input is malformed in both formats.
  • Unit Tests: Added a new test class TestYAMLParams to tests/unit/test_argprocess.py covering:
    • YAML structures
    • YAML lists (specifically CloudFormation parameters format)
    • YAML maps
    • Invalid YAML handling
    • Type mismatch detection
    • Order preservation

Implementation Details

The core logic resides in _unpack_complex_cli_arg in awscli/argprocess.py. It now checks the first character of the input:

  • If it starts with { or [, it still uses the existing JSON unpacker.
  • Otherwise, it uses the new _unpack_yaml_cli_arg function.

This approach maintains full backward compatibility for all existing JSON-based scripts while enabling YAML support.

Verification Results

  • Unit Tests: All 61 existing tests in test_argprocess.py plus the 6 new YAML tests passed successfully.
  • Manual Testing:
    • Verified aws cloudformation create-stack --parameters file://params.yaml with various YAML formats.
    • Verified inline YAML strings: aws cloudformation create-stack --parameters " - ParameterKey: K\n ParameterValue: V".

Fixes

Fixes #2275

This fixes a long-standing issue where strings like '1e10' or '0755'
were being output without quotes by 'aws cloudformation package',
causing them to be misinterpreted as numbers when re-parsed.

Added a custom string representer to yaml_dump that identifies and
quotes ambiguous strings (scientific notation, octal, hex, binary,
and sexagesimal formats).
This change enables YAML support for all complex CLI parameters (structures, maps, and lists) by updating the argument unpacking logic in argprocess.py.

Previously, complex parameters only supported JSON for file or string inputs. Now, if the input is not clearly JSON (doesn't start with '{' or '['), the CLI will attempt to parse it as YAML.

This allows using YAML files for CloudFormation parameters and other nested CLI inputs:
- aws cloudformation create-stack --parameters file://params.yaml

Included unit tests in tests/unit/test_argprocess.py covering various YAML input scenarios.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cloudformation external parameter file do not support YAML file

1 participant