资讯详情

资讯详情

Terraform AWS Provider 数据源详解:aws_organizations_organizational_unit_child_accounts

Terraform AWS Provider 数据源详解aws_organizations_organizational_unit_child_accounts【免费下载链接】terraform-provider-awsThe AWS Provider enables Terraform to manage AWS resources.项目地址: https://gitcode.com/GitHub_Trending/te/terraform-provider-awsaws_organizations_organizational_unit_child_accounts是 terraform-provider-awsThe AWS Provider中 Organizations 服务下的一个只读数据源用于获取指定父级组织单元Organizational UnitOU下的全部直接子账户列表。本文基于 官方数据源文档 展开并结合仓库内 数据源实现、测试用例 等源码深入讲解其参数、导出属性、底层 AWS API 调用链以及与“后代账户”数据源的本质区别。读完本文你将能够独立编写配置在 Terraform 中按 OU 边界批量枚举、筛选和引用账户信息。数据源定位仅返回“直接子账户”AWS Organizations 中的组织结构是树形的根Root下有组织单元OUOU 下还可以嵌套 OU账户则挂在某个 OU 或根下。该数据源解决的核心问题是给定一个父节点 ID可以是根也可以是任意 OU列出其直接挂载的子账户。关键语义是“This only provides immediate children, not all children”仅提供直接子级而非全部子级它只返回挂在parent_id这个节点正下方的账户不会递归下钻到该 OU 的子 OU 中去汇总账户。如果需要连同所有嵌套子 OU 下的账户一起返回应使用同服务下的 aws_organizations_organizational_unit_descendant_accounts 数据源其源码中通过findAllAccountsForParentAndBelow递归遍历 OU 树见 organizational_unit_descendant_accounts_data_source.go。快速上手示例下面是最常见的用法先通过aws_organizations_organization数据源读取当前组织的根节点 ID再以该根节点作为父节点列出组织内所有直接子账户。data aws_organizations_organization org {} data aws_organizations_organizational_unit_child_accounts accounts { parent_id data.aws_organizations_organization.org.roots[0].id } # 使用示例输出每个直接子账户的 ID 与名称 output child_account_ids { value [for a in data.aws_organizations_organizational_unit_child_accounts.accounts.accounts : a.id] } output child_account_names { value [for a in data.aws_organizations_organizational_unit_child_accounts.accounts.accounts : a.name] }data.aws_organizations_organization.org.roots[0].id取自 organization_data_source.go 中导出的roots列表每个 root 元素包含id、name、arn与policy_types。如果要从某个具体 OU下取账户parent_id直接传该 OU 的 ID 即可。OU 的 ID 可以通过 aws_organizations_organizational_unit 数据源、aws_organizations_organizational_units 数据源列出直接子 OU见 organizational_units_data_source.go或aws_organizations_organizational_unit资源引用获得。多级 OU 下的组合查询示例当 OU 存在嵌套时可以组合使用organizational_units数据源与organizational_unit_child_accounts数据源对某个父节点下的每个子 OU 分别取账户data aws_organizations_organization org {} # 列出根节点下的所有直接子 OU data aws_organizations_organizational_units top_ous { parent_id data.aws_organizations_organization.org.roots[0].id } # 为每个直接子 OU 取该 OU 下的直接子账户 data aws_organizations_organizational_unit_child_accounts per_ou { for_each { for ou in data.aws_organizations_organizational_units.top_ous.children : ou.id ou.name } parent_id each.key } output accounts_by_ou { value { for ou_id, ds in data.aws_organizations_organizational_unit_child_accounts.per_ou : ou_id ds.accounts } }Argument Reference参数说明该数据源仅支持一个参数参数类型必填说明parent_idstringRequired必填账户所属父节点的 ID。该父节点可以是组织的根Root也可以是任意层级的组织单元OU。数据源将返回挂在此父节点下的直接子账户。从源码看parent_id在 organizational_unit_child_accounts_data_source.go 中被声明为Required: true的字符串读取阶段通过d.Get(parent_id).(string)取出该值并原样作为ListAccountsForParentAPI 请求中的ParentId字段。Attribute Reference导出属性详解除参数本身外该数据源还导出以下属性属性类型说明idstring父节点标识符即parent_id本身。源码中读取完成后执行d.SetId(parentID)见 organizational_unit_child_accounts_data_source.go因此数据源的 ID 就是父节点 ID可在资源间作为引用键使用。accountslist直接子账户列表每个元素包含以下子属性。accounts 列表元素的子属性子属性说明arn该 AWS 账户的 ARNAmazon Resource Name。email与该 AWS 账户关联的邮箱地址。id该账户的唯一标识符12 位数字账户 ID。joined_method该账户加入组织的方式例如INVITED受邀加入 /CREATED由组织创建。joined_timestamp该账户成为组织成员的时间。name该账户的友好名称。state该账户在组织中的状态如ACTIVE、SUSPENDED、PENDING_CLOSURE等。status已弃用Deprecated请改用state。以上属性在源码的 Schema 声明中一一对应organizational_unit_child_accounts_data_source.goaccounts声明为TypeListComputed元素为嵌套schema.Resource其中status字段明确标注了Deprecated: status is deprecated. Use state instead.与文档中的弃用说明一致新配置应统一使用state数据源读取时先调用flattenAccounts把 AWS SDK 返回的[]awstypes.Account展开为 Terraform 可用的 map 列表再通过d.Set(accounts, flattenAccounts(accounts))写入状态organizational_unit_child_accounts_data_source.go。flattenAccounts的具体实现位于 organization.go它逐一将 SDK 账户对象映射为arn、email、id、joined_method、joined_timestamp按time.RFC3339格式化为字符串、name、status、state字段。当结果为空时返回nil此时accounts为空列表。子属性在配置中的实际形态data aws_organizations_organizational_unit_child_accounts accounts { parent_id data.aws_organizations_organization.org.roots[0].id } # 提取所有账户 ID 集合可用于后续资源的 for_each 或 IAM 策略生成 locals { child_account_ids toset([for a in data.aws_organizations_organizational_unit_child_accounts.accounts.accounts : a.id]) }底层实现原理从 Terraform 到 AWS API 的完整调用链该数据源在仓库中的实现文件为 organizational_unit_child_accounts_data_source.go注册入口在 service_package_gen.goSDKDataSource注解生成Factory: dataSourceOrganizationalUnitChildAccounts。整体流程如下读取阶段dataSourceOrganizationalUnitChildAccountsRead首先从 provider 上下文取得 Organizations 客户端meta.(*conns.AWSClient).OrganizationsClient(ctx)然后取出parent_id。构造请求findAccountsForParentByID构造organizations.ListAccountsForParentInput{ParentId: aws.String(id)}organizational_unit_child_accounts_data_source.go对应 AWS 的ListAccountsForParentAPI——这正是“只取直接子账户”的语义来源该 API 返回的是指定父节点下直接挂载的账户。分页聚合findAccountsForParent使用 AWS SDK 的分页器organizations.NewListAccountsForParentPaginator循环拉取所有页将每页的page.Accounts追加到结果中organizational_unit_child_accounts_data_source.go。这意味着即使父节点下有超过单页上限500 个的账户数据源也能完整返回。写回状态设置id parent_id并将flattenAccounts(accounts)的结果写入accounts属性。错误处理如果 API 调用失败会通过sdkdiag.AppendErrorf返回形如listing Organizations Accounts for parent (%s): %s的诊断信息便于定位问题。值得一提的设计细节数据源采用ReadWithoutTimeout无超时的读操作而非ReadContext这是因为读取是纯查询、不涉及资源变更交给 Terraform 的默认超时机制即可同时 Schema 使用SchemaFunc延迟构建是当前仓库中新数据源的标准写法源码文件头部注释也提示新资源应使用 skaff 脚手架生成而不是复制旧资源模式。与 descendant accounts 数据源的区别immediate vs. all该数据源最常见的易混淆点是child_accounts直接子账户与descendant_accounts后代账户的区别两者在仓库中有清晰的对照实现child_accounts本文只调用一次ListAccountsForParent返回父节点正下方的账户。对于嵌套 OU子 OU 中的账户不会被包含。descendant_accounts实现于 organizational_unit_descendant_accounts_data_source.go。其核心函数findAllAccountsForParentAndBelow先取父节点下的账户再通过findOrganizationalUnitsForParentByID找出父节点下的所有子 OU然后递归地对每个子 OU 重复上述过程最终汇总整个子树上的全部账户organizational_unit_descendant_accounts_data_source.go。场景使用 child_accounts使用 descendant_accounts组织只有根 账户无嵌套 OU✅ 结果一致✅ 结果一致组织存在嵌套 OU只想统计某 OU 直属账户✅❌ 会多出嵌套子 OU 的账户想统计某 OU 子树下全部账户含嵌套❌ 会漏掉子 OU 中的账户✅文档中aws_organizations_organizational_units数据源文档也遵循同样的“immediate children”语义用于列出直接子 OU可作为构建递归遍历的配套工具。权限与前提条件使用该数据源前当前凭证所属账户必须已是某组织的管理账户Management Account或具有读取权限的委派管理员Delegated Administrator否则ListAccountsForParent会返回AccessDeniedException。测试用例中的 PreCheck 也印证了这一点acctest.PreCheckOrganizationManagementAccount(ctx, t)明确要求测试环境必须是组织的管理账户见 organizational_unit_child_accounts_data_source_test.go。该 PreCheck 位于 internal/acctest/acctest.go 中。所需 IAM 权限通常为organizations:ListAccountsForParent与organizations:ListAccounts实际最小权限请以组织治理策略为准。测试用例与验证方式仓库为数据源提供了基础验收测试organizational_unit_child_accounts_data_source_test.gofunc testAccOrganizationalUnitChildAccountsDataSource_basic(t *testing.T) { ctx : acctest.Context(t) dataSourceName : data.aws_organizations_organizational_unit_child_accounts.test acctest.Test(ctx, t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) acctest.PreCheckOrganizationManagementAccount(ctx, t) }, ErrorCheck: acctest.ErrorCheck(t, names.OrganizationsServiceID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ { Config: testAccOrganizationalUnitChildAccountsDataSourceConfig_basic, Check: resource.ComposeTestCheckFunc( acctest.CheckResourceAttrGreaterThanValue(dataSourceName, accounts.#, 0), ), }, }, }) }对应的 Terraform 配置片段与文档示例一致以根节点为父节点校验accounts.#大于 0data aws_organizations_organization current {} data aws_organizations_organizational_unit_child_accounts test { parent_id data.aws_organizations_organization.current.roots[0].id }这说明该数据源可通过make testacc系列命令在真实 AWS 组织环境中运行验收测试参见仓库 running-and-writing-acceptance-tests.md测试前提是环境变量配置了具备 Organizations 管理权限的凭证。实战提示与限制“直接子级”语义当组织结构存在多层嵌套时务必根据需求选择 child仅直属或 descendant含子树全部数据源避免统计口径错误。id即父节点 ID数据源的id等于parent_id可用于for_each、count或作为其他资源的依赖键构建按 OU 分组的账户清单。status已弃用新代码请使用state判断账户状态state可能的取值如ACTIVE、SUSPENDED、PENDING_CLOSURE等具体以 AWS Organizations API 返回为准。joined_method与joined_timestamp可用于审计账户来源受邀加入还是组织创建与加入时间结合email、name可生成组织账户台账。分页自动处理底层分页器保证即使账户数量很大也能完整返回无需在配置中做额外处理。通过本文的文档解读与源码印证你可以放心地在 Terraform 配置中使用aws_organizations_organizational_unit_child_accounts数据源按 OU 边界精确枚举直接子账户并进一步结合descendant_accounts、organizational_units等数据源构建完整的组织账户与 OU 树视图。【免费下载链接】terraform-provider-awsThe AWS Provider enables Terraform to manage AWS resources.项目地址: https://gitcode.com/GitHub_Trending/te/terraform-provider-aws创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
觉得有用,分享给同行:

为您的企业打造数字门面

稳重轻奢商务风格,端正雅致视觉,长效耐看不易过时。

立即咨询 →