Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(cloudformation): honor the ${!Literal} escape in Fn::Sub (#1484)

maximooseminec6eb897c4bce
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 36e204d5..bdb6c646 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,8 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Fixed
+- **CloudFormation — `Fn::Sub` honors the `${!Literal}` escape** — the leading `!` was not recognized, so the name fell through every lookup and rendered as `!Literal` with the braces consumed; `${!Literal}` now renders as the literal `${Literal}` and no longer registers a dependency on a resource of that name. An `AWS::IoT::Policy` pinned to `${!iot:Connection.Thing.ThingName}` deployed with a document matching nothing, so the policy denied every connect, publish and subscribe while the stack still reported `CREATE_COMPLETE`. Reported by @iot-rocket.
### Added
- **EC2 — opt-in launch options for instance containers (`EC2_DOCKER_FLAGS`)** — a registered image that boots an operating system (systemd as PID 1) needs container options the fixed launch arguments cannot express: unprivileged, `unshare` fails with `Operation not permitted` and the instance terminates at boot. `EC2_DOCKER_FLAGS` now takes a docker-CLI-style string (`--privileged`, `--cap-add`, `-e`, `-v`, `--tmpfs`, `--add-host`, `-m`, `--shm-size`) applied to every instance container; unset, nothing changes. `--init` is refused — instance containers always run with init. Contributed by @iot-rocket.
### Fixed
diff --git a/ministack/services/cloudformation/engine.py b/ministack/services/cloudformation/engine.py
index 3807cb43..6851f1d8 100644
--- a/ministack/services/cloudformation/engine.py
+++ b/ministack/services/cloudformation/engine.py
@@ -385,6 +385,9 @@ def _resolve_refs(value, resources, params, conditions, mappings,
def _sub_replace(match):
var = match.group(1)
+ # ${!Literal} escape: emit ${Literal} without substituting
+ if var.startswith("!"):
+ return "${" + var[1:] + "}"
# Check explicit var map first
if var in resolved_map:
return str(resolved_map[var])
@@ -574,6 +577,8 @@ def _extract_deps(resource_def: dict, all_resource_names: set) -> set:
template_str = sub_val[0] if isinstance(sub_val, list) else sub_val
for match in re.finditer(r"\$\{([^}]+)\}", str(template_str)):
var = match.group(1)
+ if var.startswith("!"):
+ continue
base = var.split(".")[0]
if base in all_resource_names:
deps.add(base)
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index df8950fc..473678ee 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -1267,6 +1267,40 @@ def test_cfn_fn_sub(cfn, ssm):
val = ssm.get_parameter(Name="cfn-t06-param")["Parameter"]["Value"]
assert val == "cfn-t06-src-replica"
+
+def test_cfn_fn_sub_literal_escape(cfn, ssm):
+ """`${!Literal}` is Fn::Sub's escape for emitting `${Literal}` verbatim —
+ the way an IoT policy carries `${iot:Connection.Thing.ThingName}` through a
+ template. The name must not be looked up, even when it matches a resource,
+ and the surrounding substitutions must still resolve."""
+ template = {
+ "AWSTemplateFormatVersion": "2010-09-09",
+ "Resources": {
+ "Plain": {
+ "Type": "AWS::S3::Bucket",
+ "Properties": {"BucketName": "cfn-sub-escape-src"},
+ },
+ "Param": {
+ "Type": "AWS::SSM::Parameter",
+ "Properties": {
+ "Name": "/cfn-sub-escape/literal",
+ "Type": "String",
+ "Value": {
+ "Fn::Sub": "client/${!iot:Connection.Thing.ThingName} "
+ "and ${!Plain} in ${AWS::Region}"
+ },
+ },
+ },
+ },
+ }
+ cfn.create_stack(StackName="cfn-sub-escape", TemplateBody=json.dumps(template))
+ _wait_stack(cfn, "cfn-sub-escape")
+
+ val = ssm.get_parameter(Name="/cfn-sub-escape/literal")["Parameter"]["Value"]
+ assert val == ("client/${iot:Connection.Thing.ThingName} "
+ "and ${Plain} in us-east-1")
+
+
def test_cfn_multi_resource_dependencies(cfn, iam, lam):
template = {
"AWSTemplateFormatVersion": "2010-09-09",