Subchapter 29.10
references/template-safety-guidance.mdMarkdown8 KBView on GitHub
Never rename or remove an exported Output without checking for Fn::ImportValue consumers.
When a template has Outputs with Export.Name, other stacks may depend on
that export via Fn::ImportValue. Renaming or removing the export will cause
immediate deployment failures in all consuming stacks.
Before modifying any exported output:
Metadata."com.aws.cloudformation.Context" for documented consumerscom.aws.cloudformation.Context
context to reflect the new export nameKey principle: Exported outputs are a public API contract. Treat renames as breaking changes.
Resources sharing a Condition form an atomic feature toggle group.
When multiple resources use the same Condition, they are intentionally coupled
— they must all be created or none created. Removing the Condition from one
resource in the group breaks the atomicity.
Before modifying or removing a Condition from a resource:
Metadata."com.aws.cloudformation.Context" for feature toggle group
documentationAssess the blast radius before modifying shared security groups.
A single security group may be referenced by EC2 instances, RDS databases, Lambda VPC configs, and other resources. Adding an ingress rule affects ALL resources using that group.
Before modifying a security group:
Metadata."com.aws.cloudformation.Context" for documented references
and blast radiusKey principle: Public ingress (0.0.0.0/0) on a shared security group is almost always wrong — it exposes databases and internal services, not just the intended target.
Never remove or downgrade a DeletionPolicy on stateful resources without explicit user confirmation.
Resources with DeletionPolicy: Retain (DynamoDB tables, RDS instances, S3
buckets) contain data that cannot be recreated. When asked to remove such a
resource:
Metadata."com.aws.cloudformation.Context" for data criticality
documentationcom.aws.cloudformation.Context context to document the orphaned resourceKey principle: DeletionPolicy: Retain exists for a reason. Respect it,
document it, and warn loudly before any operation that could result in data
loss.
When adding resources to a template with naming conventions, propagate existing parameters.
Many templates use Parameters (e.g., Environment, Project, Team) to drive
resource naming for multi-environment deployment. New resources must follow the
same convention.
When adding a resource to a template with parameterized names:
Metadata."com.aws.cloudformation.Context" for naming convention
documentation!Sub "${Environment}-...")Metadata."com.aws.cloudformation.Context" to the new resource,
documenting its purpose and constraintsKey principle: Consistency in naming enables multi-environment deployment. A resource that breaks the naming convention becomes an obstacle to promotion across environments.
Check the template body size before adding resources to an already-large
template. CloudFormation enforces hard limits: a template body passed inline
(TemplateBody) is capped at 51,200 bytes, a template uploaded via S3
(TemplateURL) at 1,048,576 bytes (1 MB), and any single template at 500
resources. A template that already carries many resources or rich
Metadata."com.aws.cloudformation.Context" may be close to these limits, so the
addition you are about to make may not fit.
Service Quotas reports the current values for two of these — Template Size
(1 MB) and Template Resources (500), both non-adjustable — and also
Template Description Length (1,024 bytes), which the persist procedure
relies on. The 51,200-byte inline TemplateBody cap is not published as a
service quota; take it from the CloudFormation quotas
documentation (opens in a new tab). When the margin matters, confirm with:
aws service-quotas list-service-quotas --service-code cloudformation \
--query "Quotas[?starts_with(QuotaName, 'Template')].[QuotaName,Value,Unit]"When adding or modifying resources — especially in a large template:
wc -c <template> on
Unix/macOS or Git Bash, or (Get-Item <template>).Length in PowerShell) and
compare it against the 1,048,576-byte limit; note the remaining headroom and
the resource count against the 500 cap.Metadata."com.aws.cloudformation.Context" you are required to attach. If
the addition would push the template over the limit, do NOT blindly append.Metadata."com.aws.cloudformation.Context" (collapse long why/rationale
prose into terse caveman shorthand; keep must constraints intact).AWS::CloudFormation::Stack) or a
CloudFormation module, or relocate bulky static content (e.g., large inline
code) to S3. Preserve Metadata."com.aws.cloudformation.Context" on the
extracted resources.CreateStack/UpdateStack (e.g., “Template body is too
long” / “Template format error: number of resources exceeds maximum”).Key principle: Context is mandatory, but so is staying under the size limit. When both cannot fit, intelligently adjust existing and new context (condense, prioritize, or relocate) — never choose between blindly adding and dropping context.