Stripe Terraform プロバイダーをインストールする
ローカル開発用のプロバイダーを設定します。
Stripe Terraform プロバイダーを利用すると、インフラストラクチャーをコードとして活用して Stripe のリソースを管理できます。宣言的な Terraform 構文を使用して、商品、価格、請求メーター、複雑な料金プランを設定しましょう。Stripe インフラ構成がバージョン管理可能になり、再現性と監査性が向上します。API に関する情報はこちらの Stripe API リファレンス をご覧ください。
サンプルワークフロー
このワークフローに従って、継続価格とイベントを受信する Webhook エンドポイントのある商品を作成します。
新しい Terraform ファイル main. を作成します。
terraform { required_providers { stripe = { source = "stripe/stripe" version = "0.1.3" } } } provider "stripe" { # API key is read from STRIPE_API_KEY environment variable # Alternatively, set it explicitly (not recommended for production) # api_key = "sk_test_..." } # Define a product resource "stripe_product" "pro_plan" { name = "Pro Plan" description = "Professional tier with advanced features" } # Create a recurring price for the product resource "stripe_price" "pro_monthly" { product = stripe_product.pro_plan.id currency = "usd" unit_amount = 2900 recurring { interval = "month" } } # Set up a webhook endpoint for payment events resource "stripe_webhook_endpoint" "payments" { url = "https://api.example.com/webhooks/stripe" enabled_events = [ "payment_intent.succeeded", "payment_intent.payment_failed", "customer.subscription.created", "customer.subscription.deleted", ] } output "price_id" { value = stripe_price.pro_monthly.id }
API キーを設定します:
export STRIPE_API_KEY="sk_test_BQokikJOvBiI2HlWgH4olfQ2"
変更を確認して適用します:
terraform plan terraform apply terraform output
サポートされるリソース
このセクションでは利用可能なリソースについて簡単にご紹介します。詳細な解説と設定可能なパラメーターについては サポートされているリソース をご覧ください。
商品カタログ
| リソース | 説明 |
|---|---|
stripe_ | 商品の定義 |
stripe_ | 料金体系の設定 |
stripe_ | 割引クーポン |
stripe_ | 配送料金の設定 |
stripe_ | 税率の定義 |
stripe_ | エンタイトルメントの機能フラグ |
コアリソース
| リソース | 説明 |
|---|---|
stripe_ | 顧客レコード |
stripe_ | Webhook エンドポイントの設定 |
stripe_ | 使用状況追跡メーター |
高度な従量課金制 Private preview
これらのリソースは Billing v2 API の一部であり、プライベートプレビューへのアクセスが必要です。
| リソース | 説明 |
|---|---|
stripe_ | 料金プランコンテナ |
stripe_ | プランの構成要素 |
stripe_ | ライセンスされたアクセスアイテム |
stripe_ | サブスクリプション料金 |
stripe_ | 従量課金の請求項目 |
stripe_ | コンテナの料金体系 |
stripe_ | 個々の料金 |
stripe_ | クレジットと調整 |
データソース
| データソース | 説明 |
|---|---|
stripe_ | 既存の請求メーターを検索する |
ワークスペースのある複数の環境を管理
Terraform ワークスペースを使用すると、分離された状態ファイルを使用して、個別の Stripe 環境 (サンドボックスと本番環境) を管理できます。これにより、テスト環境での作業時に本番リソースが誤って変更されるのを防ぐことができます。
ワークスペースを設定する
サンドボックス (テスト環境) と本番環境用のワークスペースを作成します。
# Create workspaces terraform workspace new sandbox terraform workspace new livemode # List available workspaces terraform workspace list
環境を切り替える
各ワークスペースでは、独自の状態ファイルが保持されています。ワークスペースを切り替えて、対応する API キーを設定します。
# Work in sandbox (test mode) terraform workspace select sandbox export STRIPE_API_KEY=terraform plan terraform apply # Work in livemode (production) terraform workspace select livemode export STRIPE_API_KEY="sk_live_..." terraform plan terraform applysk_test_BQokikJOvBiI2HlWgH4olfQ2
Handle rate limits
Terraform runs operations in parallel by default, which can cause rate limiting errors when managing many Stripe resources. The Stripe API allows up to 100 read operations per second in live mode and 25 in sandbox mode, with individual API endpoints having their own limits.
To prevent hitting rate limits, use the -parallelism flag to reduce the number of concurrent operations:
terraform plan -parallelism=2 terraform apply -parallelism=2
For large infrastructure deployments, we recommend setting parallelism to between 2 and 5 to stay within the rate limits. This is important in sandbox mode, where rate limits are lower.