我在Amazon EC2上托管了两个不同的应用程序(好吧,第二个应用程序即将上线)。

我如何在命令行(Mac OS X)使用两个帐户,但保持EC2密钥和证书分开?我是否需要在每个ec2-*命令之前更改我的环境变量?

是否使用别名并将其设置为环境在线工作?例如:alias ec2-describe-instances1 = export EC2_PRIVATE_KEY=/path;ec2-describe-instances


当前回答

新的aws工具现在支持多个配置文件。

如果您使用工具配置访问,它会自动在~/.aws/config中创建一个默认值。

然后您可以添加其他配置文件-更多详细信息请参见:AWS CLI入门

其他回答

您应该能够使用以下命令选项来代替EC2_PRIVATE_KEY(甚至EC2_CERT)环境变量:

-K <私钥> - c <证书>

你可以把这些放在别名里面。

alias ec2-describe-instances1 ec2-describe-instances -K /path/to/key.pem

您可以通过在aws命令行上创建两个概要文件来使用两个帐户。 它将提示您输入AWS访问密钥ID、AWS秘密访问密钥和所需区域,因此请准备好它们。

例子:

$ aws configure --profile account1
$ aws configure --profile account2

然后,您可以通过传递该命令的配置文件在帐户之间切换。

$ aws dynamodb list-tables --profile account1
$ aws s3 ls --profile account2

注意:

如果你将配置文件命名为default,它将成为默认配置文件,即当命令中没有——profile参数时。


关于默认配置文件的更多信息

如果您花更多的时间使用account1,可以通过设置AWS_DEFAULT_PROFILE环境变量将其设置为默认值。当设置了默认环境变量时,不需要在每个命令上指定概要文件。

Linux、OS X示例:

$ export AWS_DEFAULT_PROFILE=account1
$ aws dynamodb list-tables

Windows的例子:

$ set AWS_DEFAULT_PROFILE=account1
$ aws s3 ls

创建或编辑此文件:

vim ~/.aws/credentials

列出尽可能多的密钥对:

[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

[user1]
aws_access_key_id=AKIAI44QH8DHBEXAMPLE
aws_secret_access_key=je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY

设置一个局部变量来选择你想要使用的一对键:

export AWS_PROFILE=user1

做你喜欢做的事:

aws s3api list-buckets  # any aws cli command now using user1 pair of keys

你也可以通过在每个命令中包含——profile user1来逐个命令执行:

aws s3api list-buckets --profile user1
# any aws cli command now using user1 pair of keys

更多详细信息:AWS CLI的命名配置文件

To use an IAM role, you have to make an API call to STS:AssumeRole, which will return a temporary access key ID, secret key, and security token that can then be used to sign future API calls. Formerly, to achieve secure cross-account, role-based access from the AWS Command Line Interface (CLI), an explicit call to STS:AssumeRole was required, and your long-term credentials were used. The resulting temporary credentials were captured and stored in your profile, and that profile was used for subsequent AWS API calls. This process had to be repeated when the temporary credentials expired (after 1 hour, by default).

更多详细信息:如何使用单个IAM用户通过AWS CLI轻松访问您的所有帐户

恕我冒昧,最简单的方法是手动编辑.aws/credentials和.aws/config文件。

它很简单,适用于Linux、Mac和Windows。阅读这篇文章了解更多细节(1分钟阅读)。

.aws /凭证文件:

[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

[user1]
aws_access_key_id=AKIAI44QH8DHBEXAMPLE
aws_secret_access_key=je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY

aws /配置文件:。

[default]
region=us-west-2
output=json

[profile user1]    <-- 'profile' in front of 'profile_name' (not for default)!!
region=us-east-1
output=text