🕌

infracost で AWS S3 のコストを計算させる

2024/01/13に公開

Amazon S3 object(1GB) の terraform resource

test.tf
resource "aws_s3_bucket" "example" {
  bucket = "duck-tf-test-bucket"

  tags = {
    Name        = "My bucket"
    Environment = "Dev"
  }
}

resource "aws_s3_bucket_object" "example" {
  key        = "duck-test-object"
  bucket     = aws_s3_bucket.example.id
  source     = "index.html"
}

AWS S3 object のサイズは 1GB で作成した。

infracost を実行してもコストが$0.00になる

$ infracost breakdown --path .
2024-01-13T19:02:13+09:00 INF Enabled policies V2
2024-01-13T19:02:13+09:00 INF Enabled tag policies
Evaluating Terraform directory at .
  ✔ Downloading Terraform modules 
  ✔ Evaluating Terraform directory 
  ✔ Retrieving cloud prices to calculate costs 

Project: duck-falcon/test

 Name                                               Monthly Qty  Unit                    Monthly Cost 
                                                                                                      
 aws_s3_bucket.example                                                                                
 └─ Standard                                                                                        
    ├─ Storage                              Monthly cost depends on usage: $0.025 per GB            
    ├─ PUT, COPY, POST, LIST requests       Monthly cost depends on usage: $0.0047 per 1k requests  
    ├─ GET, SELECT, and all other requests  Monthly cost depends on usage: $0.00037 per 1k requests 
    ├─ Select data scanned                  Monthly cost depends on usage: $0.00225 per GB          
    └─ Select data returned                 Monthly cost depends on usage: $0.0008 per GB           
                                                                                                      
 OVERALL TOTAL                                                                                  $0.00 
──────────────────────────────────
2 cloud resources were detected:
∙ 1 was estimated, it includes usage-based costs, see https://infracost.io/usage-file
∙ 1 was free, rerun with --show-skipped to see details

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓
┃ Project                                            ┃ Monthly cost ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━┫
┃ duck-falcon/test                                   ┃ $0.00        ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━┛

Amazon S3のバケットの保存容量やアクセス頻度が不明なためコストが$0.00と表示されてしまう。

解決策

次の手順で保存容量やアクセス頻度を指定すれば正しく料金が計算できる。

従量課金用の設定ファイルを作成する

infracost breakdown --sync-usage-file --usage-file infracost-usage.yml --path .

オプション

  • sync-usage-file: 現在の terraform resource を参照し、従量課金用の設定ファイルのテンプレートを作成する。
  • usage-file:
    • これで指定した従量課金用の設定ファイルを反映したコスト計算を行う。

sync-usage-file は usage-file で指定したファイルに上書きする形で設定ファイルを作る点に注意。
両方のオプションを同時に実行すると sync-usage-file で設定ファイル上書きした後に usage-file で参照してコスト計算の順になるので、正しいコストが計算されない。

従量課金用の設定ファイルの編集

長いので折りたたみ。わかりやすいコメントがあるので、変更は容易である。

