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

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

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


当前回答

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

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

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

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

其他回答

创建或编辑此文件:

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的命名配置文件

我写了一个工具箱来切换默认的AWS配置文件。

该机制是将配置文件键物理地移动到配置文件和凭证文件中的默认部分。

今天更好的解决方案应该是以下方法之一:

使用aws命令选项——profile。 使用环境变量AWS_PROFILE。

我不记得为什么我没有使用-profile的解决方案,可能我没有意识到它的存在。

然而,该工具包在做其他事情时仍然有用。以后我将使用AWS_PROFILE的方式添加软开关标志。

$ xsh list aws/cfg
[functions] aws/cfg/move
[functions] aws/cfg/set
[functions] aws/cfg/activate
[functions] aws/cfg/get
[functions] aws/cfg/delete
[functions] aws/cfg/list
[functions] aws/cfg/copy

回购:https://github.com/xsh-lib/aws

安装:

curl -s https://raw.githubusercontent.com/alexzhangs/xsh/master/boot | bash && . ~/.xshrc
xsh load xsh-lib/aws

用法:

xsh aws/cfg/list
xsh aws/cfg/activate <profilename>

您可以通过在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

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帐户?

1)获取门禁钥匙

AWS控制台>身份和访问管理(IAM) >您的安全凭据>访问密钥

2)设置访问文件和内容

~ / .aws /凭证

[default]
aws_access_key_id={{aws_access_key_id}}
aws_secret_access_key={{aws_secret_access_key}}

[{{profile_name}}]
aws_access_key_id={{aws_access_key_id}}
aws_secret_access_key={{aws_secret_access_key}}

3)设置profile - file和content

~ - aws -配置。

[default]
region={{region}}
output={{output:"json||text"}}

[profile {{profile_name}}]
region={{region}}
output={{output:"json||text"}}

4)使用参数运行文件

安装命令行应用程序-并使用AWS命令行,例如产品AWS EC2

Aws ec2 description -instances——默认

Aws ec2 describe-instances——profile {{profile_name}}——[{{profile_name}}]


Ref

https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html