From d85292ea000143e8983f94feb71a3b4db0b38344 Mon Sep 17 00:00:00 2001 From: Josh Knapp Date: Mon, 16 Nov 2026 18:40:12 -0800 Subject: [PATCH] fix(aws): add the EC2 root volume to the cost estimate; name public IPv4 The EC2 estimate adds the 50 GiB gp3 root volume at the region's gp3 GB-month price (Pricing API, cached per region like the instance price; fixture recorded 2026-11-16). The note says whether it is included and that a public-mode environment's IPv4 address adds $0.005/hour. Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 4 +- app/src-tauri/src/aws/pricing.rs | 80 ++++++++++++++++++- .../ebs-gp3-get-products-us-west-2.json | 6 ++ app/src-tauri/src/aws/pricing/tests.rs | 41 +++++++++- app/src-tauri/src/aws/tofu_modules/tests.rs | 3 +- app/src-tauri/src/commands/host_commands.rs | 20 +++-- .../projects/home/AwsCostEstimate.test.tsx | 16 +++- .../projects/home/AwsCostEstimate.tsx | 4 + app/src/lib/types.ts | 3 + 9 files changed, 159 insertions(+), 18 deletions(-) create mode 100644 app/src-tauri/src/aws/pricing/fixtures/ebs-gp3-get-products-us-west-2.json diff --git a/CLAUDE.md b/CLAUDE.md index c08d04a..afe1084 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -389,7 +389,9 @@ docker exec stdout → tokio task → emit("terminal-output-{sessionId}") → li `None` ("Estimate unavailable"), a failed region isn't re-asked for 5 min, a stale cache still answers. EC2 projects (E10) pass `instance_type` (the user's, from `aws_ec2_capacity`): the `AmazonEC2` on-demand Linux/shared-tenancy hourly price (`EC2_FILTERS`), cached as `--ec2-.json` - with the same rules — the instance's price, not the reservations. + with the same rules — the instance's price, not the reservations — plus the 50 GiB gp3 root volume + (`GP3_FILTERS`, `--ebs-gp3.json`, best effort: without it the note says it's missing; + `EC2_ROOT_VOLUME_GIB` is pinned to `ec2.tf`). A public IPv4 address ($0.005/h) is only in the note. **ECS on EC2 (phase 7; `aws/terraform/user-onboard/ec2.tf`, `workspace.rs` `ec2_plan`, `aws_host.rs`).** Opt-in per onboarding (`ec2_instance_type`); a project's `launch_type` is `Fargate | Ec2`. Why the rules are what they are: diff --git a/app/src-tauri/src/aws/pricing.rs b/app/src-tauri/src/aws/pricing.rs index ca4bc15..23eec2e 100644 --- a/app/src-tauri/src/aws/pricing.rs +++ b/app/src-tauri/src/aws/pricing.rs @@ -65,6 +65,22 @@ pub const EC2_FILTERS: &[(&str, &str, &str)] = &[ ("TERM_MATCH", "licenseModel", "No License required"), ]; +/// `GetProducts` filters for gp3 EBS storage in `{region}`. Verified against +/// the live API (2026-11-16): one row (`USW2-EBS:VolumeUsage.gp3`, $0.08 per +/// GB-month). +pub const GP3_FILTERS: &[(&str, &str, &str)] = &[ + ("TERM_MATCH", "regionCode", "{region}"), + ("TERM_MATCH", "productFamily", "Storage"), + ("TERM_MATCH", "volumeApiName", "gp3"), +]; + +/// The EC2 instance's root volume (`ec2.tf`'s `volume_size`, gp3), billed +/// while the instance exists. +pub const EC2_ROOT_VOLUME_GIB: u32 = 50; + +/// Hours in a billing month, as AWS converts GB-month prices. +pub const HOURS_PER_MONTH: f64 = 730.0; + /// One `GetProducts` query: a service and its filters, values filled in. #[derive(Debug, Clone, PartialEq)] pub struct Query { @@ -100,6 +116,14 @@ pub fn ec2_query(region: &str, instance_type: &str) -> Query { } } +/// The gp3 storage price of `region` ([`GP3_FILTERS`]). +pub fn gp3_query(region: &str) -> Query { + Query { + service_code: EC2_SERVICE_CODE, + filters: fill(GP3_FILTERS, &[("region", region)]), + } +} + /// One architecture's on-demand rates in a region, in USD. #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] pub struct FargateRates { @@ -131,13 +155,17 @@ impl RegionRates { /// usually cheaper); EFS is billed per GB-month, not per hour /// (`efs_billed_separately`). `ec2_instance`: the figure is the user's EC2 /// instance's hourly price, whatever the project reserves of it, and -/// instances scale to zero when no workspace is running (E10). +/// instances scale to zero when no workspace is running (E10); +/// `root_volume_included`: it also has the instance's 50 GiB gp3 root +/// volume (when its price could be read). An instance's public IPv4 +/// address (public-mode environments) is never in it. #[derive(Debug, Clone, Copy, PartialEq, Serialize)] pub struct HourlyEstimate { pub usd_per_hour: f64, pub note_spot: bool, pub efs_billed_separately: bool, pub ec2_instance: bool, + pub root_volume_included: bool, } pub fn estimate(cfg: &AwsWorkspaceConfig, rates: &FargateRates) -> HourlyEstimate { @@ -151,21 +179,44 @@ pub fn estimate(cfg: &AwsWorkspaceConfig, rates: &FargateRates) -> HourlyEstimat note_spot: cfg.spot, efs_billed_separately: cfg.storage != StorageMode::Ephemeral, ec2_instance: false, + root_volume_included: false, } } /// An EC2 project's estimate: its instance's on-demand hourly price — the /// instance is billed whole while it runs, so the reservations, the -/// ephemeral size and Spot (none on EC2) change nothing. -pub fn estimate_ec2(cfg: &AwsWorkspaceConfig, instance_usd_per_hour: f64) -> HourlyEstimate { +/// ephemeral size and Spot (none on EC2) change nothing — plus its +/// [`EC2_ROOT_VOLUME_GIB`] gp3 root volume at `gp3_usd_per_gb_month` (per +/// hour, over [`HOURS_PER_MONTH`]) when that price is known. +pub fn estimate_ec2( + cfg: &AwsWorkspaceConfig, + instance_usd_per_hour: f64, + gp3_usd_per_gb_month: Option, +) -> HourlyEstimate { + let root = gp3_usd_per_gb_month.map(|p| f64::from(EC2_ROOT_VOLUME_GIB) * p / HOURS_PER_MONTH); HourlyEstimate { - usd_per_hour: instance_usd_per_hour, + usd_per_hour: instance_usd_per_hour + root.unwrap_or(0.0), note_spot: false, efs_billed_separately: cfg.storage != StorageMode::Ephemeral, ec2_instance: true, + root_volume_included: root.is_some(), } } +/// The gp3 storage price per GB-month in a [`GP3_FILTERS`] price list for +/// `region`; `None` when no row matches. +pub fn parse_gp3_price(region: &str, items: &[String]) -> Option { + items.iter().find_map(|item| { + let product = serde_json::from_str::(item).ok()?; + let attrs = &product["product"]["attributes"]; + let is = |k: &str, v: &str| attrs[k].as_str() == Some(v); + if !(is("regionCode", region) && is("volumeApiName", "gp3")) { + return None; + } + on_demand_usd(&product).filter(|p| *p > 0.0) + }) +} + /// The on-demand hourly price of `instance_type` (Linux, shared tenancy) in /// an `AmazonEC2` price list for `region`. The filters already narrow it to /// one row; the attributes are checked again so a looser answer can't price @@ -385,6 +436,12 @@ impl CacheKey { file: format!("{region}--ec2-{instance_type}.json"), } } + fn gp3(region: &str) -> Self { + Self { + key: format!("{region} gp3"), + file: format!("{region}--ebs-gp3.json"), + } + } } /// A cache file. `key` was `region` before EC2 prices were cached too (an @@ -553,6 +610,21 @@ impl RatesCache { .await } + /// [`Self::rates_with`] for `region`'s gp3 storage price per GB-month + /// (an EC2 instance's root volume). + pub async fn gp3_price_with>( + &self, + api: impl Future>, + region: &str, + now: SystemTime, + ) -> Option { + let parse = |items: &[String]| { + parse_gp3_price(region, items).ok_or_else(|| format!("no gp3 price for {region} in the price list")) + }; + self.cached::(&CacheKey::gp3(region), api, gp3_query(region), parse, now) + .await + } + /// Fresh cache, else a fetch of `query` (client build included, within /// [`FETCH_TIMEOUT`]) parsed by `parse` that refreshes the cache, else /// the stale cache, else `None`; a key whose fetch failed is not asked diff --git a/app/src-tauri/src/aws/pricing/fixtures/ebs-gp3-get-products-us-west-2.json b/app/src-tauri/src/aws/pricing/fixtures/ebs-gp3-get-products-us-west-2.json new file mode 100644 index 0000000..39ab9a1 --- /dev/null +++ b/app/src-tauri/src/aws/pricing/fixtures/ebs-gp3-get-products-us-west-2.json @@ -0,0 +1,6 @@ +{ + "PriceList": [ + "{\"product\":{\"productFamily\":\"Storage\",\"attributes\":{\"maxThroughputvolume\":\"2000 MiB/s\",\"volumeType\":\"General Purpose\",\"maxIopsvolume\":\"80000\",\"usagetype\":\"USW2-EBS:VolumeUsage.gp3\",\"locationType\":\"AWS Region\",\"maxVolumeSize\":\"64 TiB\",\"storageMedia\":\"SSD-backed\",\"regionCode\":\"us-west-2\",\"servicecode\":\"AmazonEC2\",\"volumeApiName\":\"gp3\",\"location\":\"US West (Oregon)\",\"servicename\":\"Amazon Elastic Compute Cloud\",\"operation\":\"\",\"group\":\"EBS Storage\"},\"sku\":\"BB8UJWJ4XPFJB95G\"},\"serviceCode\":\"AmazonEC2\",\"terms\":{\"OnDemand\":{\"BB8UJWJ4XPFJB95G.JRTCKXETXF\":{\"priceDimensions\":{\"BB8UJWJ4XPFJB95G.JRTCKXETXF.6YS6EN2CT7\":{\"unit\":\"GB-Mo\",\"endRange\":\"Inf\",\"description\":\"$0.08 per GB-month of General Purpose (gp3) provisioned storage - US West (Oregon)\",\"appliesTo\":[],\"rateCode\":\"BB8UJWJ4XPFJB95G.JRTCKXETXF.6YS6EN2CT7\",\"beginRange\":\"0\",\"pricePerUnit\":{\"USD\":\"0.0800000000\"}}},\"sku\":\"BB8UJWJ4XPFJB95G\",\"effectiveDate\":\"2026-11-01T00:00:00Z\",\"offerTermCode\":\"JRTCKXETXF\",\"termAttributes\":{}}}},\"version\":\"20261114012926\",\"publicationDate\":\"2026-11-14T01:29:26Z\"}" + ], + "FormatVersion": "aws_v1" +} diff --git a/app/src-tauri/src/aws/pricing/tests.rs b/app/src-tauri/src/aws/pricing/tests.rs index 30a9fe6..f36a338 100644 --- a/app/src-tauri/src/aws/pricing/tests.rs +++ b/app/src-tauri/src/aws/pricing/tests.rs @@ -514,15 +514,16 @@ fn the_ec2_filters_name_linux_shared_tenancy_on_demand_for_the_type() { #[test] fn an_ec2_estimate_is_the_instances_price_whatever_the_reservations() { - let small = estimate_ec2(&ec2_cfg(512, 1024, StorageMode::Ephemeral), M7I_LARGE); - let big = estimate_ec2(&ec2_cfg(1920, 7373, StorageMode::Ephemeral), M7I_LARGE); + let small = estimate_ec2(&ec2_cfg(512, 1024, StorageMode::Ephemeral), M7I_LARGE, None); + let big = estimate_ec2(&ec2_cfg(1920, 7373, StorageMode::Ephemeral), M7I_LARGE, None); assert!(close(small.usd_per_hour, 0.1008) && close(big.usd_per_hour, 0.1008)); assert!(small.ec2_instance && !small.note_spot && !small.efs_billed_separately); + assert!(!small.root_volume_included); // Ephemeral size and Spot don't apply on EC2; EFS is still billed apart. let mut c = ec2_cfg(1024, 4096, StorageMode::Hybrid); c.ephemeral_gib = 200; c.spot = true; - let e = estimate_ec2(&c, M7G_LARGE); + let e = estimate_ec2(&c, M7G_LARGE, None); assert!(close(e.usd_per_hour, 0.0816)); assert!(e.efs_billed_separately && !e.note_spot && e.ec2_instance); // Fargate estimates say they are not an instance's. @@ -612,3 +613,37 @@ fn a_fargate_cache_file_written_before_ec2_prices_still_reads() { std::fs::write(c.dir().join("us-west-2.json"), old.to_string()).unwrap(); assert_eq!(c.read("us-west-2", at(T0)), Some((RATES, true))); } + +const GP3_FIXTURE: &str = include_str!("fixtures/ebs-gp3-get-products-us-west-2.json"); + +fn gp3_items() -> Vec { + let v: Value = serde_json::from_str(GP3_FIXTURE).unwrap(); + v["PriceList"].as_array().unwrap().iter().map(|s| s.as_str().unwrap().to_string()).collect() +} + +/// The root volume (50 GiB gp3, `ec2.tf`) is in an EC2 estimate when its +/// price is known: $0.08/GB-month in us-west-2 (recorded 2026-11-16) is +/// 50 × 0.08 / 730 ≈ $0.0055/h on top of the instance. +#[test] +fn an_ec2_estimate_adds_the_gp3_root_volume_when_its_price_is_known() { + assert_eq!(parse_gp3_price("us-west-2", &gp3_items()), Some(0.08)); + assert_eq!(parse_gp3_price("eu-west-1", &gp3_items()), None, "another region's row"); + assert_eq!(parse_gp3_price("us-west-2", &ec2_items()), None, "not a gp3 row"); + let e = estimate_ec2(&ec2_cfg(1024, 4096, StorageMode::Ephemeral), M7I_LARGE, Some(0.08)); + assert!(close(e.usd_per_hour, 0.1008 + 50.0 * 0.08 / 730.0), "{}", e.usd_per_hour); + assert!(e.root_volume_included && e.ec2_instance); + assert_eq!(gp3_query("us-west-2").service_code, "AmazonEC2"); +} + +#[tokio::test] +async fn the_gp3_price_is_fetched_once_and_cached_per_region() { + let (_d, c) = cache(); + let api = FakeApi::answering(vec![Ok(Page { items: gp3_items(), next_token: None })]); + let price = c.gp3_price_with::(async { Some(&api) }, "us-west-2", at(T0)).await; + assert_eq!(price, Some(0.08)); + assert_eq!(api.queries.lock().unwrap()[0], gp3_query("us-west-2")); + assert!(c.dir().join("us-west-2--ebs-gp3.json").is_file()); + let again = c.gp3_price_with::(async { Some(&api) }, "us-west-2", at(T0 + 1)).await; + assert_eq!(again, Some(0.08)); + assert_eq!(api.calls(), 1); +} diff --git a/app/src-tauri/src/aws/tofu_modules/tests.rs b/app/src-tauri/src/aws/tofu_modules/tests.rs index 6d14b85..0c903e6 100644 --- a/app/src-tauri/src/aws/tofu_modules/tests.rs +++ b/app/src-tauri/src/aws/tofu_modules/tests.rs @@ -2936,7 +2936,8 @@ fn the_launch_template_is_pinned() { let ebs = &one_block(bdm, "ebs").body; assert_eq!( attr(ebs, "volume_size"), - Some(&Expression::Number(50.into())) + Some(&Expression::Number(crate::aws::pricing::EC2_ROOT_VOLUME_GIB.into())), + "the cost estimate prices this root volume" ); assert_eq!( attr(ebs, "volume_type"), diff --git a/app/src-tauri/src/commands/host_commands.rs b/app/src-tauri/src/commands/host_commands.rs index b1a5172..c02e8fa 100644 --- a/app/src-tauri/src/commands/host_commands.rs +++ b/app/src-tauri/src/commands/host_commands.rs @@ -372,15 +372,21 @@ async fn cost_estimate( }; if config.launch_type == LaunchType::Ec2 { let instance_type = instance_type.filter(|t| ec2_instance_spec(t).is_some())?; - let price = match cache.read_ec2(®ion, instance_type, now) { - Some((price, true)) => price, - _ => { - cache - .ec2_price_with::(api(), ®ion, instance_type, now) - .await? + let instance = async { + match cache.read_ec2(®ion, instance_type, now) { + Some((price, true)) => Some(price), + _ => { + cache + .ec2_price_with::(api(), ®ion, instance_type, now) + .await + } } }; - return Some(pricing::estimate_ec2(config, price)); + // The root volume's price is best effort: without it the estimate + // is the instance's alone (and says so). + let gp3 = cache.gp3_price_with::(api(), ®ion, now); + let (price, gp3) = tokio::join!(instance, gp3); + return Some(pricing::estimate_ec2(config, price?, gp3)); } let rates = match cache.read(®ion, now) { Some((rates, true)) => Some(rates), diff --git a/app/src/components/projects/home/AwsCostEstimate.test.tsx b/app/src/components/projects/home/AwsCostEstimate.test.tsx index d108c83..4d43dd8 100644 --- a/app/src/components/projects/home/AwsCostEstimate.test.tsx +++ b/app/src/components/projects/home/AwsCostEstimate.test.tsx @@ -46,6 +46,7 @@ describe("AwsCostEstimate", () => { note_spot: false, efs_billed_separately: false, ec2_instance: false, + root_volume_included: false, }); const c = config(); render(); @@ -63,6 +64,7 @@ describe("AwsCostEstimate", () => { note_spot: true, efs_billed_separately: true, ec2_instance: false, + root_volume_included: false, }); render( { }); it("asks once per settled change of a cost field, not for unrelated edits", async () => { - awsCostEstimate.mockResolvedValue({ usd_per_hour: 0.1, note_spot: false, efs_billed_separately: false, ec2_instance: false }); + awsCostEstimate.mockResolvedValue({ usd_per_hour: 0.1, note_spot: false, efs_billed_separately: false, ec2_instance: false, root_volume_included: false }); const { rerender } = render(); await settle(); expect(awsCostEstimate).toHaveBeenCalledTimes(1); @@ -118,8 +120,16 @@ describe("AwsCostEstimate", () => { expect(awsCostEstimate.mock.calls[1][2].cpu).toBe(4096); }); + it("says when an EC2 estimate lacks the root volume's price", async () => { + awsCostEstimate.mockResolvedValue({ usd_per_hour: 0.1008, note_spot: false, efs_billed_separately: false, ec2_instance: true, root_volume_included: false }); + render(); + await settle(); + expect(screen.getByText(/Its 50 GiB gp3 root volume \(about \$4 a month\) is not included/)).toBeInTheDocument(); + expect(screen.getByText(/public IPv4 address adds/)).toBeInTheDocument(); + }); + it("prices an EC2 project as its instance type, and says instances scale to zero", async () => { - awsCostEstimate.mockResolvedValue({ usd_per_hour: 0.1008, note_spot: false, efs_billed_separately: true, ec2_instance: true }); + awsCostEstimate.mockResolvedValue({ usd_per_hour: 0.1008, note_spot: false, efs_billed_separately: true, ec2_instance: true, root_volume_included: true }); const c = config({ launch_type: "ec2", storage: "hybrid" }); const { rerender } = render( , @@ -129,6 +139,8 @@ describe("AwsCostEstimate", () => { expect(screen.getByText("≈ $0.10/hour")).toBeInTheDocument(); expect(screen.getByText(/Instances scale to zero when no workspace is running/)).toBeInTheDocument(); expect(screen.getByText(/EFS storage is billed separately/)).toBeInTheDocument(); + expect(screen.getByText(/Includes its 50 GiB gp3 root volume/)).toBeInTheDocument(); + expect(screen.getByText(/public IPv4 address adds \$0.005\/hour/)).toBeInTheDocument(); expect(screen.queryByText(/While the task runs/)).toBeNull(); // The reservations don't change an instance's price: no new call. rerender( diff --git a/app/src/components/projects/home/AwsCostEstimate.tsx b/app/src/components/projects/home/AwsCostEstimate.tsx index f4f5d95..cb17cd8 100644 --- a/app/src/components/projects/home/AwsCostEstimate.tsx +++ b/app/src/components/projects/home/AwsCostEstimate.tsx @@ -76,6 +76,10 @@ export default function AwsCostEstimate({ connectionId, region, config, instance {estimate?.ec2_instance && (

While your instance runs. Instances scale to zero when no workspace is running. + {estimate.root_volume_included + ? " Includes its 50 GiB gp3 root volume." + : " Its 50 GiB gp3 root volume (about $4 a month) is not included."} + {" In an environment with public networking, the instance's public IPv4 address adds $0.005/hour."} {estimate.efs_billed_separately && " EFS storage is billed separately."}

)} diff --git a/app/src/lib/types.ts b/app/src/lib/types.ts index 8b52d4f..b74b86b 100644 --- a/app/src/lib/types.ts +++ b/app/src/lib/types.ts @@ -1283,6 +1283,9 @@ export interface HourlyEstimate { /** EC2 (E10): the user's instance's on-demand price, whatever the project * reserves of it; instances scale to zero when no workspace is running. */ ec2_instance: boolean; + /** EC2: the figure includes the instance's 50 GiB gp3 root volume (its + * price could be read). A public IPv4 address is never in it. */ + root_volume_included: boolean; } export type Verdict = "allowed" | "denied" | "unverified";