使用 Git 同步使堆栈与 Git 存储库同步的先决条件 - AWS CloudFormation

使用 Git 同步使堆栈与 Git 存储库同步的先决条件

在使 CloudFormation 堆栈与 Git 存储库同步之前,确认满足以下要求。

Git 存储库

您必须将 Git 存储库托管在以下平台之一上。

存储库可以是公有的,也可以是私有的。您需要通过 Connections 控制台将 Git 存储库连接到 CloudFormation。

CloudFormation 模板

您的 Git 存储库必须包含 CloudFormation 模板文件,且该文件已签入您打算连接到 Git 同步的分支。堆栈部署文件将引用此模板。

Git 同步服务角色

Git 同步需要 IAM 角色。您可以选择在配置 Git 同步时为堆栈创建 IAM 角色,也可以使用现有角色。

注意

自动生成的 IAM 角色仅将权限应用于为其生成角色的堆栈。要重复使用自动生成的 IAM 角色,必须编辑新堆栈的角色。

Git 同步服务角色的必要权限

为 Git 同步提供的 IAM 角色需要以下权限。

  • cloudformation:CreateChangeSet

  • cloudformation:DeleteChangeSet

  • cloudformation:DescribeChangeSet

  • cloudformation:DescribeStackEvents

  • cloudformation:DescribeStacks

  • cloudformation:ExecuteChangeSet

  • cloudformation:ListChangeSets

  • cloudformation:ValidateTemplate

  • events:PutRule

  • events:PutTargets

注意

上述所需权限会自动添加到 Git 同步生成的 IAM 角色中。

以下示例 IAM 角色包括 Git 同步的必备权限。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "SyncToCloudFormation", "Effect": "Allow", "Action": [ "cloudformation:CreateChangeSet", "cloudformation:DeleteChangeSet", "cloudformation:DescribeChangeSet", "cloudformation:DescribeStackEvents", "cloudformation:DescribeStacks", "cloudformation:ExecuteChangeSet", "cloudformation:GetTemplate", "cloudformation:ListChangeSets", "cloudformation:ListStacks", "cloudformation:ValidateTemplate" ], "Resource": "*" }, { "Sid": "PolicyForManagedRules", "Effect": "Allow", "Action": [ "events:PutRule", "events:PutTargets" ], "Resource": "*", "Condition": { "StringEquals": { "events:ManagedBy": ["cloudformation.sync.codeconnections.amazonaws.com"] } } }, { "Sid": "PolicyForDescribingRule", "Effect": "Allow", "Action": "events:DescribeRule", "Resource": "*" } ] }

信任策略

在创建角色以定义信任关系时,必须提供以下信任策略:

JSON
{ "Version": "2012-10-17", "Statement": [ { "Sid": "CfnGitSyncTrustPolicy", "Effect": "Allow", "Principal": { "Service": "cloudformation.sync.codeconnections.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }

控制台用户的 IAM 权限

要通过 CloudFormation 控制台成功设置 Git 同步,还必须通过 IAM 为最终用户授予权限。

创建和管理与 Git 存储库的连接需要以下 codeconnections 权限。

  • codeconnections:CreateRepositoryLink

  • codeconnections:CreateSyncConfiguration

  • codeconnections:DeleteRepositoryLink

  • codeconnections:DeleteSyncConfiguration

  • codeconnections:GetRepositoryLink

  • codeconnections:GetSyncConfiguration

  • codeconnections:ListRepositoryLinks

  • codeconnections:ListSyncConfigurations

  • codeconnections:ListTagsForResource

  • codeconnections:TagResource

  • codeconnections:UntagResource

  • codeconnections:UpdateRepositoryLink

  • codeconnections:UpdateSyncBlocker

  • codeconnections:UpdateSyncConfiguration

  • codeconnections:UseConnection

在 Git 同步设置过程中,控制台用户还必须拥有以下 cloudformation 权限才能查看和管理堆栈。

  • cloudformation:CreateChangeSet

  • cloudformation:DeleteChangeSet

  • cloudformation:DescribeChangeSet

  • cloudformation:DescribeStackEvents

  • cloudformation:DescribeStacks

  • cloudformation:ExecuteChangeSet

  • cloudformation:GetTemplate

  • cloudformation:ListChangeSets

  • cloudformation:ListStacks

  • cloudformation:ValidateTemplate

注意

尽管对于仅控制台使用场景,变更集权限(cloudformation:CreateChangeSetcloudformation:DeleteChangeSetcloudformation:DescribeChangeSetcloudformation:ExecuteChangeSet)可能不是硬性要求,但仍建议授予这些权限,以启用全栈检查和管理功能。

以下示例 IAM 策略包含通过控制台设置 Git 同步所需的用户权限。

{ "Version": "2012-10-17", "Statement": [ { "Sid": "CodeConnectionsPermissions", "Effect": "Allow", "Action": [ "codeconnections:CreateRepositoryLink", "codeconnections:CreateSyncConfiguration", "codeconnections:DeleteRepositoryLink", "codeconnections:DeleteSyncConfiguration", "codeconnections:GetRepositoryLink", "codeconnections:GetSyncConfiguration", "codeconnections:ListRepositoryLinks", "codeconnections:ListSyncConfigurations", "codeconnections:ListTagsForResource", "codeconnections:TagResource", "codeconnections:UntagResource", "codeconnections:UpdateRepositoryLink", "codeconnections:UpdateSyncBlocker", "codeconnections:UpdateSyncConfiguration", "codeconnections:UseConnection", "codeconnections:CreateForcedTargetSync", "codeconnections:CreatePullRequestForResource" ], "Resource": "*" }, { "Sid": "CloudFormationConsolePermissions", "Effect": "Allow", "Action": [ "cloudformation:CreateChangeSet", "cloudformation:DeleteChangeSet", "cloudformation:DescribeChangeSet", "cloudformation:DescribeStackEvents", "cloudformation:DescribeStacks", "cloudformation:ExecuteChangeSet", "cloudformation:GetTemplate", "cloudformation:ListChangeSets", "cloudformation:ListStacks", "cloudformation:ValidateTemplate" ], "Resource": "*" } ] }
注意

创建包含 codeconnections:CreateForcedTargetSynccodeconnections:CreatePullRequestForResource 权限的 IAM 策略时,您可能会在 IAM 控制台中看到一条警告,指出这些操作不存在。可以忽略此警告,策略仍将成功创建。某些 Git 同步操作需要这些权限,尽管 IAM 控制台可能无法识别它们。