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 個回答
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

已回答 7 個月前
  • 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.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南