

# 使用 Git 同步使堆栈与 Git 存储库同步的先决条件
<a name="git-sync-prereq"></a>

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

**Topics**
+ [Git 存储库](#git-sync-prereq-repo)
+ [CloudFormation 模板](#git-sync-prereq-template)
+ [Git 同步服务角色](#git-sync-prereq-iam)
+ [控制台用户的 IAM 权限](#git-sync-prereq-user-permissions)

## Git 存储库
<a name="git-sync-prereq-repo"></a>

您必须将 Git 存储库托管在以下平台之一上。
+ [GitHub](https://github.com/)
+ [GitHub Enterprise](https://github.com/enterprise)
+ [GitLab](https://about.gitlab.com/)
+ [Bitbucket](https://bitbucket.org)
+ [GitLab 自托管](https://docs.gitlab.com/subscriptions/self_managed/)

存储库可以是公有的，也可以是私有的。您需要通过 [Connections 控制台](https://console.aws.amazon.com/codesuite/settings/connections)将 Git 存储库连接到 CloudFormation。

## CloudFormation 模板
<a name="git-sync-prereq-template"></a>

您的 Git 存储库必须包含 [CloudFormation 模板文件](git-sync-concepts-terms.md#git-sync-concepts-terms-template-file)，且该文件已签入您打算连接到 Git 同步的分支。[堆栈部署文件](git-sync-concepts-terms.md#git-sync-concepts-terms-depoyment-file)将引用此模板。

## Git 同步服务角色
<a name="git-sync-prereq-iam"></a>

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

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

### Git 同步服务角色的必要权限
<a name="git-sync-prereq-permissions"></a>

为 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 同步的必备权限。

------
#### [ JSON ]

****  

```
{
    "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": "*"
        }
    ]
}
```

------

#### 信任策略
<a name="git-sync-prereq-trust-policy"></a>

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

------
#### [ JSON ]

****  

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

------

建议您使用 `aws:SourceArn` 和 `aws:SourceAccount` 条件键来防止出现混淆代理人问题。源账户是您的账户 ID，源 ARN 是 [CodeConnections](https://docs.aws.amazon.com/codeconnections/latest/APIReference/Welcome.html) 服务中允许 CloudFormation 连接到您的 Git 存储库的连接的 ARN。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "CfnGitSyncTrustPolicy",
      "Effect": "Allow",
      "Principal": {
        "Service": "cloudformation.sync.codeconnections.amazonaws.com"
      },
      "Action": "sts:AssumeRole",
      "Condition": {
        "StringEquals": {
          "aws:SourceAccount": "123456789012"
        },
        "ArnLike": {
          "aws:SourceArn": "arn:aws:codeconnections:us-east-1:123456789012:connection/EXAMPLE64-8aad-4d5d-8878-dfcab0bc441f"
        }
      }
    }
  ]
}
```

------

有关混淆代理人问题的更多信息，请参阅 [防止跨服务混淆代理](cross-service-confused-deputy-prevention.md)。

## 控制台用户的 IAM 权限
<a name="git-sync-prereq-user-permissions"></a>

要通过 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:CreateChangeSet`、`cloudformation:DeleteChangeSet`、`cloudformation:DescribeChangeSet`、`cloudformation:ExecuteChangeSet`）可能不是硬性要求，但仍建议授予这些权限，以启用全栈检查和管理功能。

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

------
#### [ JSON ]

****  

```
{
    "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:CreateForcedTargetSync` 和 `codeconnections:CreatePullRequestForResource` 权限的 IAM 策略时，您可能会在 IAM 控制台中看到一条警告，指出这些操作不存在。可以忽略此警告，策略仍将成功创建。某些 Git 同步操作需要这些权限，尽管 IAM 控制台可能无法识别它们。