diff --git a/aws/data_source_aws_organizations_account_ids.go b/aws/data_source_aws_organizations_account_ids.go new file mode 100644 index 00000000000..90cf1142c0f --- /dev/null +++ b/aws/data_source_aws_organizations_account_ids.go @@ -0,0 +1,65 @@ +package aws + +import ( + "time" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/organizations" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceAwsOrganizationsAccountIds() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsOrganizationAccountIdsRead, + + Schema: map[string]*schema.Schema{ + "parent_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "ids": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + } +} + +func dataSourceAwsOrganizationAccountIdsRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).organizationsconn + + accountIds := make([]string, 0) + + if parentId, ok := d.GetOk("parent_id"); ok { + input := &organizations.ListAccountsForParentInput{ + ParentId: aws.String(parentId.(string)), + } + result, err := conn.ListAccountsForParent(input) + + if err != nil { + return err + } + + for _, account := range result.Accounts { + accountIds = append(accountIds, *account.Id) + } + } else { + input := &organizations.ListAccountsInput{} + result, err := conn.ListAccounts(input) + + if err != nil { + return err + } + + for _, account := range result.Accounts { + accountIds = append(accountIds, *account.Id) + } + } + + d.SetId(time.Now().UTC().String()) + d.Set("ids", accountIds) + + return nil +} diff --git a/aws/provider.go b/aws/provider.go index 1891e6418ba..e76951f8b36 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -219,6 +219,7 @@ func Provider() terraform.ResourceProvider { "aws_mq_broker": dataSourceAwsMqBroker(), "aws_nat_gateway": dataSourceAwsNatGateway(), "aws_network_interface": dataSourceAwsNetworkInterface(), + "aws_organizations_account_ids": dataSourceAwsOrganizationsAccountIds(), "aws_partition": dataSourceAwsPartition(), "aws_prefix_list": dataSourceAwsPrefixList(), "aws_rds_cluster": dataSourceAwsRdsCluster(), diff --git a/website/aws.erb b/website/aws.erb index 616605551f5..8b680a31d18 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -220,6 +220,9 @@ > aws_mq_broker + > + aws_organizations_account_ids + > aws_partition diff --git a/website/docs/d/organizations_account_ids.html.markdown b/website/docs/d/organizations_account_ids.html.markdown new file mode 100644 index 00000000000..023c148277b --- /dev/null +++ b/website/docs/d/organizations_account_ids.html.markdown @@ -0,0 +1,29 @@ +--- +layout: "aws" +page_title: "AWS: aws_organizations_account_ids" +sidebar_current: "docs-aws-datasource-organizations-account-ids" +description: |- + Provides a list of Account IDs in an Organization or Organizational Unit.Use this data source to get a list of Account IDs in an Organization or Organizational Unit +--- + +# Data Source: aws_organizations_account_ids + +`aws_organizations_account_ids` provides a list of AccountIds in an [organization](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_org.html) or [organizational unit](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_ous.html). + +Will give an error if Organizations aren't enabled - see `aws_organizations_organization`. + +## Example Usage + +```hcl +data "aws_organizations_account_ids" "master" {} +``` + +## Argument Reference + +* `parent_id` - (Optional) The ID for the [parent root](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_getting-started_concepts.html#root) or [organizational unit](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_getting-started_concepts.html#organizationalunit) whose accounts you want to list. If you specify the root you get the list of all the accounts that are not in any organizational unit. If you specify an organizational unit, you get a list of all the accounts in only that organizational unit, and not any child organizational units. + +## Attributes Reference + +The following attributes are exported: + +* `ids` - is set to a list Account IDs.