繁体   English   中英

如何在没有提示的情况下在没有交互的情况下自动在 Amazon AWS EC2 上运行 aws configure?

[英]How to run aws configure on Amazon AWS EC2 automatically without interaction without prompt?

我正在尝试设置 Amazon AWS EC2 实例以与 s3 通信。 基本命令是

aws configure

然后按照提示输入

AWS Access Key ID [None]: my-20-digit-id
AWS Secret Access Key [None]: my-40-digit-secret-key
Default region name [None]: us-east-1
Default output format [None]: text

但是,我真正想要的是命令

aws configure

自动无需交互,即不提示并等待输入

我知道有文件在

~.aws/credentials
~.aws/config

我把这 4 个键 = 值对放在哪里。 “凭证”文件看起来像

[default]
aws_secret_access_key = my-40-digit-secret-key
aws_access_key_id = my-20-digit-id

而“配置”文件看起来像

[default]
region = us-east-1
output = text

但是,对于 ~/.aws/ 中的那些文件,我进入 ~/.aws/,并在命令行中键入并输入命令

aws configure

我仍然收到询问我的提示

AWS Access Key ID [None]:
AWS Secret Access Key [None]:
Default region name [None]:
Default output format [None]:

如果我没有在提示符下输入有效值,我将无法连接到 s3,例如通过命令

aws s3 ls s3://mybucket

我转向帮助亚马逊 aws 文档页面。 在这个页面上,它提到了这个选项

“命令行选项 – 区域、output 格式和配置文件可以指定为命令选项以覆盖默认设置。”

作为 aws configure 的第一个选项

https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html

但是,它没有提到如何使用命令行选项。 我试过这样的事情

aws configure --region us-east-1

但我还是得到了

AWS Access Key ID [None]:
AWS Secret Access Key [None]:
Default region name [None]:
Default output format [None]:

就像我没有选择“--region us-east-1”一样

如果我尝试

aws configure --aws_access_key_id my-20-digit-id --aws_secret_access_key my-40-digit-secret-key --region us-east-1

我明白了

usage: aws [options] <command> <subcommand> [parameters]
aws: error: argument subcommand: Invalid choice, valid choices are:

我如何运行命令

aws configure

自动,没有提示,没有交互。

请帮忙! TIA

编辑并回复 helloV,因为主帖中的格式比评论更清晰。 我尝试了提到的命令 helloV,但出现错误

aws configure set aws_access_key_id my-20-digit-id
usage: aws [options] <command> <subcommand> [parameters]
aws: error: argument subcommand: Invalid choice, valid choices are:

不过谢谢。 继续“aws configure set” 在我已经设置连接到 s3 的另一个 EC2 实例上,我输入

aws configure set region us-east-1

运行并返回到命令提示符“>”

aws configure set aws_access_key_id my-20-digit-id

运行并返回到命令提示符“>”

aws configure set aws_secret_access_key my-40-digit-secret-key

运行并返回到命令提示符“>”

aws configure

运行但带有提示并等待交互

AWS Access Key ID [****************ABCD]:
AWS Secret Access Key [****************1234]:
Default region name [us-east-1]:
Default output format [text]:

helloV:这是我的屏幕样子

ubuntu@ip-11111:~/.aws$ more config
[default]
region = us-east-1
output = text
ubuntu@ip-11111:~/.aws$ more credentials
[default]
aws_secret_access_key = my-40-digit-secret-key
aws_access_key_id = my-20-digit-id
ubuntu@ip-11111:~/.aws$ aws s3 ls s3://

我懂了

Unable to locate credentials. You can configure credentials by running "aws configure".

在此之后,我跑

aws configure list
  Name                    Value             Type    Location
  ----                    -----             ----    --------
profile                <not set>             None    None
access_key                <not set>             None    None
secret_key                <not set>             None    None
region                us-east-1      config_file    ~/.aws/config

看起来它不检查 ~/.aws/credentials 文件,但 ~/.aws/config 文件在列表中。

我终于明白了。 使用导出如

export AWS_ACCESS_KEY_ID=my-20-digit-id
export AWS_SECRET_ACCESS_KEY=my-40-digit-secret-key
export AWS_DEFAULT_REGION=us-east-1

然后运行

aws s3 ls s3://

会工作。 不要像其他人提到的那样运行“aws configure”。

谢谢你们。

你很好地描述了这个文件。 为什么不创建一个文件并将其放在正确的位置? 我刚试过......它和运行aws configure完全一样

更新 :您提到您要从EC2实例访问S3。 在这种情况下,您根本不应该使用凭据。 您应该使用角色代替

这些命令对我有用。 如果这不适合你。 尝试第一次使用交互模式aws configure

aws --profile default configure set aws_access_key_id "my-20-digit-id"

aws --profile default configure set aws_secret_access_key "my-40-digit-secret-key"

解决方案是您实际上不必运行 aws configure。 在您第一次运行它并建立凭据 (~/.aws/credentials) 和配置 (~/,aws/config) 之后。 展望未来,您只需运行所需的 aws 命令。 我用 cron 作业对此进行了测试,并执行了“aws s3 ls”命令,它无需在它之前提供配置命令即可工作。

按照这个命令

$aws configure set aws_access_key_id default_access_key
$ aws configure set aws_secret_access_key default_secret_key
$ aws configure set default.region us-west-2

或者

aws configure set aws_access_key_id <key_id> && aws configure set aws_secret_access_key <key> && aws configure set default.region us-east-1

有关详细信息,请使用此链接https://awscli.amazonaws.com/v2/documentation/api/latest/reference/configure/set.html

我使用这样的东西:

aws configure --profile my-profile-name <<-EOF > /dev/null 2>&1
${AWS_ACCESS_KEY_ID}
${AWS_SECRET_ACCESS_KEY}
${AWS_REGION}
text
EOF

还要在自动化过程后进行清理,而不是删除 `~/.aws/ 目录(因为其他一些凭证可能存储在那里)我运行:

aws configure --profile my-profile-name <<-EOF > /dev/null 2>&1
null
null
null
text
EOF

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM