Send message from SQS to ECS task via Event bridge Rule

0

I have setup the SQS and event bridge rule. On receiving the message the ECS task is getting triggered. But I am not able to figure out how to send the message body to the task definiton argument.

When I was doing it with the code, I was using ECS run-task to do it by overriding the command.

response = ecs.run_task(
        # edit
        cluster=settings.ECS_CLUSTER,
        taskDefinition=settings.ECS_TASK_DEFINITION,
        count=1,
        enableECSManagedTags=True,
        enableExecuteCommand=True,
        launchType="FARGATE",
        networkConfiguration={
            "awsvpcConfiguration": {
                "subnets": settings.subnets,
                "securityGroups": [
                    settings.security_groups,
                ],
            },
        },
        overrides={
            "containerOverrides": [
                {
                    "command": ["python3", task_path, "--request", payload],
                    "name": settings.ECS_TASK_CONTAINER_NAME,
                },
            ],
        },
    )

I want to pass my SQS message body to the task defintion as the input for the --request argument

1 Answer
0

Hi, When using Amazon EventBridge Pipes you should be able to pass the body of the SQS payload using the "$.body" expression directly in your ECS Run Task Target Definition.

https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-pipes-input-transformation.html#input-transform-implicit

You will need to replace your settings variables with values from CloudFormation/CDK and convert the existing call to a JSON. Then use the Input Transformer to extract the SQS Message body and pass it to the ECS Run Task Target.

Something like this:

       {
	"cluster": "cluster-name",
	"taskDefinition": "task-definition",
	"count": 1,
	"enableECSManagedTags": "True",
	"enableExecuteCommand": "True",
	"launchType": "FARGATE",
	"networkConfiguration": {
		"awsvpcConfiguration": {
			"subnets": [
				"subnets"
			],
			"securityGroups": [
				"security-groups-ids"
			]
		}
	},
	"overrides": {
		"containerOverrides": [
			{
				"command": [
					"python3",
					"task_path",
					"--request",
					<$.body>
				],
				"name": "container-task-name"
			}
		]
	}
}

The following github sample provides CDK to deploy this scenario: https://github.com/rhlarora84/sqs-fargate-eventbridge-pipe/blob/main/lib/app-stack.ts

answered 6 months ago
  • Thank you for your answer, the template which you have given is for the event bridge pipe. I was asking how to do the same overriding in the Event bridge rule.

    "InputTransformer": {
                  "InputPathsMap": {
                    "payload": "$.body"
                  },
                  "InputTemplate": "{ \"request\": <payload> }"
                }
    

    in event bridge rule there is Input transformer and in documentation it is mentioned that it will pass the message but how to receive in the dockerfile or the python file as in case of event bridge pipe the CMD of dockerfile is override and the message is passed as an argument.

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.

Guidelines for Answering Questions