0
1

GitHub Actionsを使ってAWSにSSOログインする方法

Last updated at Posted at 2024-09-11

はじめに

今回は、GitHub Actionsを使用してAWSにSSOログインする設定手順を紹介します。
最近、SSOでAWSのログインを行うように切り替えました。切り替えたことにより、GitHub Actionsの設定を変更する必要が出てきたので、GitHub Actions内でSSOのログイン方法を紹介していきます。

Github Actionsを導入しており知見がある方を対象に書いているので、Gihub Actionsnの詳しい設定などは省きます。

手順

  1. Assume RoleするためのIAMロールの作成
  2. Github Actions用のymlファイルの設定

IAMロールの作成

  1. IAM コンソールから「ID プロバイダ」→「プロバイダを追加」を選択します。
  2. プロバイダのタイプに「OpenID Connect」を選択します。
  3. プロバイダの URL に https://token.actions.githubusercontent.com を入力します。
  4. 「サムプリントを取得」をクリックします。
  5. 対象者には sts.amazonaws.com を入力します。
  6. IAM コンソールから「ロール」→「ロールを作成」を選択します。
  7. 信頼されたエンティティタイプに「カスタム信頼ポリシー」を選択し、以下のカスタム信頼ポリシーを入力します。
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Principal": {
                "Federated": "arn:aws:iam::<accountID>:oidc-provider/token.actions.githubusercontent.com"
            },
            "Action": "sts:AssumeRoleWithWebIdentity",
            "Condition": {
                "StringEquals": {
                    "token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
                },
                "StringLike": {
                    "token.actions.githubusercontent.com:sub": "repo:<ユーザーの名前>/<リポジトリ名>:*"
                }
            }
        }
    ]
}

上記の権限でログインはできますがリソースの操作に関しては追加で権限をアタッチする必要があるので、使用する際は注意してください。

GitHub Actionsの設定

name: aws-login
#トリガーは自由に設定してください
on: [push]

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: <environment-name> # 使用する環境の名前を指定します
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-region: 'ap-northeast-1'
          role-to-assume: 'arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ secrets.ASSUME_ROLE_NAME }}'

環境変数の設定

  • GitHubに移動し、Github Actionsのenvironment-nameで設定した名前で環境を作成します。
    • Settings → Secrets and variables → Actions → Manage environment secrets → New environment
  • 環境(Environment)を設定します。
    • 環境に名前を付け、必要なシークレットを追加します。
      • AWS_ACCOUNT_ID(AWSのアカウントID)
      • ASSUME_ROLE_NAME (作成したIAMロールの名前)

まとめ

これにより、指定した環境のシークレットを参照してGitHub Actionsを使用してAWSにSSOログインできるようになります。
例えば、Github Actionsのファイル内で environment: dev を指定すると、dev 環境に設定されたシークレットが参照されます。

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