iTranslated by AI
A Practical Guide to Managing Stripe Products and Prices with Terraform
Introduction
When managing Stripe in solo projects, handling Products and Prices can become unexpectedly troublesome.
At first, you create them manually in the dashboard and hardcode the Price IDs into your code. Every time you change your pricing, you create a new Price and swap the IDs via SQL migrations. Since you use separate Stripe accounts for each environment (dev / staging / production), you end up repeating the same tasks over and over.
I faced this exact problem with Storyie, the diary app I am developing. While revising my Pro plan pricing twice—changing it from $5 to $2 and then to $4.99—I had to write SQL migrations and replace Price IDs each time.
-- I was writing migrations like this every time
UPDATE "subscription_plans"
SET "stripe_price_id_monthly" = 'price_1T1cfGPt9XoaRJBnqJ836kRp',
"stripe_price_id_yearly" = 'price_1T1cfGPt9XoaRJBnPP8NecDR',
"price_monthly" = 499,
"price_yearly" = 3999,
"updated_at" = now()
WHERE "name" = 'pro' AND "is_active" = true;
If we manage this with Terraform, creating and modifying Stripe resources becomes declarative, and environment discrepancies disappear. This article introduces the design and implementation for this approach.
Why Manage Stripe with Terraform?
The Limits of Manual Management
Challenges when using the Stripe Dashboard + SQL migrations:
- Lack of Reproducibility — It is a hassle to recreate the same Product/Price configuration in dev as in production.
- Difficult to Track Change History — Operations in the dashboard appear in the audit log, but cannot be tracked via Git.
- Cumbersome Price ID Handoff — A relay sequence of Creating in Stripe → Copying ID → SQL migration → Deployment occurs every time.
- Risk of Pricing Changes — Errors in Price creation are often only noticed after a payment is made.
What Terraform Solves
-
Declarative Definition — The configuration of Products and Prices is consolidated into
.tffiles. - Environmental Consistency — Clear management of staging/production via directory separation.
-
Visibility of Changes — Diffs can be verified in advance using
terraform plan. - Automatic Linkage via Outputs — Created Price IDs can be passed to other IaC tools or your application.
Setting up the Stripe Terraform Provider
You can use the community provider: lukasaron/stripe.
terraform {
required_providers {
stripe = {
source = "lukasaron/stripe"
version = "~> 1.9"
}
}
}
provider "stripe" {
api_key = var.stripe_secret_key
}
variable "stripe_secret_key" {
type = string
sensitive = true
}
Pass the API key via terraform.tfvars or the environment variable TF_VAR_stripe_secret_key. Do not hardcode Secret Keys in .tf files.
Defining Products and Prices
Basic Structure
Storyie has two plans, Free and Pro, where the Pro plan includes both monthly and yearly Prices.
# products.tf
resource "stripe_product" "free" {
name = "Storyie Free"
description = "Basic diary features"
active = true
metadata = {
plan_name = "free"
}
}
resource "stripe_product" "pro" {
name = "Storyie Pro"
description = "Unlimited diaries and advanced features"
active = true
metadata = {
plan_name = "pro"
}
}
# prices.tf
locals {
pro_prices = {
monthly = {
unit_amount = 499 # $4.99
interval = "month"
}
yearly = {
unit_amount = 3999 # $39.99
interval = "year"
}
}
}
resource "stripe_price" "pro" {
for_each = local.pro_prices
product = stripe_product.pro.id
currency = "usd"
unit_amount = each.value.unit_amount
recurring = {
interval = each.value.interval
}
metadata = {
billing_cycle = each.key
}
}
Exposing Price IDs via Outputs
# outputs.tf
output "free_product_id" {
value = stripe_product.free.id
}
output "pro_product_id" {
value = stripe_product.pro.id
}
output "pro_price_id_monthly" {
value = stripe_price.pro["monthly"].id
}
output "pro_price_id_yearly" {
value = stripe_price.pro["yearly"].id
}
Using these outputs, you can automate database seeding or environment variable updates.
Performing Pricing Changes with Terraform
Stripe Prices are immutable. If you want to change prices, you must create a new Price and set the old one to inactive.
Terraform handles this naturally:
# If you change unit_amount from 499 to 699 and apply:
# 1. A new Price is created
# 2. The old Price is removed from Terraform management (archiving must be done manually)
resource "stripe_price" "pro" {
for_each = local.pro_prices
# ...
unit_amount = 699 # Changed
}
However, note the following:
- Existing subscriptions remain at the old price. Stripe does not automatically migrate them.
- If you need to change the price for existing users, you must explicitly update the subscription price via the Stripe API.
- If you set
lifecycle { create_before_destroy = true }, you can manage the flow of archiving the old Price after the new one is created.
Strategy for Environment Separation
Directory Separation (Storyie's Implementation)
For Storyie, I adopted the approach of separating directories by environment.
terraform/stripe/
├── staging/
│ ├── main.tf
│ ├── products.tf
│ ├── prices.tf
│ ├── variables.tf
│ ├── webhook.tf
│ └── outputs.tf
└── production/
├── main.tf
├── products.tf
├── prices.tf
├── variables.tf
├── webhook.tf
└── outputs.tf
While you can use terraform workspace, separating directories offers the following advantages:
- Independent State Files — Mistakes in staging do not affect production.
- Easier Configuration per Environment — You can flexibly handle requirements like adding test webhooks only in staging.
-
Explicit
terraform applyTargets — You know exactly which environment you are targeting when you runcd production && terraform apply.
# staging
cd terraform/stripe/staging
TF_VAR_stripe_secret_key=sk_test_xxx terraform apply
# production
cd terraform/stripe/production
TF_VAR_stripe_secret_key=sk_live_xxx terraform apply
Integration with Applications
There are several ways to pass Price IDs created by Terraform to your application.
1. Passing Outputs to DB Migrations
# Get Price IDs from terraform output to generate SQL
PRO_MONTHLY=$(terraform output -raw pro_price_id_monthly)
PRO_YEARLY=$(terraform output -raw pro_price_id_yearly)
cat <<SQL > migrations/update_stripe_ids.sql
UPDATE subscription_plans
SET stripe_price_id_monthly = '${PRO_MONTHLY}',
stripe_price_id_yearly = '${PRO_YEARLY}',
updated_at = now()
WHERE name = 'pro' AND is_active = true;
SQL
2. Passing as Environment Variables
If you are using SST, you can bind Terraform outputs to SST environment variables.
// sst.config.ts
new sst.aws.Nextjs("Web", {
environment: {
STRIPE_PRICE_PRO_MONTHLY: process.env.STRIPE_PRICE_PRO_MONTHLY!,
STRIPE_PRICE_PRO_YEARLY: process.env.STRIPE_PRICE_PRO_YEARLY!,
},
});
3. Referencing Terraform State Directly (Advanced)
You can use the terraform_remote_state data source to reference Stripe outputs from another Terraform project. This is effective if your entire infrastructure is managed via Terraform.
Thoughts After Implementation
Pros
-
Peace of mind with
terraform plan. You clearly understand "what will change" before updating prices. -
Recreating the environment is one command away. If you break the dev environment,
terraform applyrestores it. - Trackable change history in Git. You have a commit log recording "when and why" the price was changed.
Considerations
- Maturity of the Community Provider. Since it is not an official provider, it does not cover all Stripe API features.
- Managing additional resources. You may want to manage resources like Webhook endpoints or Coupons, but you must check the provider's support status.
-
Importing existing resources is required. You must bring existing Products/Prices created via the dashboard into your state using
terraform import.
Example of Import
# Import an existing Product
terraform import stripe_product.pro prod_TXLt8df1Nl61Cw
# Import an existing Price
terraform import 'stripe_price.pro["monthly"]' price_1T1cfGPt9XoaRJBnqJ836kRp
Summary
By migrating Stripe Product/Price management to Terraform:
- Price revisions become declarative, safe, and traceable.
- Consistency across environments is maintained.
- You are freed from the hassle of manually copying and pasting Price IDs.
Even for personal projects, if you have two or more pricing plans, it is worth implementing IaC early. If you have ever experienced a pricing revision, you will appreciate the sense of security this brings.
About Storyie
The design introduced in this article is practiced in Storyie, a diary and storytelling app.
- 🌐 Web: storyie.com
- 🍎 iOS: App Store
- 🤖 Android Beta: storyie.com/android-beta
Discussion