json - 如何在 AWS CLI 上使用 AWS-RunRemoteScript?
问题描述
我正在尝试做的事情
我正在尝试使用 Windows 上的 AWS CLI 将 PowerShell 脚本从 GitHub 存储库中提取到 AWS EC2 实例上,然后在实例上运行该脚本。
我试过的
我知道转义的双引号 (\") 是有效的,因为我在没有它们的情况下尝试过无济于事。我还尝试将 --parameters 之后的 JSON 部分放在单独的文件中并将该文件加载到命令中,但是在这种情况下,它抱怨格式化。
我已经在 PowerShell 和 Windows CMD 中尝试过该命令(两者的语法不同,即使用双引号)。我在下面包含了两种变体。
使用 AWS EC2 控制台,我可以使用图形界面让所有这些工作。现在的关键是让它也可以使用命令行工作。
这是 PowerShell 的命令:
aws ssm send-command --document-name "AWS-RunRemoteScript" --document-version "1" --targets "Key=instanceids,Values=<INSTANCE-ID>" --parameters '{\"sourceType\":[\"GitHub\"],\"sourceInfo\":{\"owner\":\"<USERNAME>\",\"repository\":\"Projects\",\"path\":\"test_script.ps1\"},\"commandLine\":[\".\\test_script.ps1\"],\"workingDirectory\":[""],\"executionTimeout\":[\"3600\"]}' --timeout-seconds 600 --max-concurrency "50" --max-errors "0" --region us-east-2
问题似乎来自 --parameters 参数和它后面的 JSON。
这是 Git Bash 的命令(实际上有效):
aws ssm send-command --document-name "AWS-RunRemoteScript" --document-version "1" --targets "Key=instanceids,Values=<INSTANCE-ID>" --parameters '{"sourceType":["GitHub"],"sourceInfo":["{\"owner\": \"<USERNAME>\", \"repository\": \"Projects\", \"path\": \"test_script.ps1\"}"],"commandLine":[".\\test_script.ps1"],"workingDirectory":[""],"executionTimeout":["3600"]}' --timeout-seconds 600 --max-concurrency "50" --max-errors "0" --region us-east-2
这是产生的错误:
Parameter validation failed: Invalid type for parameter Parameters.sourceInfo, value: OrderedDict([('owner', '<USERNAME>'), ('repository', 'Projects'), ('path', 'test_script.ps1')]), type: <class 'collections.OrderedDict'>, valid types: <class 'list'>, <class 'tuple'>
如何以适当的格式指定这些参数?
解决方案
您总是可以发疯(就像我一样)并双重转义内容。参数的值应该是一个表示 json 的字符串,该字符串包含一个属性,该属性是表示 json 的字符串集合。
我在这里是认真的。
aws ssm send-command --document-name "AWS-RunRemoteScript" --document-version "1" --targets "Key=instanceids,Values=<INSTANCE-ID>" --parameters '{\"sourceType\":[\"GitHub\"],\"sourceInfo\":[\"{\\\"owner\\\":\\\"<USERNAME>\\\",\\\"repository\\\":\\\"Projects\\\"}\"],\"commandLine\":[\".\\test_script.ps1\"],\"workingDirectory\":[""],\"executionTimeout\":[\"3600\"]}' --timeout-seconds 600 --max-concurrency "50" --max-errors "0" --region us-east-2
所以这里的整个技巧是通过将转义的反斜杠放在正确的地方\\
已经转义的引号之前来创建转义的反斜杠。\"
推荐阅读
- firebase - 如何将 JSON 导入 Cloud Firestore
- powerbi - Power Bi DAX:累积和未显示正确数据
- node.js - 可以使用 Heroku 上的流下载 TXT 文件,但无法下载 PDF 文件 - NodeJS、Heroku
- swift - 如何正确地将 ImagePicker 插入标题中的图像?SwiftUI
- realm - 从 webRTC 客户端为 coTurn 提供多个领域/来源
- python - 如何从循环中创建的多个条目小部件中获取输入?
- scala - 代码中的Scala Spark Task不可序列化错误
- sql-server - 在函数中执行动态 sql 的解决方法
- typescript - 类型“RefObject>”上不存在属性“getPickerData”
- authentication - 如何通过对主机的 LDAP 身份验证执行 Ansible 剧本?