如何使用 CodePipeline 在委派管理员账户中部署服务托管的 CloudFormation StackSet?
我想使用 AWS CodePipeline 将 AWS CloudFormation StackSet 部署到 AWS Organizations 中的目标组织单元 (OU)。我想使用服务托管权限在委派管理员账户中部署堆栈。
解决方案
**注意:**如果您在运行 AWS 命令行界面 (AWS CLI) 命令时收到错误,请参阅 AWS CLI 错误故障排除。此外,请确保您使用的是最新版本的 AWS CLI。
先决条件:激活 AWS CloudFormation StackSets 与 Organizations 之间的可信访问。有关权限,请参阅启用可信访问所需的权限。要激活可信访问,您必须是管理账户中的管理员用户。
注册委派管理员账户
要将成员账户委派为管理员账户,请运行以下 register-delegated-administrator AWS CLI 命令:
aws organizations register-delegated-administrator \ --service-principal=member.org.stacksets.cloudformation.amazonaws.com \ --account-id="111122223333"
**注意:**请将 111122223333 替换为您的账户 ID。
要验证注册,请运行以下 list-delegated-administrators CLI 命令:
aws organizations list-delegated-administrators \ --service-principal=member.org.stacksets.cloudformation.amazonaws.com
设置管线
要创建允许 CloudFormation 操作的管线,您可以使用 Codepipeline 控制台或 AWS CLI。
使用控制台
要使用 CodePipeline 控制台创建管线,请参阅“使用静态模板创建管线”。在 Configure template(配置模板)中,添加以下 cp-template.yaml 模板:
AWSTemplateFormatVersion: "2010-09-09" Parameters: TargetRegions: Type: String Resources: Bucket: Type: "AWS::S3::Bucket" DeletionPolicy: Retain Properties: VersioningConfiguration: Status: Enabled ServiceRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Principal: Service: - codepipeline.amazonaws.com Action: - sts:AssumeRole Policies: - PolicyName: CodePipelinePermissions PolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Action: - s3:GetObject - s3:GetObjectVersion - s3:GetBucketVersioning - s3:PutObject - s3:PutObjectAcl Resource: - !Sub arn:aws:s3:::${Bucket} - !Sub arn:aws:s3:::${Bucket}/* - Effect: Allow Action: - cloudformation:CreateStackSet - cloudformation:UpdateStackSet - cloudformation:DeleteStackSet - cloudformation:DescribeStackSet - cloudformation:DescribeStackSetOperation - cloudformation:ListStackInstances - cloudformation:CreateStackInstances Resource: "*" - Effect: Allow Action: - organizations:ListDelegatedAdministrators Resource: "*" Pipeline: Type: AWS::CodePipeline::Pipeline Properties: RoleArn: !GetAtt ServiceRole.Arn ArtifactStore: Type: S3 Location: !Ref Bucket Stages: - Name: Source Actions: - Name: SourceAction ActionTypeId: Category: Source Owner: AWS Provider: S3 Version: "1" Configuration: S3Bucket: !Ref Bucket S3ObjectKey: "source.zip" PollForSourceChanges: false OutputArtifacts: - Name: SourceArtifact - Name: Deploy Actions: - Name: ServiceManaged-SS RunOrder: "1" ActionTypeId: Category: Deploy Owner: AWS Provider: CloudFormationStackSet Version: "1" Configuration: PermissionModel: SERVICE_MANAGED CallAs: DELEGATED_ADMIN OrganizationsAutoDeployment: Enabled Regions: !Ref TargetRegions StackSetName: !Sub codepipeline-deployed-cfn-${AWS::AccountId}-stackset MaxConcurrentPercentage: "50" FailureTolerancePercentage: "100" ConcurrencyMode: SOFT_FAILURE_TOLERANCE Capabilities: CAPABILITY_NAMED_IAM,CAPABILITY_IAM,CAPABILITY_AUTO_EXPAND DeploymentTargets: "SourceArtifact::ou.txt" TemplatePath: "SourceArtifact::template.yaml" Parameters: "SourceArtifact::parameters.txt" InputArtifacts: - Name: SourceArtifact
**注意:**您为 CallAs 指定的 AWS Identity and Access Management (IAM) 服务角色必须拥有 organizations:ListDelegatedAdministrators 权限。
使用 AWS CLI
要使用 AWS CLI 创建管线,请参阅创建管线 (CLI)。
(可选)创建堆栈
如果您不使用模板来创建管线,则必须在 CloudFormation 中为您的管线创建一个堆栈。
使用控制台
要使用 CloudFormation 控制台创建堆栈,请参阅“创建堆栈”。在 Parameters(参数)部分中,添加 StackSet 实例部署到的 AWS 区域。例如,在 TargetRegions 中包含 aa-example-1、aa-example-2。确保您为 CallAs 指定的 IAM 服务角色拥有 organizations:ListDelegatedAdministrators 权限。
**注意:**创建堆栈后,管线预计会失败,因为您未上传源 .zip 文件。您需要在准备好源文件之后上传该 .zip 文件。
使用 AWS CLI
使用与 cp-template.yaml 相同的文件路径,然后运行以下 create-stack AWS CLI 命令:
aws cloudformation create-stack \ --stack-name stack-name \ --capabilities CAPABILITY_NAMED_IAM \ --template-body file://cp-template.yaml \ --parameters ParameterKey=TargetRegions,ParameterValue=aa-example-1\\,aa-example-2
**注意:**请将 stack-name 替换为您的堆栈的名称,将 //./cp-template.yaml 替换为您的文件路径,将 aa-example-1 和 aa-example-2 替换为您的区域。
将源文件上传到您的 Amazon S3 存储桶
准备源文件
要定义部署目标,请创建一个 .txt 文件并将其命名为 ou.txt。将以下代码添加到该 .txt 文件中:
["ou-xrop-xxxxxxxx","ou-xrop-yyyyyyyy"]
要定义 CloudFormation 模板参数,请创建一个 .txt 文件并将其命名为 parameters.txt。将以下代码添加到该 .txt 文件中:
[ { "ParameterKey": "VersioningStatus", "ParameterValue": "Enabled" } ]
要创建 StackSet 模板,请创建一个 .txt 文件并将其命名为 template.yaml。将以下代码添加到该 .txt 文件中:
AWSTemplateFormatVersion: 2010-09-09 Parameters: VersioningStatus: Type: String Default: Suspended AllowedValues: - Enabled - Suspended Resources: S3Bucket: Type: 'AWS::S3::Bucket' DeletionPolicy: Delete Properties: VersioningConfiguration: Status: !Ref VersioningStatus
部署源文件
将这三个文件压缩为 source.zip,然后将该 .zip 文件上传到源 Amazon Simple Storage Service (Amazon S3) 存储桶的根目录。如果管线失败,且您收到"Account used is not a delegated administrator"错误,请确认您是否已将该账户注册为委派管理员。
相关信息
AWS CloudFormation StackSets 部署操作参考
如何解决运行 ListStackSets 操作时出现的"Account used is not a delegated administrator"错误?
- 语言
- 中文 (简体)

相关内容
AWS 官方已更新 10 个月前