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.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