📚

github actionとawsの連携方法

2023/02/13に公開

従来の連携方法

従来awsとgithub actionを連携させるにはcredencial情報をgithub actionのsecretに保存し、接続する方法が使われていた。
しかし、credencialの流出や悪用の問題があり、昨今ではあまり使われない方針に舵を切られてきている。
最近ではIDプロバイダーを利用し、連携する形に変わってきているのその方法を今回は説明して行こうと思います。

IDプロバイダーの作成

今回のIDプロバイダーのの設定値は以下で行う
type: OpenID connect
プロバイダーurl: https://token.actions.githubusercontent.com
audience: sts.amazonaws.com

1, IDプロバイダー設定

2, IAMロールの作成
今回は少し権限を広めに設定しています。
まず

  • 信頼エンティティーをウェブアイデンティティにし、プロバイダーとaudienceを1で作成したものを選択する。

  • あとはポリシーを付与する

  • 信頼ポリシーを編集する

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Federated": "arn:aws:iam::572919087216:oidc-provider/token.actions.githubusercontent.com"
            },
            "Action": "sts:AssumeRoleWithWebIdentity",
            "Condition": {
                "StringLike": {
                    "token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
                    "token.actions.githubusercontent.com:sub": [
                        "repo:組織名/リポジトリー名:*"
                    ]
                }
            }
        }
    ]
}

3、workflow作成

name: "infra preview & deploy"

on:
  push:
    branches:
      - main
    paths:
      - infra/**
      - .github/workflows/**
  workflow_dispatch:

permissions:
  id-token: write
  contents: read

jobs:
  preview:
    name: Preview
    runs-on: ubuntu-latest
    steps:
      - run: |
          echo "Hello World"

terraformのplanをgithub action上で行う

ここからはcloud9で実施していきます。

checkout, terraformのセットアップする

使い方: https://github.com/marketplace/actions/checkout

https://github.com/marketplace/actions/hashicorp-setup-terraform

name: "infra preview & deploy"

on:
  push:
    branches:
      - main
    paths:
      - infra/**
      - .github/workflows/**
  workflow_dispatch:

permissions:
  id-token: write
  contents: read
  
env:
  TF_VERSION: 1.28
  
defaults:
  run: 
    shell: bash
    working-directory: "v1_infra/"
  
jobs:
  preview:
    name: Preview
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v3
        with: 
          fetch-depth: 0
      - name: setup terraform
        uses: hashicorp/setup-terraform@v2
        with:
          terraform_version: ${{ env.TF_VERSION }}
          terraform_wrapper: false
          

terraform fmt実装

stepに下記を追加する
usesなどは使わずterraformのデフォルトのコマンドで実装を行う

 - name: format
        run: |
          terraform fmt --check

credencialの情報設定、terraform init実装

credencial生成にはaws-actions/configure-aws-credentials@v1を利用する

OIDCを使ってawsにアクセスする際には、IAMロールのarnを指定する必要があるので、IAMロールのarnをgithub actionのsecretに登録しておく必要がある。

公式の使い方を見ながら下記を追加した

- name: aws credencial setup
        uses: aws-actions/configure-aws-credentials@v1
        with: 
          role-to-assume: ${{ secrets.AWS_IAM_ROLE_ARN }}
          aws-region: ${{ env.AWS_REGION }}
      - name: terraform init
        run: | 
          terraform init --no-color

terraform validateの実装

terraformのvalidateコマンドを実行すれば良いので
下記を追加した

- name: terraform validate
        run: |
          terraform validate -no-color

terraform planを実装

下記を追加する

- name: terraform paln
  continue-on-error: true
  run: | 
    terraform plan \
      -no-color \
      -detailed-exitcode

apply

- name: terraform apply
        if: ${{ !contains(steps.plan.outputs.stdout, 'No changes.') }}
        run: | 
          terraform apply \
            -input=false \
            -no-color \
            -auto-approve
          

ついでに手動で起動し、不要な時は消しておけるdestoryのワークフローも作成

name: "destory"

on:
  workflow_dispatch:

permissions:
  id-token: write
  contents: read
  
env:
  TF_VERSION: 1.2.8
  AWS_REGION: ap-northeast-1
  
defaults:
  run: 
    shell: bash
    working-directory: "v1_infra/"
  
jobs:
  destory:
    name: Destory
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v3
        with: 
          fetch-depth: 0
      - name: setup terraform
        uses: hashicorp/setup-terraform@v2
        with:
          terraform_version: ${{ env.TF_VERSION }}
          terraform_wrapper: false
      - name: aws credencial setup
        uses: aws-actions/configure-aws-credentials@v1
        with: 
          role-to-assume: ${{ secrets.AWS_IAM_ROLE_ARN }}
          aws-region: ${{ env.AWS_REGION }}
      - name: terraform init
        run: | 
          terraform init -no-color
      - name: terraform destory
        run: terraform apply -destroy -auto-approve

Discussion