Skip to content

feat(ec2): enforce the 50-tag-per-resource limit (#469) - #479

Merged
scttfrdmn merged 3 commits into
mainfrom
fix/469-tag-limit
Aug 2, 2026
Merged

scttfrdmn merged 3 commits into
mainfrom
fix/469-tag-limit

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Substrate accepted any number of tags on a resource, so a consumer that hit real EC2's
50-tag ceiling had no way to reach the TagLimitExceeded branch — and, worse, a test
asserting the rejection passed against substrate while the same code failed against AWS.

CreateTags and every tag-on-create path (RunInstances, CreateFleet, CreateImage,
CreateNatGateway) now refuse a resource that would carry more than 50 user tags:
TagLimitExceeded, HTTP 400. From the tagging documentation's restrictions: "Maximum
number of tags per resource – 50".

The counting rule

Two documented rules make this less like arithmetic than it looks, and both fall out of
one expression — the count is over the post-merge key set, excluding reserved keys:

  • Tags with the aws: prefix do not count ("Tags with the aws: prefix do not
    count against your tags per resource limit"). This is load-bearing rather than
    pedantry: substrate stamps aws:ec2:fleet-id on every fleet instance (EC2: CreateFleet does not apply the aws:ec2:fleet-id tag, so a fleet's instances can't be found #443), so a
    counter that included reserved keys would refuse a fleet launch whose template names
    the full 50 user tags — a launch real EC2 accepts. A fleet instance holds 51 tags
    legally.
  • Overwriting an existing key at the limit succeeds, since a key already present
    adds nothing to the set. Written as len(existing) + len(incoming) it would fail
    instead, and real AWS permits it — getmoto/moto#8151
    reports exactly that case.

This also closes the companion gap v0.85.0 recorded as vacuous: the reserved-tag
exemption now has a limit to be exempt from.

Ordering

As with reserved keys (#468), the whole request is refused before anything is modified —
a CreateTags naming one instance with room and one at the limit tags neither.
CreateTags accepts up to 1000 resource IDs and a partial apply is a state real EC2
never produces.

A resource ID that names nothing is not counted against, because the apply step
ignores it: checking it would refuse a request real EC2 accepts as a no-op.
ec2TaggableStateKey is factored out of applyTagsToResource so the limit is counted
against exactly the resources the apply step will modify, rather than the two drifting.

Provenance — split, and the weaker half marked

The code is a doc citation, unlike #452's: EC2's client-error table lists
TagLimitExceeded as "You've reached the limit on the number of tags that you can
assign to the specified resource." The wire message is published nowhere, so the
wording is moto's — a reimplementation rather than a capture. SDKs dispatch on
Error.Code, so the documented half is the one a consumer's error branch turns on.
docs/services.md and the CHANGELOG both say which half is which.

Two documented restrictions still unenforced and tracked separately: a tag key's maximum
of 128 Unicode characters and a value's maximum of 256.

Tests

Six new tests in emulator/ec2_tags_test.go:

  • 50 tags accepted, the 51st rejected with the exact code, status and message.
  • Overwriting key7 on a 50-tag instance succeeds and the value changes; adding key51
    is refused — the two outcomes side by side in one table. The overwrite half fails
    under naive arithmetic.
  • Two resource IDs, one with room and one at the limit → neither is tagged.
  • The aws: exemption asserted on a real fleet instance: it carries
    aws:ec2:fleet-id and still takes 50 user tags (51 total), while the 51st user tag
    is still refused, so the exemption did not raise the ceiling.
  • Tag-on-create: 50 launch tags accepted, 51 rejected with no instance created; 51 tags
    on CreateNatGateway and on CreateImage rejected with no resource left behind.
  • A CreateFleet naming the full 50 instance tags still launches, and the stamp survives.

Four mutations confirmed to fail these tests, then restored:

Mutation Killed by
count len(existing) + len(incoming) OverwriteAtLimitSucceeds
stop exempting aws: keys TagLimit_ExemptsReservedKeys
interleave the check with the apply loop LimitRejectionIsAllOrNothing
check the tag-on-create limit after the launch TagOnCreate_EnforcesTagLimit (the rollback assertion)

Verification

From the repo root: gofmt -l ., go build ./..., go vet ./...,
golangci-lint run ./... (0 issues), make test (race), make docs-reference docs-reference-check docs-versions, go test -C test/e2e ./... — all pass. The full
EC2 suite including the fleet tests is green, which is the #443 regression gate.

Closes #469

Substrate accepted any number of tags on a resource, so a consumer that hit
real EC2's 50-tag ceiling had no way to reach the TagLimitExceeded branch --
and a test asserting the rejection passed against substrate while the same
code failed against AWS.

CreateTags and every tag-on-create path (RunInstances, CreateFleet,
CreateImage, CreateNatGateway) now refuse a resource that would carry more
than 50 user tags: TagLimitExceeded, HTTP 400.

Two rules make this less like arithmetic than it looks, and both are
modelled in one expression -- the count is over the post-merge key set,
excluding reserved keys:

  - Tags with the aws: prefix do not count against the limit. This is
    load-bearing rather than pedantry, because substrate stamps
    aws:ec2:fleet-id on every fleet instance (#443): a counter that
    included reserved keys would refuse a fleet launch whose template
    names the full 50 user tags, which real EC2 accepts.

  - Overwriting an existing key at the limit succeeds, since a key already
    present adds nothing to the set. Written as len(existing)+len(incoming)
    it would fail instead; getmoto/moto#8151 reports real AWS permitting
    it.

As with reserved keys, the whole request is refused before anything is
modified. A resource ID naming nothing is not counted against, because the
apply step ignores it -- checking it would refuse a request real EC2 accepts
as a no-op. ec2TaggableStateKey is factored out of applyTagsToResource so
the limit is counted against exactly the resources the apply step will
modify.

Provenance is split, and the weaker half is marked as such: the code is a
doc citation (EC2's client-error table lists TagLimitExceeded), while the
wire message is moto's, from a reimplementation rather than a capture.

Four mutations were confirmed to fail the new tests: counting
len(existing)+len(incoming); counting reserved keys; interleaving the check
with the apply loop; and moving the tag-on-create check after the launch.

Closes #469
@codecov

codecov Bot commented Aug 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.12698% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/ec2_plugin.go 86.04% 3 Missing and 3 partials ⚠️
emulator/ec2_fleet.go 0.00% 2 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

ec2TaggableStateKey is what makes the limit check and the apply step agree on
which IDs resolve, and only its instance and NAT-gateway arms were exercised.
A resource type the apply step tags but the check could not resolve would be
tagged past the limit, silently.

The new table walks all eight ID prefixes substrate recognises. It asserts on
the rejection rather than a tag read-back because, apart from instances and
NAT gateways, substrate's Describe operations for these resources emit no
tagSet -- the rejection is the observation available.

The second test pins the other side: an ID the apply step ignores is not
counted against either, in both the ways an ID goes unresolved (a prefix
substrate does not tag, and a taggable prefix naming nothing). Counting one
would refuse a request real EC2 accepts as a no-op, trading a
silent-success infidelity for a spurious-rejection one.
golangci-lint enforces US spellings in Go source; the repo's markdown
deliberately uses British forms, which is why this only bites in .go files.
@scttfrdmn
scttfrdmn merged commit be05ef5 into main Aug 2, 2026
16 checks passed
@scttfrdmn scttfrdmn mentioned this pull request Aug 2, 2026
@scttfrdmn
scttfrdmn deleted the fix/469-tag-limit branch August 2, 2026 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EC2: the 50-tag-per-resource limit is unmodelled (TagLimitExceeded never fires)

1 participant