请参阅IAM策略中的AWS账号

时间:2018-05-05 17:41:05

标签: amazon-web-services amazon-iam

为了自动化,我希望我的IAM策略是通用的。

我知道${aws:username}提取了应用它的策略的用户名。

是否可以在IAM策略中使用arn:aws:iam::1234567890:user/${aws:username}之类的内容对AWS账号arn:aws:iam::${aws:accountnumber}:user/${aws:username}执行相同的操作。

编辑: 允许的变量列在下面链接的文档中。 https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_variables.html

2 个答案:

答案 0 :(得分:2)

AWS IAM策略变量不允许引用AWS账户名称。因此,除了在IAM策略中动态引用帐号之外别无选择。

答案 1 :(得分:0)

获取我找到的帐号的最好方法是通过STS。这是我用来从AssumeRole获取与凭据一起使用的帐号的Java代码,

String awsRegion = Regions.US_EAST_1;
AWSCredentials cred = new BasicAWSCredentials("access_key_id", "secret_key_id");
// AWSCredentials cred = BasicSessionCredentials(sessionCredentials.getAccessKeyId(),sessionCredentials.getSecretAccessKey(), sessionCredentials.getSessionToken());

AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard()
        .withCredentials(new AWSStaticCredentialsProvider(cred))
        .withRegion(awsRegion)
        .build();
String awsAccountNumber = stsClient.getCallerIdentity(new GetCallerIdentityRequest())
        .getAccount();
相关问题