feat(cloudformation): add AWS::Events::Rule provisioning support (#261)

CloudFormation stacks containing AWS::Events::Rule resources were marked
as CREATE_COMPLETE but no actual EventBridge rules or targets were created.
This happened because the resource type was falling through to the default
stub handler in CloudFormationResourceProvisioner.

Changes:
- Inject EventBridgeService into CloudFormationResourceProvisioner
- Add provisioning handler for AWS::Events::Rule that creates actual rules
  via EventBridgeService.putRule() and wires inline targets via putTargets()
- Add delete handler that removes targets before deleting the rule
- Support auto-generated rule names when Name property is omitted
- Handle EventPattern, ScheduleExpression, State, Description, RoleArn,
  and inline Targets from CloudFormation properties
This commit is contained in:
Victor Sanchez Velasco authored and GitHub committed 2026-04-08 16:16:54 -05:00
1 parent 3d2cc1c95e
commit c475e52984
2 files changed
+267 -1

No files matched your search

@@ -2,6 +2,9 @@ package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
import io.github.hectorvent.floci.services.eventbridge.model.RuleState;
import io.github.hectorvent.floci.services.eventbridge.model.Target;
import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;
import io.github.hectorvent.floci.services.dynamodb.model.GlobalSecondaryIndex;
import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;
@@ -45,13 +48,15 @@ public class CloudFormationResourceProvisioner {
private final SsmService ssmService;
private final KmsService kmsService;
private final SecretsManagerService secretsManagerService;
private final EventBridgeService eventBridgeService;
@Inject
public CloudFormationResourceProvisioner(S3Service s3Service, SqsService sqsService,
SnsService snsService, DynamoDbService dynamoDbService,
LambdaService lambdaService, IamService iamService,
SsmService ssmService, KmsService kmsService,
SecretsManagerService secretsManagerService) {
SecretsManagerService secretsManagerService,
EventBridgeService eventBridgeService) {
this.s3Service = s3Service;
this.sqsService = sqsService;
this.snsService = snsService;
@@ -61,6 +66,7 @@ public class CloudFormationResourceProvisioner {
this.ssmService = ssmService;
this.kmsService = kmsService;
this.secretsManagerService = secretsManagerService;
this.eventBridgeService = eventBridgeService;
}
/**
@@ -99,6 +105,7 @@ public class CloudFormationResourceProvisioner {
case "AWS::ECR::Repository" -> provisionEcrRepository(resource, properties, engine, stackName);
case "AWS::Route53::HostedZone" -> provisionRoute53HostedZone(resource, properties, engine);
case "AWS::Route53::RecordSet" -> provisionRoute53RecordSet(resource, properties, engine);
case "AWS::Events::Rule" -> provisionEventBridgeRule(resource, properties, engine, region, stackName);
default -> {
LOG.debugv("Stubbing unsupported resource type: {0} ({1})", resourceType, logicalId);
resource.setPhysicalId(logicalId + "-" + UUID.randomUUID().toString().substring(0, 8));
@@ -131,6 +138,7 @@ public class CloudFormationResourceProvisioner {
case "AWS::KMS::Alias" -> kmsService.deleteAlias(physicalId, region);
case "AWS::SecretsManager::Secret" ->
secretsManagerService.deleteSecret(physicalId, null, true, region);
case "AWS::Events::Rule" -> deleteEventBridgeRuleSafe(physicalId, region);
default -> LOG.debugv("Skipping delete of unsupported resource type: {0}", resourceType);
}
} catch (Exception e) {
@@ -558,6 +566,67 @@ public class CloudFormationResourceProvisioner {
r.getAttributes().put("Outputs.BootstrapVersion", "21");
}
// ── EventBridge ─────────────────────────────────────────────────────────
private void provisionEventBridgeRule(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
String region, String stackName) {
String ruleName = resolveOptional(props, "Name", engine);
if (ruleName == null || ruleName.isBlank()) {
ruleName = generatePhysicalName(stackName, r.getLogicalId(), 64, false);
}
String busName = resolveOptional(props, "EventBusName", engine);
String description = resolveOptional(props, "Description", engine);
String roleArn = resolveOptional(props, "RoleArn", engine);
String scheduleExpression = resolveOptional(props, "ScheduleExpression", engine);
String eventPattern = null;
if (props != null && props.has("EventPattern") && !props.get("EventPattern").isNull()) {
JsonNode patternNode = engine.resolveNode(props.get("EventPattern"));
eventPattern = patternNode.toString();
}
String stateStr = resolveOptional(props, "State", engine);
RuleState state = "DISABLED".equals(stateStr) ? RuleState.DISABLED : RuleState.ENABLED;
var rule = eventBridgeService.putRule(ruleName, busName, eventPattern, scheduleExpression,
state, description, roleArn, Map.of(), region);
r.setPhysicalId(ruleName);
r.getAttributes().put("Arn", rule.getArn());
// Provision inline targets
if (props != null && props.has("Targets")) {
List<Target> targets = new ArrayList<>();
for (JsonNode targetNode : props.get("Targets")) {
JsonNode resolved = engine.resolveNode(targetNode);
String targetId = resolved.path("Id").asText(null);
String targetArn = resolved.path("Arn").asText(null);
String input = resolved.path("Input").asText(null);
String inputPath = resolved.path("InputPath").asText(null);
if (targetId != null && targetArn != null) {
targets.add(new Target(targetId, targetArn, input, inputPath));
}
}
if (!targets.isEmpty()) {
eventBridgeService.putTargets(ruleName, busName, targets, region);
}
}
}
private void deleteEventBridgeRuleSafe(String ruleName, String region) {
try {
// Remove all targets before deleting the rule
var targets = eventBridgeService.listTargetsByRule(ruleName, null, region);
if (!targets.isEmpty()) {
List<String> targetIds = targets.stream().map(Target::getId).toList();
eventBridgeService.removeTargets(ruleName, null, targetIds, region);
}
eventBridgeService.deleteRule(ruleName, null, region);
} catch (Exception e) {
LOG.debugv("Could not delete EventBridge rule {0}: {1}", ruleName, e.getMessage());
}
}
// ── Helpers ───────────────────────────────────────────────────────────────
private void provisionCdkMetadata(StackResource r) {