

# 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 エンタープライズ](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 リポジトリには、Git 同期で接続する対象のブランチにチェックインされた [CloudFormation テンプレートファイル](git-sync-concepts-terms.md#git-sync-concepts-terms-template-file)が含まれている必要があります。このテンプレートは[スタックデプロイファイル](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"
    }
  ]
}
```

------

Confused Deputy Problem (混乱した使節の問題) から自分を守るために、`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 を通じてアクセス許可を付与する必要があります。

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 コンソールに表示されることがあります。この警告を無視しても、ポリシーは正常に作成されます。これらのアクセス許可は、IAM コンソールでは認識されないものの、特定の Git 同期オペレーションには必要です。