OpenTacoでInfracostを使ってAWSコスト試算をやってみた

OpenTacoでInfracostを使ってAWSコスト試算をやってみた

2026.01.24

OpenTacoのカスタムコマンドを使うことで、OpenTacoのワークフロー上で他のツールを統合できます。

この方法で以前、OPAを使ったポリシーテストを試してみました。

https://dev.classmethod.jp/articles/opentaco-opa-policy-test-inline/

今回はInfracostを使って、AWS利用費を試算してみます。

以下を参考にしています。

https://docs.opentaco.dev/ce/howto/using-infracost

Infracost APIキーをGitHub Actions Secretsに登録

Infracost APIキーを取得します。

Infracost Cloudにログインして、SettingsOrg SettingsAPI tokensの順で選択します。

CLI and CI/CD tokenが表示されるため、こちらをコピーします。

Organization_settings_-_Infracost.png

コピーしたトークンを、GitHub Actions SecretsにINFRACOST_API_KEYという名前で登録します。

Add_Actions_secret_·_msato0731_quickstart-actions-aws.png

Infracostをセットアップ(GitHub Actions)

GitHub Actions上でInfracostを利用できるようにするため、以下を追記します。

digger_workflow.yml
name: Digger Workflow

on:
  workflow_dispatch:
    inputs:
      spec:
        required: true
      run_name:
        required: false

run-name: '${{inputs.run_name}}'

jobs:
  digger-job:
    runs-on: ubuntu-latest
    permissions:
      contents: write      # required to merge PRs
      actions: write       # required for plan persistence
      id-token: write      # required for workload-identity-federation
      pull-requests: write # required to post PR comments
      issues: read         # required to check if PR number is an issue or not
      statuses: write      # required to validate combined PR status

    steps:
      - uses: actions/checkout@v4
+      - name: Setup Infracost
+        uses: infracost/actions/setup@v3
+        # See https://github.com/infracost/actions/tree/master/setup for other inputs
+        # If you can't use this action, use Docker image infracost/infracost:ci-0.10
+        with:
+          api-key: ${{ secrets.INFRACOST_API_KEY }}
      - name: ${{ fromJSON(github.event.inputs.spec).job_id }}
        run: echo "job id ${{ fromJSON(github.event.inputs.spec).job_id }}"
      - uses: diggerhq/digger@vLatest
        with:
          digger-spec: ${{ inputs.spec }}
          setup-aws: true
          setup-terraform: true
          terraform-version: 1.14.3
          aws-role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

infracost/actions: GitHub Action for Infracost. See cloud cost estimates for Terraform in pull requests. 💰📉 Love your cloud bill!

digger.ymlにInfracost用のカスタムコマンドを追加

digger.ymlにInfracost用のカスタムコマンドを追加します。

digger.yml
projects:
- name: production
  dir: prod
  workflow: with-infracost

workflows:
  with-infracost:
    plan:
      steps:
        - init
        - plan
        - run: infracost breakdown --path=. | tee -a $DIGGER_OUT

GitHub Pull Requestのコメント上でInfracostの結果を表示するため、$DIGGER_OUTにも出力しています。

$DIGGER_OUTについては以下をご確認ください。

https://dev.classmethod.jp/articles/opentaco-custome-command-output/

動作確認

digger planを実行して、Pull Requestのコメントを確認してみました。

以下のようにInfracostの結果が確認できました。

Add_Test_tag_to_Terraform_instance_by_msato0731_·_Pull_Request__13_·_msato0731_quickstart-actions-aws.png

ちなみに利用したTerraformコードは以下です。

main.tf
provider "aws" {
  region = "us-east-1" # Replace with your desired AWS region
}

resource "aws_vpc" "vpc_network" {
  cidr_block = "10.0.0.0/16"
  tags = {
    Name = "terraform-network"
  }
}

resource "aws_subnet" "vpc_subnet" {
  vpc_id                  = aws_vpc.vpc_network.id
  cidr_block              = "10.0.1.0/24"
  availability_zone       = "us-east-1a"
  map_public_ip_on_launch = true

  tags = {
    Name = "terraform-subnet"
  }
}

resource "aws_security_group" "security_group" {
  vpc_id      = aws_vpc.vpc_network.id
  name_prefix = "terraform-"
  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "vm_instance" {
  ami                    = "ami-05c13eab67c5d8861" # us-east-1 Amazon Linux 2023 AMI 2023.2.20231030.1 x86_64 HVM kernel-6.1
  instance_type          = "t2.micro"
  subnet_id              = aws_subnet.vpc_subnet.id
  vpc_security_group_ids = [aws_security_group.security_group.id]
  tags = {
    Name        = "terraform-instance"
  }
}

おわりに

OpenTacoのカスタムコマンドでInfracostを利用する方法についてでした。

Pull Requestのコメントでコスト見積もりが自動表示されるため、インフラ変更時のコスト影響をレビュー段階で把握できます。

今回は単純なコスト試算をご紹介しました。他にもコスト差分を確認する方法も公式ドキュメントで紹介されていますので合わせてご確認ください。

https://docs.opentaco.dev/ce/howto/using-infracost#diff

InfracostとGitHubリポジトリの連携は、OpenTacoに組み込む他にもGitHub Appを使った方法もあります。

興味がある方は以下の記事も確認してみてください。

https://dev.classmethod.jp/articles/infracost-cloud-github-repo-alignment/

この記事をシェアする

FacebookHatena blogX

関連記事