Skip to content

Why is EventBridge getting Invalid input for target message from Systems Manager Document

1

I have tried everything I can find but cannot figure why it is not working. When autoscaling terminates an ec2 I am wanting eventbridge to run a systems manager document. The event bridge is running targets as I have cloud watch being triggered and a DLQ setup. Can someone please tell me why I am getting the Invalid input for target?

DLQ error message.
ERROR_CODE
String
INVALID_JSON
ERROR_MESSAGE
String
Invalid input for target

Input Transformer Setup:
Input Path:
{ "instanceId":"$.detail.EC2InstanceId" }

Template:
{ "instanceId": "<instanceId>" }

Test Output:
{ "instanceId": "I-123456789" }

Target Key.
InstanceIds.
$.detail.EC2InstanceId.

This is my SSM Document (Simplified just to get it to work)

---
schemaVersion: '2.2'
description: 'Backup EC2 logs to S3'
parameters:
  instanceId:
    type: String
    description: "EC2 instance ID"
mainSteps:
  - action: aws:runShellScript
    name: backupLogs
    inputs:
      runCommand:
        - |
          #!/bin/bash
          set -e  # Exit on error
          
          # Get instance ID from parameter
          INSTANCE_ID='{{ instanceId }}'
          #LOG_DIR="/var/log"
          #S3_BUCKET_NAME="asg-log-backup"
          TIMESTAMP=$(date +%Y-%m-%d-%H-%M-%S)
          TEMP_DIR="/tmp/logs-backup-${TIMESTAMP}"
          
          # Create temp directory
          mkdir -p "${TEMP_DIR}"
          
           echo "sometext" > "${TEMP_DIR}/testtext.txt"
          echo "${INSTANCE_ID}" > "${TEMP_DIR}/instanceid.txt"
         

Thank You

1 Answer
0

The issue you're experiencing with EventBridge and the "Invalid input for target" error message when trying to run a Systems Manager Document is likely due to a mismatch between the input format expected by the Systems Manager Document and the input provided by EventBridge.

Based on your setup, there are a few potential issues:

  1. Input Transformer: Your input transformer is correctly extracting the EC2 instance ID, but it's wrapping it in a JSON object. Systems Manager might be expecting a different format.

  2. Target Configuration: The "Target Key" you've set as "InstanceIds" suggests you're trying to use the EC2 Run Command target type, which has specific input requirements.

  3. Document Parameters: Your Systems Manager Document expects a parameter named "instanceId", but the way you're providing this parameter might not be correct.

To resolve this issue, try the following:

  1. Modify your Input Transformer template to match the exact format expected by Systems Manager. Instead of:

    { "instanceId": "<instanceId>" }

    Try using:

    { "InstanceIds": ["<instanceId>"] }

  2. In the EventBridge rule target configuration, make sure you're using the "AWS Systems Manager Run Command" target type, not the generic "AWS Systems Manager" type.

  3. If you're using the Run Command target type, you don't need to specify the instance ID in the input transformer. Instead, use the "InstanceIds" field in the target configuration to specify the instance ID dynamically.

  4. Ensure that the IAM role associated with your EventBridge rule has the necessary permissions to invoke the Systems Manager Document and perform actions on the target EC2 instances.

  5. Double-check that your Systems Manager Document is correctly formatted and that the parameter names in the document match the input you're providing.

If these steps don't resolve the issue, you may need to review the EventBridge rule configuration and Systems Manager Document in more detail to ensure they're properly aligned.
Sources
Event bus targets in Amazon EventBridge - Amazon EventBridge
Troubleshoot Amazon EventBridge rule with ECS | AWS re:Post
Amazon EventBridge input transformation - Amazon EventBridge

answered 2 years ago

EXPERT

reviewed 2 years 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.