1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CloudformationとGitHubを連携させる

Last updated at Posted at 2025-08-17

はじめに

通常はS3にテンプレートファイルを設置するのだが、GitHubと連携できるようになったので、その機能を試す。

CloudFormationとGitHubを繋げる「接続」を作る

デベロッパー用ツール > 接続 > 接続を作成

image.png

接続名は任意です


image.png


image.png
ここはGitHubの画面です。ここにはアカウントやOrganizationが表示されているので、AWSと接続させるものをクリックします。GitHubにログインしてないと、間にログイン画面が挟まるかもしれない


image.png
この画面で接続するリポジトリを選択します。今回はAllにしました

CloudFormationとGitHubを繋げる「ポリシー」「ロール」を作る

CloudFormationの画面からロールを新規作成してくれる機能もあるのですが、そのままでは動かなかったです。

ポリシー

IAM > ポリシー

image.png

ポリシーの中身
{
    "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": "*"
        }
    ]
}
信頼ポリシー
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "CfnGitSyncTrustPolicy",
      "Effect": "Allow",
      "Principal": {
        "Service": "cloudformation.sync.codeconnections.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

ポリシー名は任意です。
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/git-sync-prereq.html#git-sync-prereq-iam
マニュアルに載ってます

ロール

IAM > ロール
image.png

ロール名は任意です。作ったポリシーをアタッチすればOK

スタック作成

CloudFormation > スタック > スタックの作成

image.png

image.png

image.png

image.png

image.png
image.png

image.png
IAMロールは作成したものを使いましょう。画像で書き忘れました
image.png

  • パラメータファイル
    名前は任意です。このファイルは必須です
deployment-parameters.yaml
template-file-path: handson-bucket-dev.yaml  # 注)これがcloudformationで実行するテンプレートです。ここで指定します
parameters:
  BucketName: handson-lab.click
  Environment: dev
  Project: aws-handson
  NameTag: handson-lab.click
tags: {}

挙動について(重要)

  • stack作成後、エラーが出ているときがあるが、嘘のときがあるので、画面を更新する
  • 初回のスタックはデプロイされます
  • リポジトリを更新するとデプロイされます。PRを作成し、そこで止まるケースがあります。原因がわからない。マージすればデプロイされるのでしょう
  • 最新のコミットを再試行をクリックしないと動かないときもある。
  • 画面がエラーのままのとき、古い情報を表示しているときがある。更新しよう。
  • タグだけ更新してもデプロイされません
  • デプロイされるかどうかはchangesetが作れるかどうかです
  • この画面が大事。
    image.png
  • 最新のコミットを再試行でchangesetがあればデプロイされると思われるが未確認
  • テンプレートタブやイベントタブを見ると動いているかどうかわかる。

感想

メリットをあまり感じず、手間が増えただけで、当面は使いたくないと思いました。

トラブルシューティング

No connection exists with the ARN

AWSとリポジトリをリンクさせた後、接続を再作成したら再リンクできなくなった(リンクされたままになっていて操作不能になるイメージ)
原因は以下の記事と同じかなと思われる

対処としては以下の通り。コマンドは少し変わっている。リンクが解除されるので、再度リンクさせればよい。エラーが解消しなければ、スタック名は変えて再実行してみよう

PS C:\Users\user> aws codestar-connections list-repository-links
{
    "RepositoryLinks": [
        {
            "ConnectionArn": "arn:aws:codeconnections:ap-northeast-1:XXXXXX:connection/f41c2ac0-4778-4782-b131-0a5bd87b9e37",
            "OwnerId": "aws-hands-on-training",
            "ProviderType": "GitHub",
            "RepositoryLinkArn": "arn:aws:codestar-connections:ap-northeast-1:XXXXXX:repository-link/f2fcfd6e-2221-4eef-b053-b72b738dde46",
            "RepositoryLinkId": "f2fcfd6e-2221-4eef-b053-b72b738dde46",
            "RepositoryName": "aws-static-website-hosting"
        }
    ]
}

PS C:\Users\user> aws codestar-connections delete-repository-link   --repository-link-id f2fcfd6e-2221-4eef-b053-b72b738dde46   --region ap-northeast-1

PS C:\Users\user> aws codestar-connections list-repository-links
{
    "RepositoryLinks": []
}

githubのアプリを削除するには

接続を作るときにインストールしたアプリを削除する
setting > GitHub Apps >

AWS Connector for GitHub > Configure
があるはず

image.png

image.png

接続をやり直すときはアンインストールする?

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?