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

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

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


当前回答

我写了一个工具箱来切换默认的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帐户?

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

创建或编辑此文件:

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

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

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

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

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

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