我在Amazon EC2上托管了两个不同的应用程序(好吧,第二个应用程序即将上线)。
我如何在命令行(Mac OS X)使用两个帐户,但保持EC2密钥和证书分开?我是否需要在每个ec2-*命令之前更改我的环境变量?
是否使用别名并将其设置为环境在线工作?例如:alias ec2-describe-instances1 = export EC2_PRIVATE_KEY=/path;ec2-describe-instances
我在Amazon EC2上托管了两个不同的应用程序(好吧,第二个应用程序即将上线)。
我如何在命令行(Mac OS X)使用两个帐户,但保持EC2密钥和证书分开?我是否需要在每个ec2-*命令之前更改我的环境变量?
是否使用别名并将其设置为环境在线工作?例如:alias ec2-describe-instances1 = export EC2_PRIVATE_KEY=/path;ec2-describe-instances
当前回答
新的aws工具现在支持多个配置文件。
如果您使用工具配置访问,它会自动在~/.aws/config中创建一个默认值。
然后您可以添加其他配置文件-更多详细信息请参见:AWS CLI入门
其他回答
您可以编写shell脚本,根据用户的输入,为每个帐号设置相应的环境变量值。这样做,您不需要创建任何别名,此外,ELB工具,自动缩放命令行工具等工具也可以在多个帐户下工作。
新的aws工具现在支持多个配置文件。
如果您使用工具配置访问,它会自动在~/.aws/config中创建一个默认值。
然后您可以添加其他配置文件-更多详细信息请参见:AWS CLI入门
您应该能够使用以下命令选项来代替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的命名配置文件
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轻松访问您的所有帐户