Skip to content

cost: fetch live AWS prices via GetProducts, with the offer file and a bundled snapshot behind it #227

Open

Description

@scttfrdmn

Split from #183. Steps 3–7 of that issue, deferred until #226 gives an accurate rate a consumer — per #183's own framing: "Getting real prices into a calculator no code calls is the most expensive way to change nothing."

Do not start this before #226. Not a process preference: without an observable output there is no way to tell a correct fetch from the failure mode that killed the last attempt (see below), and the traps here are ones that return plausible-shaped wrong numbers rather than errors.

What already landed

internal/awsrates is the single rate table, verified against the live Pricing API by an integration test that re-reads 23 rates (#220, #225). So this issue is a single-site change — swap the table's source — rather than the five-site change it would have been.

Why the previous attempt failed

#161 deleted a "Pricing API integration" that was not partial: its parser matched products on human-readable storage-class names that do not appear in Pricing API payloads, and both cost extractors ignored their arguments and returned hardcoded constants. Enabling use_pricing_api fetched a multi-megabyte JSON document over HTTP and discarded it in favour of 0.023.

It would have passed against any hand-written mock, because a mock is built from the same assumptions as the parser. That is why the emulator matters here specifically.

Verified traps, carried over from #183

  1. productFamily is absent from 315 of 381 S3 products. Any filter or switch keyed on it silently drops 83% of the catalog. This is what broke the deleted parser.
  2. usagetype is the only stable 1:1 key — USW2-Requests-Tier1, USW2-TimedStorage-ByteHrs. Match on it, never on storageClass or product descriptions.
  3. us-west-2 is not a Pricing API endpoint. The API is at api.pricing.us-east-1.amazonaws.com and api.pricing.ap-south-1.amazonaws.com. EndpointResolverV2 templates any well-formed region into a hostname, so a wrong region gives an opaque DNS failure rather than a validation error. Validate against the two known endpoints before constructing the client. The region whose prices you want is a filter value, not the endpoint.
  4. productFamily=Storage + volumeType='Glacier Deep Archive' returns a staging SKU at 21× the real rate. The trap that makes a naive implementation worse than the static table it replaces: it looks like success and is off by a factor that would dominate any report it appears in. Every fetched rate needs a per-tier sanity band, and a rate outside its band must be rejected and logged — never used.
  5. pricePerUnit values are strings, and a SKU can carry multiple priceDimensions with endRange: "Inf". Tiered pricing is normal.
  6. service/pricing forces smithy-go ≥1.26.0. Its endpoints/private/bdd import is absent from the pinned v1.24.0, and the bump also moves service/s3 and aws-sdk-go-v2 — a dependency change touching the data path, so it wants its own commit and a full -race run before any pricing code lands on top of it.

Hot-path constraint, non-negotiable

GetTierPricing is reachable from a filesystem read: GetObject → CostOptimizer.RecordAccess → calculateObjectCost.

  • atomic.Pointer[RateTable], swapped whole. No lock on the read path.
  • Refresh from a background ticker only. Never fetch synchronously from a filesystem operation.
  • A pricing failure must never fail a filesystem operation. Stale rates, or the bundled snapshot — never an error propagated to a read. The cost feature is advisory; the filesystem is not.

Three sources, in order

  1. GetProducts via service/pricing, on a ticker. Daily is ample; S3 list prices move rarely.
  2. The public offer file (https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonS3/current/index.json) as an unauthenticated fallback. This matters more than it sounds: GetProducts needs pricing:GetProducts IAM permission, which a mount's credentials usually will not have. Without the fallback the feature is off for most users.
  3. The bundled snapshot — internal/awsrates as it stands, regenerated by //go:generate. Always available, no network dependency, and what makes "a pricing failure cannot fail a read" trivially true rather than carefully true.

Region

Step 7 of #183, and the reason #226 must not publish region-labelled cost metrics before this lands: every rate today is us-east-1, so region: us-west-2 yields us-east-1 numbers labelled us-west-2. Fix by filtering on the configured region, or refuse to label output with a region whose rates were not fetched. Needs per-region rates to fix rather than to document, which is why it is here and not in #226.

Testing

internal/testaws runs the real S3 backend against an in-process substrate endpoint. Substrate has no pricing plugin — GetProducts returns 501 — so the fidelity requirements are filed upstream, and they are exactly the traps above:

  • PriceList as []string of raw JSON, not decoded objects
  • pricePerUnit values as strings
  • at least one SKU with multiple priceDimensions and endRange: "Inf"
  • productFamily omitted on some SKUs

Also filed upstream: extractRegionFromHost returns "pricing" instead of the region for api.pricing.<region>.amazonaws.com.

A hand-written mock cannot serve here, for the reason CLAUDE.md gives and #161 demonstrated.

Activity

  1. added this to the Future Work milestone on Aug 3, 2026
  2. added
    type: enhancement
    priority: low
    area: cost
    effort: medium
    on Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: costeffort: mediumpriority: lowtype: enhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions