我在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访问密钥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
其他回答
您可以编写shell脚本,根据用户的输入,为每个帐号设置相应的环境变量值。这样做,您不需要创建任何别名,此外,ELB工具,自动缩放命令行工具等工具也可以在多个帐户下工作。
您可以通过在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
恕我冒昧,最简单的方法是手动编辑.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
您应该能够使用以下命令选项来代替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轻松访问您的所有帐户