infracost-usage.yml
infracost-usage.yml
# You can use this file to define resource usage estimates for Infracost to use when calculating
# the cost of usage-based resource, such as AWS S3 or Lambda.
# `infracost breakdown --usage-file infracost-usage.yml [other flags]`
# See https://infracost.io/usage-file/ for docs
version: 0.1
# resource_type_default_usage:
  ##
  ## The following usage values apply to each resource of the given type, which is useful when you want to define defaults.
  ## All values are commented-out, you can uncomment resource types and customize as needed.
  ##
  # aws_s3_bucket:
    # object_tags: 0 # Total object tags. Only for AWS provider V3.
    # standard:
      # storage_gb: 0.0 # Total storage in GB.
      # monthly_tier_1_requests: 0 # Monthly PUT, COPY, POST, LIST requests (Tier 1).
      # monthly_tier_2_requests: 0 # Monthly GET, SELECT, and all other requests (Tier 2).
      # monthly_select_data_scanned_gb: 0.0 # Monthly data scanned by S3 Select in GB.
      # monthly_select_data_returned_gb: 0.0 # Monthly data returned by S3 Select in GB.
    # intelligent_tiering:
      # frequent_access_storage_gb: 0.0 # Total storage for Frequent Access Tier in GB.
      # infrequent_access_storage_gb: 0.0 # Total storage for Infrequent Access Tier in GB.
      # monitored_objects: 0 # Total objects monitored by the Intelligent Tiering.
      # monthly_tier_1_requests: 0 # Monthly PUT, COPY, POST, LIST requests (Tier 1).
      # monthly_tier_2_requests: 0 # Monthly GET, SELECT, and all other requests (Tier 2).
      # monthly_lifecycle_transition_requests: 0 # Monthly Lifecycle Transition requests.
      # monthly_select_data_scanned_gb: 0.0 # Monthly data scanned by S3 Select in GB.
      # monthly_select_data_returned_gb: 0.0 # Monthly data returned by S3 Select in GB.
      # early_delete_gb: 0.0 # If an archive is deleted within 1 months of being uploaded, you will be charged an early deletion fee per GB.
      # archive_access_storage_gb: 0.0
      # deep_archive_access_storage_gb: 0.0
    # standard_infrequent_access:
      # storage_gb: 0.0 # Total storage in GB.
      # monthly_tier_1_requests: 0 # Monthly PUT, COPY, POST, LIST requests (Tier 1).
      # monthly_tier_2_requests: 0 # Monthly GET, SELECT, and all other requests (Tier 2).
      # monthly_lifecycle_transition_requests: 0 # Monthly Lifecycle Transition requests.
      # monthly_data_retrieval_gb: 0.0 # Monthly data retrievals in GB
      # monthly_select_data_scanned_gb: 0.0 # Monthly data scanned by S3 Select in GB.
      # monthly_select_data_returned_gb: 0.0 # Monthly data returned by S3 Select in GB.
    # one_zone_infrequent_access:
      # storage_gb: 0.0 # Total storage in GB.
      # monthly_tier_1_requests: 0 # Monthly PUT, COPY, POST, LIST requests (Tier 1).
      # monthly_tier_2_requests: 0 # Monthly GET, SELECT, and all other requests (Tier 2).
      # monthly_lifecycle_transition_requests: 0 # Monthly Lifecycle Transition requests.
      # monthly_data_retrieval_gb: 0.0 # Monthly data retrievals in GB
      # monthly_select_data_scanned_gb: 0.0 # Monthly data scanned by S3 Select in GB.
      # monthly_select_data_returned_gb: 0.0 # Monthly data returned by S3 Select in GB.
    # glacier_flexible_retrieval:
      # storage_gb: 0 # Total storage in GB.
      # monthly_tier_1_requests: 0 # Monthly PUT, COPY, POST, LIST requests (Tier 1).
      # monthly_tier_2_requests: 0 # Monthly GET, SELECT, and all other requests (Tier 2).
      # monthly_lifecycle_transition_requests: 0 # Monthly Lifecycle Transition requests.
      # monthly_standard_select_data_scanned_gb: 0.0 # Monthly data scanned by S3 Select in GB (for standard level of S3 Glacier).
      # monthly_standard_select_data_returned_gb: 0.0 # Monthly data returned by S3 Select in GB (for standard level of S3 Glacier).
      # monthly_bulk_select_data_scanned_gb: 0.0 # Monthly data scanned by S3 Select in GB (for bulk level of S3 Glacier)
      # monthly_bulk_select_data_returned_gb: 0.0 # Monthly data returned by S3 Select in GB (for bulk level of S3 Glacier)
      # monthly_expedited_select_data_scanned_gb: 0.0 # Monthly data scanned by S3 Select in GB (for expedited level of S3 Glacier)
      # monthly_expedited_select_data_returned_gb: 0.0 # Monthly data returned by S3 Select in GB (for expedited level of S3 Glacier)
      # monthly_standard_data_retrieval_requests: 0 # Monthly data Retrieval requests (for standard level of S3 Glacier).
      # monthly_expedited_data_retrieval_requests: 0 # Monthly data Retrieval requests (for expedited level of S3 Glacier).
      # monthly_standard_data_retrieval_gb: 0.0 # Monthly data retrievals in GB (for standard level of S3 Glacier).
      # monthly_expedited_data_retrieval_gb: 0.0 # Monthly data retrievals in GB (for expedited level of S3 Glacier).
      # early_delete_gb: 0.0 # If an archive is deleted within 3 months of being uploaded, you will be charged an early deletion fee per GB.
    # glacier_deep_archive:
      # storage_gb: 0.0 # Total storage in GB.
      # monthly_tier_1_requests: 0 # Monthly PUT, COPY, POST, LIST requests (Tier 1).
      # monthly_tier_2_requests: 0 # Monthly GET, SELECT, and all other requests (Tier 2).
      # monthly_lifecycle_transition_requests: 0 # Monthly Lifecycle Transition requests.
      # monthly_standard_data_retrieval_requests: 0 # Monthly data Retrieval requests (for standard level of S3 Glacier).
      # monthly_bulk_data_retrieval_requests: 0 # Monthly data Retrieval requests (for bulk level of S3 Glacier).
      # monthly_standard_data_retrieval_gb: 0.0 # Monthly data retrievals in GB (for standard level of S3 Glacier).
      # monthly_bulk_data_retrieval_gb: 0.0 # Monthly data retrievals in GB (for bulk level of S3 Glacier).
      # early_delete_gb: 0.0 # If an archive is deleted within 6 months of being uploaded, you will be charged an early deletion fee per GB.
