简体   繁体   English

如何在使用 AWS Organizations 从 aws-cli 创建的账户之间切换

[英]How to switch between accounts created using AWS Organizations from aws-cli

I am using AWS Organizations from my master account to create sub-accounts like prod, dev, playground etc.我正在使用我的主账户中的 AWS Organizations 创建子账户,例如 prod、dev、playground 等。

Inside the AWS Console it is easy to switch between the accounts by clicking the "Switch Role" button.在 AWS 控制台中,通过单击“切换角色”按钮可以轻松地在账户之间切换。

How do I achieve the same from the aws-cli using profiles?如何使用配置文件从 aws-cli 实现相同的目标? Can somebody list the least amount of steps necessary to achieve that?有人可以列出实现这一目标所需的最少步骤吗?

When I search the internet (and I have) I find very different solutions and many of them involving creating new roles from scratch.当我搜索互联网时(我有),我发现了非常不同的解决方案,其中许多涉及从头开始创建新角色。 However, I figure that I should be able to use the AWSServiceRoleForOrganizations role already created by AWS Organizations.但是,我认为我应该能够使用 AWS Organizations 已经创建的 AWSServiceRoleForOrganizations 角色。

Thank you谢谢

I figured it out.我想到了。 In the credentials file add:在凭证文件中添加:

[master] aws_access_key_id = xxxxxxxxxxxxxxxxxxxxx aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxx [主] aws_access_key_id = xxxxxxxxxxxxxxxxxxxxx aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxx

[sub-account] role_arn = arn:aws:iam::XXXXXXXXXXXX:role/OrganizationAccountAccessRole source_profile = master [子账户] role_arn = arn:aws:iam::XXXXXXXXXXXX:role/OrganizationAccountAccessRole source_profile = master

Where XXXXXXXXXXXX is the account number of the sub-account.其中 XXXXXXXXXXXX 为子账号的账号。

In your ~/.aws/config and ~/.aws/credentials file you need to add different profiles and credentials .在您的~/.aws/config~/.aws/credentials文件中,您需要添加不同的配置文件和凭据。

Place your keys in your ~/.aws/credentials file.将您的密钥放在您的~/.aws/credentials文件中。

[default]
aws_access_key_id=XXXXXXXXXXXXXXXX
aws_secret_access_key=XXXXXXXXXXXXXX/XXXXXX/XXXXXXXXX

[dev]
aws_access_key_id=XXXXXXXXXXXXXXXX
aws_secret_access_key=XXXXXXXXXXXXXX/XXXXXX/XXXXXXXXX

[playground]
aws_access_key_id=XXXXXXXXXXXXXXXX
aws_secret_access_key=XXXXXXXXXXXXXX/XXXXXX/XXXXXXXXX

[prod]
aws_access_key_id=XXXXXXXXXXXXXXXX
aws_secret_access_key=XXXXXXXXXXXXXX/XXXXXX/XXXXXXXXX

Modify your ~/.aws/config file.修改您的~/.aws/config文件。 Remember to Add the prefix 'profile'记得添加前缀'profile'

[default]
region=us-west-2

[profile dev]
region=us-east-1

[profile playground]
region=us-east-1

[profile prod]
region=us-east-1

Now you can switch between profile by using the --profile flag现在您可以使用--profile标志在配置文件之间切换

aws s3 ls --profile dev # will use keys and config from dev profile

aws s3 ls # will use keys and config from default profile

aws s3 ls --profile production # will use and config keys from prod profile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM