

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 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/)

儲存庫可以是公有或私有。可以透過[連線主控台](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 是允許 CloudFormation 連線至 Git 儲存庫的 [CodeConnections](https://docs.aws.amazon.com/codeconnections/latest/APIReference/Welcome.html) 服務中的連線 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 授予最終使用者許可。

需要下列 `codeconnections` 許可才能建立和管理 Git 儲存庫的連線。
+ `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`

主控台使用者也必須具備下列 `cloudformation` 許可，才能在 Git 同步設定程序期間檢視和管理堆疊。
+ `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 主控台中看到警告，指出這些動作不存在。可以忽略此警告，且仍會成功建立政策。即使 IAM 主控台無法辨識，某些 Git 同步操作仍需要這些許可。