mirror of
https://github.com/floci-io/floci.git
synced 2026-12-06 03:28:18 -05:00
A GetProducts filter read only the product's attributes, under the exact spelling of the field, so `productFamily` (held beside the attributes) and `serviceCode` (the attribute is `servicecode`) matched nothing. A filter's field is now one of the product's attributes, or else a member the product holds beside them, as productFamily is. No field is named in the code. The API reference says "Matching isn't case sensitive for any filter type", so fields and values match without regard to case under every filter type, and an exact regionCode in any case picks its Region. With FLOCI_SERVICES_PRICING_AWS_PRICE_LIST=true (off by default), GetProducts answers from the public files of AWS's Price List Bulk API instead of the bundled snapshot. The service is looked up in AWS's offer index. An exact regionCode filter reads the Region's current file, named by the service's region index; without one, the current file for every Region, named by the version index, is read. Each file is downloaded once into <persistent-path>/pricing, and again when AWS publishes a new version. It is read as a stream that keeps only the products of the page, in the file's order, with their terms. An index is reused while AWS's response says it is fresh (Cache-Control max-age less Age), and is otherwise asked for again with its ETag, so an unchanged index comes back as 304 without a body. A connection that takes 10 s to open, or stops sending for 10 s, fails the request. The offer index address is the setting FLOCI_SERVICES_PRICING_AWS_PRICE_LIST_URL. A page holds at most MaxResults products, or the most the operation models when the request names none. Pricing's request constraints come from botocore through tools/aws/regen_pricing_shapes.py, and a request that breaks one is refused in the words AWS answers with: MaxResults out of range, more than 50 Filters, a filter's Field or Value outside 1 to 1024 characters, a missing member, or a filter Type outside the modeled set. The shared request check now words a list of structures, each element's members at its position counting from 1 (filters.1.member.type), as AWS does. A missing required member is InvalidParameterException, the only validation error Pricing models, instead of ValidationException. ListPriceLists holds a timestamp, which the shared check does not word yet, so only its MaxResults is checked. A token names the file version it was read from: a token from a version AWS has replaced has expired (ExpiredNextTokenException), and a token no page wrote is answered as AWS answers it (InvalidNextTokenException). Cost Explorer reads every product of a Region, not only the first 1000, in one pass: the file is read once for where each SKU's terms are, then for the products, reading a match's terms from their place. AWS lists one product per operating system under a usage type, so a usage line is priced at the product for its operation, as the BCM Pricing Calculator matches it. Rates read from a version of AWS's file are kept until AWS publishes another. A usage line with no usage costs nothing at any rate, so no rate is looked up for it, and no Region's file is read for a service nothing used. DescribeServices, GetAttributeValues, ListPriceLists and GetPriceListFileUrl keep answering from the snapshot. Closes #8339
159 lines
7.0 KiB
Markdown
159 lines
7.0 KiB
Markdown
# Pricing (AWS Price List Service)
|
|
|
|
**Protocol:** JSON 1.1
|
|
**Header:** `X-Amz-Target: AWSPriceListService.<Action>`
|
|
**Endpoint prefix:** `api.pricing`
|
|
|
|
Floci emulates the AWS Price List Service backed by a bundled static snapshot.
|
|
Responses match the real AWS wire format so AWS SDK and CLI clients accept the
|
|
reply without modification. The bundled snapshot covers a minimal, representative
|
|
set of services and regions; for broader coverage, point Floci at your own
|
|
snapshot with `FLOCI_SERVICES_PRICING_SNAPSHOT_PATH`.
|
|
|
|
## Supported Operations
|
|
|
|
| Operation | Notes |
|
|
|-----------|-------|
|
|
| `DescribeServices` | Lists bundled services and their queryable attribute names |
|
|
| `GetAttributeValues` | Returns the set of values a given attribute can take |
|
|
| `GetProducts` | Returns `PriceList` as an array of JSON-encoded product-offer strings (matches AWS format) |
|
|
| `ListPriceLists` | Lists available price-list ARNs filtered by service, currency, and optional region |
|
|
| `GetPriceListFileUrl` | Returns a stub HTTPS URL; useful for code paths that validate URL presence |
|
|
|
|
Pagination is supported on all list operations via `NextToken` + `MaxResults`.
|
|
A request is checked against the constraints in the operation's AWS model, such
|
|
as `MaxResults` from 1 to 100, at most 50 `Filters`, and a filter's `Field` and
|
|
`Value` of at most 1024 characters, and a request that breaks one is refused with
|
|
`InvalidParameterException`, as AWS refuses it.
|
|
|
|
## Bundled Snapshot
|
|
|
|
The default snapshot on the classpath covers:
|
|
|
|
| ServiceCode | Regions | Notes |
|
|
|-------------|---------|-------|
|
|
| `AmazonEC2` | `us-east-1` (Linux/Shared tenancy, 3 instance types) | `t3.micro`, `m5.large`, `c5.large` |
|
|
| `AmazonS3` | `us-east-1` (Standard storage) | |
|
|
| `AWSLambda` | `us-east-1` (Requests) | |
|
|
|
|
The snapshot is intentionally minimal — enough to exercise SDK parsing and
|
|
filter logic — not a comprehensive price database.
|
|
|
|
## Configuration
|
|
|
|
| Variable | Default | Description |
|
|
|---|---|---|
|
|
| `FLOCI_SERVICES_PRICING_ENABLED` | `true` | Enable or disable the service |
|
|
| `FLOCI_SERVICES_PRICING_SNAPSHOT_PATH` | *(unset)* | Filesystem directory overriding the bundled snapshot |
|
|
| `FLOCI_SERVICES_PRICING_AWS_PRICE_LIST` | `false` | Answer `GetProducts` from AWS's public price list files instead of the snapshot |
|
|
| `FLOCI_SERVICES_PRICING_AWS_PRICE_LIST_URL` | `https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/index.json` | The offer index of AWS's price list files |
|
|
|
|
### Reading AWS's price list files
|
|
|
|
With `FLOCI_SERVICES_PRICING_AWS_PRICE_LIST=true`, `GetProducts` answers from the
|
|
files of the [AWS Price List Bulk API](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/using-the-aws-price-list-bulk-api.html),
|
|
which need no credentials. Floci looks the service up in AWS's offer index, so
|
|
every service AWS publishes prices for can be queried, then reads:
|
|
|
|
- the current file for one Region, from the service's region index, when the
|
|
request has a `TERM_MATCH` or `EQUALS` filter on `regionCode`;
|
|
- the current file for every Region, from the service's version index, otherwise.
|
|
|
|
Each file is downloaded once into `<persistent-path>/pricing` and downloaded
|
|
again when AWS publishes a new version, which replaces the old copy. The indexes
|
|
are reused while AWS's response says they are fresh (its `Cache-Control`
|
|
max-age), and are otherwise asked for again with their `ETag`, so an unchanged
|
|
index is not downloaded again. Floci needs network access to AWS while the
|
|
setting is on. A connection that takes more than 10 seconds to open, or stops
|
|
sending for 10 seconds, fails the request. Files can be large: the EC2 file for
|
|
`us-east-1` is about 485 MB, and the EC2 file for every Region about 9.2 GB, so
|
|
add a `regionCode` filter to EC2 queries.
|
|
|
|
The products come back as AWS lists them, in the order of the file, with their
|
|
terms of each type. A page holds at most `MaxResults` products, or 100, the most
|
|
`GetProducts` allows, when the request names none. A `NextToken` names the file
|
|
version it was read from: once AWS publishes a new version, the token has expired
|
|
(`ExpiredNextTokenException`) and the query starts again without it.
|
|
|
|
Cost Explorer and the BCM Pricing Calculator read their rates through the
|
|
Pricing service, so they read AWS's files too while the setting is on. Cost
|
|
Explorer reads every product of a Region in one pass over the file, and keeps
|
|
the rates until AWS publishes a new version of it. AWS lists one product per
|
|
operating system under the same usage type, so a usage line is priced at the
|
|
product for its operation, such as `RunInstances` for Linux.
|
|
`DescribeServices`, `GetAttributeValues`, `ListPriceLists` and
|
|
`GetPriceListFileUrl` keep answering from the snapshot.
|
|
|
|
### Snapshot directory layout
|
|
|
|
When `FLOCI_SERVICES_PRICING_SNAPSHOT_PATH` is set, Floci reads files in this
|
|
layout (falling back to the classpath entry for any file that does not exist):
|
|
|
|
```
|
|
<path>/
|
|
services.json # [ { "ServiceCode": "...", "AttributeNames": [...] } ]
|
|
attribute-values/<ServiceCode>/<Attr>.json # [ { "Value": "..." } ]
|
|
products/<ServiceCode>/<Region>.json # [ { "product": {...}, "terms": {...}, ... } ]
|
|
price-lists/<ServiceCode>.json # [ { "PriceListArn": "...", "RegionCode": "...", ... } ]
|
|
```
|
|
|
|
Each product entry is stored as a JSON object; Floci re-serializes it into the
|
|
array-of-JSON-strings shape AWS returns. Drop in a full snapshot generated from
|
|
the [AWS Price List Bulk API](https://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/price-changes.html)
|
|
when the bundled fixtures are insufficient.
|
|
|
|
## Examples
|
|
|
|
```bash
|
|
export AWS_ENDPOINT_URL=http://localhost:4566
|
|
export AWS_DEFAULT_REGION=us-east-1
|
|
export AWS_ACCESS_KEY_ID=test
|
|
export AWS_SECRET_ACCESS_KEY=test
|
|
|
|
aws pricing describe-services --service-code AmazonEC2
|
|
|
|
aws pricing get-attribute-values \
|
|
--service-code AmazonEC2 --attribute-name instanceType
|
|
|
|
aws pricing get-products \
|
|
--service-code AmazonEC2 \
|
|
--filters 'Type=TERM_MATCH,Field=instanceType,Value=t3.micro' \
|
|
'Type=TERM_MATCH,Field=regionCode,Value=us-east-1'
|
|
|
|
aws pricing list-price-lists \
|
|
--service-code AmazonEC2 \
|
|
--effective-date 2026-01-01T00:00:00Z \
|
|
--currency-code USD
|
|
```
|
|
|
|
```python
|
|
import boto3
|
|
|
|
client = boto3.client(
|
|
"pricing",
|
|
endpoint_url="http://localhost:4566",
|
|
region_name="us-east-1",
|
|
)
|
|
|
|
resp = client.get_products(
|
|
ServiceCode="AmazonEC2",
|
|
Filters=[
|
|
{"Type": "TERM_MATCH", "Field": "instanceType", "Value": "t3.micro"},
|
|
{"Type": "TERM_MATCH", "Field": "regionCode", "Value": "us-east-1"},
|
|
],
|
|
)
|
|
for item in resp["PriceList"]:
|
|
# AWS returns PriceList as an array of JSON strings; parse each separately.
|
|
import json
|
|
print(json.loads(item)["product"]["sku"])
|
|
```
|
|
|
|
## Out of Scope
|
|
|
|
- Bulk download of full regional price lists (`GetPriceListFileUrl` returns a
|
|
stub URL; the file is not served).
|
|
- Volume discounts, Savings Plans, or Reserved Instance pricing terms beyond
|
|
what the bundled snapshot declares.
|
|
- Automatic refresh of the snapshot from upstream AWS. To read current prices,
|
|
turn on `FLOCI_SERVICES_PRICING_AWS_PRICE_LIST`.
|