0
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?

GitHub Actions SSM ver.

0
Posted at

SSM agent verも残しておく
1 . EC2側準備:SSM Agentのインストール&起動

1-1. SSM Agent インストール確認

sudo yum install -y amazon-ssm-agent

1-2. SSM Agentの起動&自動起動設定

sudo systemctl enable amazon-ssm-agent
sudo systemctl start amazon-ssm-agent
sudo systemctl status amazon-ssm-agent

2 . EC2 IAM ロール設定

2-1. IAMロール作成
AWS コンソールの IAM > ロール から新規ロール作成
「EC2」を信頼できるエンティティとして選択
ポリシー検索で AmazonSSMManagedInstanceCore を選択しアタッチ
ロール名は例:EC2-SSM-Role とする

2-2. 作成したロールをEC2にアタッチ
EC2 > 対象インスタンス > 「インスタンス設定」> 「IAMロールの変更」からアタッチ
※ここでインスタンスIDをコピーしておく

3 . ネットワーク設定確認
EC2インスタンスのセキュリティグループ、NACLは アウトバウンド TCP 443 (HTTPS) が開放されていること
SSMは AWSのエンドポイントとHTTPS通信でやりとりするため、インバウンドの22番ポートは不要(SSHは開けなくてよい)

4 . GitHub Actions用IAMロール設定(SSM操作権限)
4-1. IAMロール作成
IAMで新規ロール作成

「信頼できるエンティティの種類」→「Web ID プロバイダー」

「プロバイダー」には GitHubのOIDC プロバイダー token.actions.githubusercontent.com を選択または作成

信頼ポリシー例:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "arn:aws:iam::<AWSアカウントID>:oidc-provider/token.actions.githubusercontent.com"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
          "token.actions.githubusercontent.com:sub": "repo:<GitHubユーザー名>/<リポジトリ名>:ref:refs/heads/main"
        }
      }
    }
  ]
}

4-2. ポリシーアタッチ(権限)
以下のようなカスタムポリシーを作成し、ロールにアタッチしてください。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ssm:SendCommand"
      ],
      "Resource": [
        "arn:aws:ec2:ap-northeast-1:<AWSアカウントID>:instance/<対応インスタンスID>",
        "arn:aws:ssm:ap-northeast-1::document/AWS-RunShellScript"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "ssm:ListCommandInvocations",
        "ssm:GetCommandInvocation",
        "ssm:ListCommands"
      ],
      "Resource": "*"
    }
  ]
}

4-3. ロールARNをメモ
例)arn:aws:iam:::role/GitHub-Actions-SSM-Role

5 . GitHub リポジトリ設定
5-1. Secretsに値を登録

名前 備考
EC2_INSTANCE_ID i-0xxxxxxxxxxxxxxxxx 対象EC2のインスタンスID
AWS_ROLE_ARN ロールARN(上記4-3参照) GitHub ActionsでAssumeするロール

6 . GitHub Actions ワークフロー例

name: Deploy via SSM

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read

    steps:
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-region: ap-northeast-1
          role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
          role-session-name: github-actions-deploy

      - name: Deploy on EC2 via SSM
        run: |
          aws ssm send-command \
            --instance-ids ${{ secrets.EC2_INSTANCE_ID }} \
            --document-name "AWS-RunShellScript" \
            --comment "Deploy via GitHub Actions" \
            --parameters 'commands=["cd <展開予定のdirを書く> && git checkout . && git clean -fd && git pull origin main"]'

7 . 動作確認&デバッグポイント
IAMロールの信頼関係が正しいか(GitHub OIDCの設定)
GitHub Secretsの値が間違っていないか
EC2インスタンスに正しいIAMロールが付与されているか
SSM Agentが起動しているか(sudo systemctl status amazon-ssm-agent)
ネットワークのアウトバウンドHTTPS443が通っているか

こんな感じかな?

0
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
0
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?