如何将文件内容作为 aws cli 选项值提供
2020-03-22
696
我正在尝试在 Linux Box 中借助 AWS CLI 创建 SFTP 用户。
下面是我在 bash 脚本中传递的 AWS CLI 命令(我的 ssh 公钥在一个文件中,借助变量,我将其传递到 AWS CLI 选项部分)
customer_name_pub_value=$(cat /home/developer/naman/dir/$customer_name.pub)
aws transfer create-user --user-name $customer_name --home-directory script-test/power-archive-ireland/$customer_name/ --server-id s-aaabbbccc --ssh-public-key-body $customer_name_pub_value --tags 'Key=Product,Value="demo",Key=Environment,Value=dev,Key=Contact,Value="[email protected]",Key=Service,Value="sftp"' --role customer-sftp-role
下面是我在执行上述命令时遇到的错误:
[developer@dev-lin demo]$ aws transfer create-user --user-name $customer_name --home-directory script-test/power-archive-ireland/$customer_name/ --server-id s-aaabbbccc --ssh-public-key-body $customer_name_pub_value --tags 'Key=Product,Value="demo",Key=Environment,Value=dev,Key=Contact,Value="[email protected]",Key=Service,Value="sftp"' --role customer-sftp-role
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
Unknown options: [email protected], XXXXXXXXXXAB3NzaC1yc2EAAAADAQABAAABAQCm2hI3Y33K1GVbdQV0lfkm/klZRJS7Kcz8+53e/BoIbVMFH0jqm1aejELDFgPnN7HvIZ/csYGzF/ssTx5lXVaHQh/qkYwfqQBg8WvXVB0Jmogj1hr6z5M8Qy/3oCx0fSmh6e/Ekfk8vHhiHQlGZV3o8a2AW5SkP8IH/OgT6Bq+SMuB+xtSciVBZqSLI0OgYtOZ0MyxBzfLau1Tyegu5lVFevZDVjecnIaS4l+v2VIQ/OgaZ40oAI3NuRZ2EdnLqEqFyLjasx4kcuwNzD5oaXAU6T9UsqKN2rVLMKrXXXXXXXXXXX
我在传递选项值时是否遗漏了一些 bash 语法!
更新 2020 年 3 月 30 日 根据以下评论中的建议,我在命令中添加了 AWS ARN 角色,现在面临的问题与以前不同
代码:
customer_name='demo'
customer_name_pub_value=$(cat /home/developer/naman/dir/$customer_name.pub)
aws transfer create-user --user-name $customer_name --home-directory script-test/power-archive-ireland/$customer_name/ --server-id s-aaabbbccc --ssh-public-key-body "$customer_name_pub_value" --tags 'Key=Product,Value="demo",Key=Environment,Value=dev,Key=Contact,Value="[email protected]",Key=Service,Value="sftp"' --role "arn:aws:iam::8XXXXXXXXX2:role/customer-sftp-role"
错误
An error occurred (ValidationException) when calling the CreateUser operation: 1 validation error detected: Value 'script-test/power-archive-ireland/demo/' at 'homeDirectory' failed to satisfy constraint: Member must satisfy regular expression pattern: ^$|/.*
2个回答
是的,对于最后一个错误,您应该将其作为对象列表提供:
--tags [{Key="Product", Value="demo"}, {Key="Environment", Value="dev"}, {Key="Contact", Value="[email protected]"}, {Key="Service", Value="sftp"
您可能需要将“Key”和“Value”放在引号中,甚至可能必须尝试键:值对(即 {"Product": "demo"}),但这应该是一般语法。
user1394
2020-03-31
以下是最终有效的 CLI 命令:
更改
-
添加了 ROLE ARN(感谢 @user1394 的建议)
-
通过将 / 放在 --home-directory 选项之前解决了最大问题(糟糕的 AWS 文档( https://docs.aws.amazon.com/cli/latest/reference/transfer/create-user.html )及其过时的 RegEx
^$|/.*
) -
将损坏的 CLI 转换为基于 JSON 的 CLI 以修复最终错误(并非所有标签都能够附加在旧命令中)
#!/bin/bash
customer_name='demo'
customer_name_pub_value=$(cat /home/developer/naman/dir/$customer_name.pub)
aws transfer create-user \
--user-name $customer_name \
--server-id s-aaabbbccc \
--role "arn:aws:iam::8XXXXXXXXX2:role/customer-sftp-role" \
--ssh-public-key-body "$customer_name_pub_value" \
--home-directory /script-test/power-archive-ireland/$customer_name \
--tags '[
{"Key": "Product", "Value": "demo"},
{"Key": "Environment", "Value": "dev"},
{"Key": "Contact", "Value": "[email protected]"},
{"Key": "Service", "Value": "sftp"}
]'
Naman Joshi
2020-03-31