Skip to content

Generated AWS types fall back to string where the CFN spec omits AllowedValues, so the compiler stops teaching the API #1497

Closed

Description

@lex00

The AWS lexicon emits a TypeScript literal union wherever the CloudFormation spec declares AllowedValues, and that works well:

RetentionInDays?: 1 | 3 | 5 | 7 | 14 | 30 | 60 | 90 | 120 | 150 | 180 | 365 | ... | 3653

Pass 400 and tsc rejects it and prints the whole valid set in the error. That is the most valuable thing a generated type does for a model or a newcomer: the compiler teaches the API without anyone reading docs.

Where the spec is silent, the property falls back to string, number, or any. That is faithful to the spec, but the spec is silent on a lot, including the properties people touch most.

The surface today

lexicons/aws/src/generated/index.d.ts:

count
literal-union properties 1,728
bare string 20,533
bare number 3,162
any 762

The part that bites

It is not uniformly sparse — the same property name is typed on one resource and untyped on another, so what the compiler catches depends on which resource you happen to be writing:

InstanceType?: "ml.c4.2xlarge" | "ml.c5.18xlarge" | ...     (SageMaker)
InstanceType?: string                                       (58 other resources, incl. EC2)

VolumeType?: "gp2" | "gp3" | "io1" | "io2" | "sc1" | "st1" | "standard"
VolumeType:  string

Engine: "aurora" | "aurora_postgresql" | "db2" | "mariadb" | ...
Engine: string

RetentionInDays?: 1 | 3 | 5 | ... | 3653
RetentionInDays?: number

ProtocolType?: any

AWS::EC2::Instance.InstanceType — plausibly the single most-written property in AWS — is string. SageMaker's is a union. Nothing about that ordering is defensible from a user's point of view, and it makes the type system's helpfulness feel arbitrary.

Why it is worth doing

A wrong enum value currently survives tsc and chant build, and fails at deploy — the most expensive tier there is. Every value moved into a union relocates that failure to the cheapest tier, which is the whole argument for a typed substrate over a stringly-typed one.

AWS CDK's L2 constructs hand-write enums well past what the CFN spec carries (ec2.InstanceType, ec2.InstanceClass, ec2.InstanceSize), so this is the specific axis where a spec-faithful generator loses to a hand-curated one, and the gap is closeable without giving up generation.

It matters double for agent authoring: an agent writing chant has read a great deal of CloudFormation and none of chant, so the union in an error message is the cheapest teaching channel available — no docs fetch, no MCP round trip, no extra tokens.

Shape of a fix

Not "hand-write 20,533 types". Suggested order:

  1. A curated overlay keyed by CFN type + property, merged over the generated output, so a hand-added enum survives the next spec refresh instead of being overwritten by it. The refresh path already exists (feat(aws): upgrade to latest spec #1423 / lexicon-aws v0.40.0).
  2. Seed the overlay from a better source than the spec where one exists — botocore/service-2.json carries enum shapes the CFN spec omits, and is machine-readable.
  3. Start with the highest-traffic properties rather than by count — EC2 InstanceType, EBS VolumeType, RDS/ElastiCache Engine and EngineVersion, DynamoDB BillingMode, S3 StorageClass.
  4. Kill the 762 anys separately. ProtocolType?: any is worse than string: it disables checking rather than merely widening it, and it is a small enough set to sweep.
  5. Report coverage as a number in the lexicon build, so it can be watched rather than rediscovered.

Widening a type is backwards-compatible; narrowing string to a union is not, so this wants to land with a lexicon minor bump and a note about what newly fails to compile. Anything the overlay narrows should be a value that was already invalid at deploy.

Where it came from

Found while assessing how chant would place in a pre-registered benchmark comparing typed intent-level constructs against raw HCL for AI-assisted IaC, whose headline metric is tokens-to-green and whose first planted "catch" is an enum value-trap — retention_in_days outside the allowed set. chant catches that one at compile time today. It would not catch the same class of error on InstanceType.

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:awsenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions