是否可以通过编程方式从帐户 ID 中检索 AWS 计费帐户 ID

Is it possible to retrieve the the AWS Billing Account Id from an Account Id programmatically

是否可以通过 AWSSDK 检索账户的账单账户 ID,如下面的屏幕截图所示?

我不确定这是否是正确的方法,但我能够通过 AWS Organization SDK 实现我需要的。

using var awsClient = new AmazonOrganizationsClient(assumeRoleAwsCredentials, RegionEndpoint.GetBySystemName(regionEndpointSystemName));

try
{
    var response = await awsClient.DescribeOrganizationAsync(new DescribeOrganizationRequest());
    return response.Organization.MasterAccountId;
}
catch (AWSOrganizationsNotInUseException)
{
    return accountId;
}

因为不在组织中的帐户会抛出 AWSOrganizationsNotInUseException,所以结算帐户 ID 必须是它自己。