You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Boto3更新CloudFormation StackSet OU ID时遇验证错误求助

解决CloudFormation StackSet更新OU时的"OU not found"错误

核心问题分析

错误提示Organizational unit ou-rhew-abcdef not found in StackSet,本质是操作API与需求不匹配,或StackSet当前状态不支持直接更新到目标OU。

解决方案步骤

1. 用add_stack_instances替代update_stack_instances添加新OU

如果你的需求是将新OU纳入现有StackSet的部署范围(而非替换原有部署目标),应调用add_stack_instances API——update_stack_instances用于修改已有部署实例的配置(如区域、参数)或替换部署目标,新增部署目标需用add_stack_instances:

import boto3

cloudformation_client = boto3.client('cloudformation')

def lambda_handler(event, context):
    response = cloudformation_client.add_stack_instances(
        StackSetName='AWS-BACKUP-VAULT',
        DeploymentTargets={
            'OrganizationalUnitIds': [
                'ou-rhew-abcdef'
            ]
        },
        Regions=[
            'eu-west-1',
            'eu-central-1',
            'eu-west-2',
            'eu-west-3',
            'eu-north-1',
            'ap-southeast-1',
            'ap-south-1',
            'ap-northeast-2',
            'ap-southeast-2',
            'us-east-2',
            'us-east-1',
            'us-west-2'
        ]
    )

2. 确认OU ID的正确性

  • 登录AWS Organizations控制台,导航到CoreOU -> ConnectOU -> Prod,核对该OU的ID是否确实为ou-rhew-abcdef
  • 确保执行Lambda的IAM角色拥有organizations:ListOrganizationalUnitsForParent权限,能正常读取OU信息

3. 检查StackSet的部署目标类型

若StackSet最初基于单个账户而非OU部署,直接更新为OU会触发验证错误。此时需:

  1. 调用delete_stack_instances移除原有账户的Stack实例
  2. 再用add_stack_instances添加目标OU
  3. 或重新创建StackSet时指定OU作为部署目标

4. 验证IAM权限

确保执行操作的IAM角色(Lambda角色或本地用户)拥有以下权限:

  • cloudformation:AddStackInstances / cloudformation:UpdateStackInstances
  • organizations:ListAccountsForParent
  • organizations:ListOrganizationalUnitsForParent
  • iam:PassRole(若StackSet使用服务角色)

5. 检查StackSet的状态

调用describe_stack_set API确认当前StackSet的部署配置,排查是否存在锁定或异常状态:

response = cloudformation_client.describe_stack_set(StackSetName='AWS-BACKUP-VAULT')
print(response['StackSet']['DeploymentTargets'])

内容的提问来源于stack exchange,提问作者SUBHAS PATIL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 12:25:17