feat: add support of Cloudformation mapping and Fn::FindInMap function (#101)

This commit is contained in:
Ilya Bryzgalov authored and GitHub committed 2026-04-01 01:24:47 -05:00
1 parent 48bee44634
commit eef66983d9
3 files changed
+46 -3

No files matched your search

@@ -200,6 +200,10 @@ public class CloudFormationService {
// Resolve conditions first
Map<String, Boolean> conditions = resolveConditions(template, resolvedParams, stack, region);
// Mappings
Map<String, JsonNode> mappings = new HashMap<>();
template.path("Mappings").fields().forEachRemaining(e -> mappings.put(e.getKey(), e.getValue()));
// Process resources in order
JsonNode resources = template.path("Resources");
Map<String, String> physicalIds = new LinkedHashMap<>();
@@ -224,7 +228,7 @@ public class CloudFormationService {
CloudFormationTemplateEngine engine = new CloudFormationTemplateEngine(
config.defaultAccountId(), region, stack.getStackName(),
stack.getStackId(), resolvedParams, physicalIds, resourceAttrs, conditions, objectMapper);
stack.getStackId(), resolvedParams, physicalIds, resourceAttrs, conditions, mappings, objectMapper);
StackResource resource = stack.getResources().get(logicalId);
if (resource == null) {
@@ -251,7 +255,7 @@ public class CloudFormationService {
stack.getOutputs().clear();
CloudFormationTemplateEngine finalEngine = new CloudFormationTemplateEngine(
config.defaultAccountId(), region, stack.getStackName(),
stack.getStackId(), resolvedParams, physicalIds, resourceAttrs, conditions, objectMapper);
stack.getStackId(), resolvedParams, physicalIds, resourceAttrs, conditions, mappings, objectMapper);
JsonNode outputs = template.path("Outputs");
if (outputs.isObject()) {
@@ -27,6 +27,7 @@ public class CloudFormationTemplateEngine {
private final Map<String, String> physicalIds;
private final Map<String, Map<String, String>> resourceAttributes;
private final Map<String, Boolean> conditions;
private final Map<String, JsonNode> mappings;
private final ObjectMapper objectMapper;
CloudFormationTemplateEngine(String accountId, String region, String stackName, String stackId,
@@ -34,6 +35,7 @@ public class CloudFormationTemplateEngine {
Map<String, String> physicalIds,
Map<String, Map<String, String>> resourceAttributes,
Map<String, Boolean> conditions,
Map<String, JsonNode> mappings,
ObjectMapper objectMapper) {
this.accountId = accountId;
this.region = region;
@@ -43,6 +45,7 @@ public class CloudFormationTemplateEngine {
this.physicalIds = physicalIds;
this.resourceAttributes = resourceAttributes;
this.conditions = conditions;
this.mappings = mappings;
this.objectMapper = objectMapper;
}
@@ -87,6 +90,9 @@ public class CloudFormationTemplateEngine {
if (node.has("Fn::ImportValue")) {
return resolve(node.get("Fn::ImportValue"));
}
if (node.has("Fn::FindInMap")) {
return resolveFindInMap(node.get("Fn::FindInMap"));
}
}
return node.asText();
}
@@ -245,4 +251,24 @@ public class CloudFormationTemplateEngine {
LOG.debugv("Unresolved GetAtt: {0}.{1}", logicalId, attrName);
return logicalId + "." + attrName;
}
private String resolveFindInMap(JsonNode node) {
if (node.isArray()) {
String mapName = resolve(node.get(0));
String topLvlName = resolve(node.get(1));
String secondLvlName = resolve(node.get(2));
JsonNode map = mappings.get(mapName);
if (map != null && map.isObject()) {
JsonNode topLvl = map.get(topLvlName);
if (topLvl != null && topLvl.isObject()) {
JsonNode secondLvl = topLvl.get(secondLvlName);
if (secondLvl != null) {
return resolve(secondLvl);
}
}
}
}
return "";
}
}
@@ -28,11 +28,24 @@ class CloudFormationIntegrationTest {
void createStack_withS3AndSqs() {
String template = """
{
"Mappings": {
"Env": {
"us-east-1": {
"Name": "test"
}
}
},
"Resources": {
"MyBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": "cf-test-bucket"
"BucketName": {
"Fn::Sub": ["cf-${env}-bucket", {
"env": {
"Fn::FindInMap": ["Env", { "Ref" : "AWS::Region" }, "Name"]
}
}]
}
}
},
"MyQueue": {