resource_usage:
  aws_s3_bucket.example:
    # object_tags: 0 # Total object tags. Only for AWS provider V3.
    standard:
      storage_gb: 0.0 # Total storage in GB.
      # monthly_tier_1_requests: 0 # Monthly PUT, COPY, POST, LIST requests (Tier 1).
      # monthly_tier_2_requests: 0 # Monthly GET, SELECT, and all other requests (Tier 2).
      # monthly_select_data_scanned_gb: 0.0 # Monthly data scanned by S3 Select in GB.
      # monthly_select_data_returned_gb: 0.0 # Monthly data returned by S3 Select in GB.
    # intelligent_tiering:
      # frequent_access_storage_gb: 0.0 # Total storage for Frequent Access Tier in GB.
      # infrequent_access_storage_gb: 0.0 # Total storage for Infrequent Access Tier in GB.
      # monitored_objects: 0 # Total objects monitored by the Intelligent Tiering.
      # monthly_tier_1_requests: 0 # Monthly PUT, COPY, POST, LIST requests (Tier 1).
      # monthly_tier_2_requests: 0 # Monthly GET, SELECT, and all other requests (Tier 2).
      # monthly_lifecycle_transition_requests: 0 # Monthly Lifecycle Transition requests.
      # monthly_select_data_scanned_gb: 0.0 # Monthly data scanned by S3 Select in GB.
      # monthly_select_data_returned_gb: 0.0 # Monthly data returned by S3 Select in GB.
      # early_delete_gb: 0.0 # If an archive is deleted within 1 months of being uploaded, you will be charged an early deletion fee per GB.
      # archive_access_storage_gb: 0.0
      # deep_archive_access_storage_gb: 0.0
    # standard_infrequent_access:
      # storage_gb: 0.0 # Total storage in GB.
      # monthly_tier_1_requests: 0 # Monthly PUT, COPY, POST, LIST requests (Tier 1).
      # monthly_tier_2_requests: 0 # Monthly GET, SELECT, and all other requests (Tier 2).
      # monthly_lifecycle_transition_requests: 0 # Monthly Lifecycle Transition requests.
      # monthly_data_retrieval_gb: 0.0 # Monthly data retrievals in GB
      # monthly_select_data_scanned_gb: 0.0 # Monthly data scanned by S3 Select in GB.
      # monthly_select_data_returned_gb: 0.0 # Monthly data returned by S3 Select in GB.
    # one_zone_infrequent_access:
      # storage_gb: 0.0 # Total storage in GB.
      # monthly_tier_1_requests: 0 # Monthly PUT, COPY, POST, LIST requests (Tier 1).
      # monthly_tier_2_requests: 0 # Monthly GET, SELECT, and all other requests (Tier 2).
      # monthly_lifecycle_transition_requests: 0 # Monthly Lifecycle Transition requests.
      # monthly_data_retrieval_gb: 0.0 # Monthly data retrievals in GB
      # monthly_select_data_scanned_gb: 0.0 # Monthly data scanned by S3 Select in GB.
      # monthly_select_data_returned_gb: 0.0 # Monthly data returned by S3 Select in GB.
    # glacier_flexible_retrieval:
      # storage_gb: 0 # Total storage in GB.
      # monthly_tier_1_requests: 0 # Monthly PUT, COPY, POST, LIST requests (Tier 1).
      # monthly_tier_2_requests: 0 # Monthly GET, SELECT, and all other requests (Tier 2).
      # monthly_lifecycle_transition_requests: 0 # Monthly Lifecycle Transition requests.
      # monthly_standard_select_data_scanned_gb: 0.0 # Monthly data scanned by S3 Select in GB (for standard level of S3 Glacier).
      # monthly_standard_select_data_returned_gb: 0.0 # Monthly data returned by S3 Select in GB (for standard level of S3 Glacier).
      # monthly_bulk_select_data_scanned_gb: 0.0 # Monthly data scanned by S3 Select in GB (for bulk level of S3 Glacier)
      # monthly_bulk_select_data_returned_gb: 0.0 # Monthly data returned by S3 Select in GB (for bulk level of S3 Glacier)
      # monthly_expedited_select_data_scanned_gb: 0.0 # Monthly data scanned by S3 Select in GB (for expedited level of S3 Glacier)
      # monthly_expedited_select_data_returned_gb: 0.0 # Monthly data returned by S3 Select in GB (for expedited level of S3 Glacier)
      # monthly_standard_data_retrieval_requests: 0 # Monthly data Retrieval requests (for standard level of S3 Glacier).
      # monthly_expedited_data_retrieval_requests: 0 # Monthly data Retrieval requests (for expedited level of S3 Glacier).
      # monthly_standard_data_retrieval_gb: 0.0 # Monthly data retrievals in GB (for standard level of S3 Glacier).
      # monthly_expedited_data_retrieval_gb: 0.0 # Monthly data retrievals in GB (for expedited level of S3 Glacier).
      # early_delete_gb: 0.0 # If an archive is deleted within 3 months of being uploaded, you will be charged an early deletion fee per GB.
    # glacier_deep_archive:
      # storage_gb: 0.0 # Total storage in GB.
      # monthly_tier_1_requests: 0 # Monthly PUT, COPY, POST, LIST requests (Tier 1).
      # monthly_tier_2_requests: 0 # Monthly GET, SELECT, and all other requests (Tier 2).
      # monthly_lifecycle_transition_requests: 0 # Monthly Lifecycle Transition requests.
      # monthly_standard_data_retrieval_requests: 0 # Monthly data Retrieval requests (for standard level of S3 Glacier).
      # monthly_bulk_data_retrieval_requests: 0 # Monthly data Retrieval requests (for bulk level of S3 Glacier).
      # monthly_standard_data_retrieval_gb: 0.0 # Monthly data retrievals in GB (for standard level of S3 Glacier).
      # monthly_bulk_data_retrieval_gb: 0.0 # Monthly data retrievals in GB (for bulk level of S3 Glacier).
      # early_delete_gb: 0.0 # If an archive is deleted within 6 months of being uploaded, you will be charged an early deletion fee per GB.

