如何让AWS CLI接受YAML文件作为--target-tracking-configuration参数
解决AWS CLI识别YAML格式目标追踪配置的问题
当执行aws autoscaling put-scaling-policy命令时,直接指定YAML配置文件作为--target-tracking-configuration参数会触发解析错误,报错示例:
Error parsing parameter '--target-tracking-configuration': Expected: '=', received: 'T' for input: --- TargetValue: 75 ^ PredefinedMetricSpecification: PredefinedMetricType: ASGAverageCPUUtilization
而使用JSON格式配置文件则正常,这是因为AWS CLI的file://输入默认仅支持JSON结构。以下是两种可行的解决方法:
方法1:使用yq工具转换YAML为JSON后传递
yq是一款YAML处理工具,可将YAML文件转换为JSON格式,通过进程替换直接传递给CLI:
aws autoscaling put-scaling-policy \ --policy-name your-policy-name \ --auto-scaling-group-name your-asg-name \ --target-tracking-configuration file://<(yq -o json path/to/your/config.yaml)
注:需提前安装yq(可通过包管理器如brew install yq或apt install yq完成安装)。
方法2:用Python内置模块转换
如果没有yq,可借助Python的yaml和json模块完成转换:
aws autoscaling put-scaling-policy \ --policy-name your-policy-name \ --auto-scaling-group-name your-asg-name \ --target-tracking-configuration file://<(python3 -c "import yaml, json; print(json.dumps(yaml.safe_load(open('path/to/your/config.yaml'))))")
注:需确保Python环境已安装pyyaml模块(可通过pip install pyyaml安装)。
内容的提问来源于stack exchange,提问作者Frank
相关产品推荐
相关产品推荐

