Skip to content

difficulties with EventBridge in CF template, unclear what causes the error

0

I am caught in a loop of contradictory CloudFormation errors while trying to create a simple AWS EventBridge rule that logs all events from a specific source to a CloudWatch Logs group. I have tried multiple, documented approaches, and each one fails with a different, seemingly conflicting error message.

The Goal:

Create an AWS::Events::Rule that captures all events where the source is "com.cth.rets.delta" and sends them to a CloudWatch Logs group.

The Core template.yaml Snippet:

Resources:
  RetsEventBus:
    Type: AWS::Events::EventBus
    Properties:
      Name: !Sub "${EnvironmentPrefix}RetsEventBus"

  RetsEventBusLogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub "/aws/events/eventbus/${EnvironmentPrefix}RetsEventBus"

  RetsEventBusLoggingRule:
    Type: AWS::Events::Rule
    Properties:
      EventBusName: !Ref RetsEventBus
      EventPattern:
        source:
          - "com.cth.rets.delta"
      Targets:
        - Arn: !GetAtt RetsEventBusLogGroup.Arn
          Id: "RetsEventBusLogGroupTarget"
          # RoleArn is included or excluded based on the attempt
          RoleArn: !GetAtt RetsEventBusRuleRole.Arn 

  RetsEventBusRuleRole:
    Type: AWS::IAM::Role
    Properties:
      # ... (Standard AssumeRolePolicy for events.amazonaws.com)
      Policies:
        # ... (Policy with logs:PutLogEvents permission for the Log Group)

The Cycle of Failures:

I have been through a series of attempts, each leading to a different failure. Here is the timeline of errors:

Attempt 1: No RoleArn on the Target

Initially, I did not include a RoleArn on the target, assuming a service-linked role would handle permissions.

  • Result: UPDATE_FAILED. The root cause was assumed to be missing permissions.

Attempt 2: Added RoleArn to the Target

I added an explicit AWS::IAM::Role with logs:PutLogEvents permissions and referenced it in the RoleArn property of the target.

  • EventPattern used:
    EventPattern:
      source:
        - "com.cth.rets.delta"
  • Result: CREATE_FAILED with the following error (NOTE: Account number has been anonymized for security reasons)

    "RoleArn is not supported for target arn:aws:logs:us-west-2:700520812014:log-group:/aws/events/eventbus/prod-RetsEventBus:*. (Service: EventBridge, Status Code: 400)"

This error suggests that for a CloudWatch Logs target, a RoleArn should not be specified, and permissions must be handled by a resource-based policy on the Log Group itself.

Attempt 3: Removed RoleArn and Added a Resource-Based Policy

Based on the previous error, I removed the RoleArn from the target and added an AWS::Logs::ResourcePolicy to grant EventBridge permission to write to the log group.

  • Result: CREATE_FAILED with the error:

    "Parameter(s) EventPattern or ScheduleExpression must be specified. (Service: EventBridge, Status Code: 400)"

This error occurred even though a valid EventPattern was present in the template. This seems to indicate that the template is being processed incorrectly or that there is a deeper issue.

The Contradiction:

  • The service tells me RoleArn is not supported for my target.
  • When I remove RoleArn, the service tells me I'm missing the EventPattern, even though it's clearly there.

This has created a catch-22 situation where I cannot satisfy both conditions simultaneously. I have validated the YAML syntax, the EventPattern structure, and the IAM role definitions repeatedly.

The Question:

  1. What is the correct way to configure an AWS::Events::Rule with a CloudWatch Logs target in CloudFormation to avoid this contradictory error loop?
  2. Why would CloudFormation report that RoleArn is not supported, and then, upon its removal, fail to recognize a valid EventPattern?
  3. Is there a known issue or a subtle nuance with the AWS SAM CLI, CloudFormation, or EventBridge that could cause this behavior?

Any insight into this frustrating loop would be greatly appreciated.

asked a year ago315 views

1 Answer
0
Accepted Answer

Based on your description, you're encountering a common issue with EventBridge rules targeting CloudWatch Logs in CloudFormation templates. Let me clarify how this should work:

When CloudWatch Logs is the target of an EventBridge rule, you don't need to specify a RoleArn in the target configuration. Instead, CloudWatch Logs requires a resource-based policy that enables EventBridge to write to it. This is why you received the error stating "RoleArn is not supported for target" when you included it.

For your second error about missing EventPattern, this could be due to a few issues:

  1. YAML indentation problems - EventPattern needs to be properly indented under Properties
  2. A potential issue with how the template is being processed

Here's the correct approach:

  1. Remove the RoleArn from the target configuration
  2. Create a resource-based policy for CloudWatch Logs
  3. Ensure your EventPattern is correctly formatted

Your EventBridge rule should look like this:

RetsEventBusLoggingRule:
Type: AWS::Events::Rule
Properties:
EventBusName: !Ref RetsEventBus
EventPattern:
source:
- "com.cth.rets.delta"
Targets:
- Arn: !GetAtt RetsEventBusLogGroup.Arn
Id: "RetsEventBusLogGroupTarget"

And add a resource policy:

LogsResourcePolicy:
Type: AWS::Logs::ResourcePolicy
Properties:
PolicyName: EventBridgeToCloudWatchLogs
PolicyDocument: !Sub |
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EventBridgeToCloudWatchLogs",
"Effect": "Allow",
"Principal": {"Service": "events.amazonaws.com"},
"Action": ["logs:PutLogEvents","logs:CreateLogStream"],
"Resource": "${RetsEventBusLogGroup.Arn}"
}
]
}

If you're still encountering the "Parameter(s) EventPattern or ScheduleExpression must be specified" error despite having a valid EventPattern, try these troubleshooting steps:

  1. Validate your CloudFormation template using the AWS CLI command aws cloudformation validate-template
  2. Try simplifying your template to just the EventBridge rule and its dependencies to isolate the issue
  3. Check if there are any transform operations in your template that might be affecting how EventPattern is processed

The resource-based policy approach is the correct way to handle permissions for EventBridge to write to CloudWatch Logs, not an IAM role attached to the target.
Sources
EventBridge Rule Target ID | AWS re:Post
How does EventBridge Pipes have permissions to log to CloudWatch? | AWS re:Post

answered a year ago

EXPERT

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.