s3 バケットの storage 量を1GBにしてみる。

infracost-usage.yml
@@ -74,7 +74,7 @@
   aws_s3_bucket.example:
     # object_tags: 0 # Total object tags. Only for AWS provider V3.
     standard:
-      storage_gb: 0.0 # Total storage in GB.
+      storage_gb: 1.0 # Total storage in GB.
       # monthly_tier_1_requests: 0 # Monthly PUT, COPY, POST, LIST requests (Tier 1).
       # monthly_tier_2_requests: 0 # Monthly GET, SELECT, and all other requests (Tier 2).
       # monthly_select_data_scanned_gb: 0.0 # Monthly data scanned by S3 Select in GB.

従量課金用の設定ファイルを指定して実行

$ infracost breakdown --usage-file infracost-usage.yml --path .
2024-01-13T19:26:13+09:00 INF Enabled policies V2
2024-01-13T19:26:13+09:00 INF Enabled tag policies
Evaluating Terraform directory at .
  ✔ Downloading Terraform modules 
  ✔ Evaluating Terraform directory 
  ✔ Retrieving cloud prices to calculate costs 

Project: duck-falcon/test

 Name                                               Monthly Qty  Unit                    Monthly Cost 
                                                                                                      
 aws_s3_bucket.example                                                                                
 └─ Standard                                                                                        
    ├─ Storage                                              1  GB                             $0.03 
    ├─ PUT, COPY, POST, LIST requests       Monthly cost depends on usage: $0.0047 per 1k requests  
    ├─ GET, SELECT, and all other requests  Monthly cost depends on usage: $0.00037 per 1k requests 
    ├─ Select data scanned                  Monthly cost depends on usage: $0.00225 per GB          
    └─ Select data returned                 Monthly cost depends on usage: $0.0008 per GB           
                                                                                                      
 OVERALL TOTAL                                                                                  $0.03 
──────────────────────────────────
2 cloud resources were detected:
∙ 1 was estimated, it includes usage-based costs, see https://infracost.io/usage-file
∙ 1 was free, rerun with --show-skipped to see details

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓
┃ Project                                            ┃ Monthly cost ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━┫
┃ duck-falcon/test                                   ┃ $0.03        ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━┛

Monthly cost が $0.03 と正しく計算された。

Discussion