You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2025-06-15-AWS-SQS-Pipe-ECS.md
+116-1Lines changed: 116 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,4 +19,119 @@ header:
19
19
20
20
Today I was working on a integration to process message from an AWS Simple Queueing Services (SQS) Queue to a backend services. The specifics of this backend services doesn't really matter for this post, the only thing relevant is that it using the old-school Windows Communication Framework (WCF). With a custom .NET application written in the beloved C# language, we can communicate with the backend.
21
21
22
-
This integration start with a third party putting a message on our queue. Because the amound of message we think this intergration would process we would like to make this integration serverless.
22
+
<!--more-->
23
+
24
+
This integration start with a third party putting a message on our queue. Because the amount of message we think this integration would process we would like to make this integration serverless. This means no service running 24-7 that is constantly polling for work to do, but a process that is triggered when a new message has arrived.
25
+
26
+
## Integration overview
27
+
28
+

29
+
30
+
In this post I will show you how to use AWS SQS and EventBridge Pipes to send messages to ECS. The integration consists of the following components:
31
+
-**SQS Queue**: The queue where the third party put the messages. Nothing special about this queue, just a standard SQS queue.
32
+
-**EventBridge Pipe**: The pipe that connects the SQS queue to the ECS task.
33
+
-**ECS Task**: The task that processes the messages from the SQS queue. This task is running a custom .NET application that communicates with the backend service using WCF.
34
+
35
+
### EventBridge Pipe
36
+
37
+
Creating an EventBridge Pipe is a [straightforward process](https://docs.aws.amazon.com/eventbridge/latest/userguide/pipes-get-started.html). The pipe will listen to the SQS queue and trigger the ECS task when a new message arrives. When creating the pipe, you need to specify the source (SQS queue) and the target (ECS task). You can also configure the pipe to filter messages or enrich messages based on certain criteria, but for this example, we will keep it simple and process all messages as they arrive.
38
+
39
+

40
+
41
+
### CloudFormation Template
42
+
43
+
```yaml
44
+
AWSTemplateFormatVersion: '2010-09-09'
45
+
Description: CloudFormation template for EventBridge PipeSQS-Pipe-ECS-Integration
46
+
Resources:
47
+
MySQSQueue:
48
+
Type: AWS::SQS::Queue
49
+
Properties:
50
+
QueueName: MyQueue
51
+
52
+
MyECSCluster:
53
+
Type: AWS::ECS::Cluster
54
+
Properties:
55
+
ClusterName: MyECSCluster
56
+
57
+
MyECSTaskDefinition:
58
+
Type: AWS::ECS::TaskDefinition
59
+
Properties:
60
+
Family: MyTaskDefinition
61
+
ContainerDefinitions:
62
+
- Name: MyContainer
63
+
Image: my-docker-image
64
+
Memory: 512
65
+
Cpu: 256
66
+
Essential: true
67
+
68
+
MyEventBridgePipe:
69
+
Type: AWS::Events::Pipe
70
+
Properties:
71
+
Name: MyQueueToECSPipe
72
+
DesiredState: RUNNING
73
+
Source: !GetAtt MySQSQueue.Arn
74
+
Target: !GetAtt MyECSCluster.Arn
75
+
TargetParameters:
76
+
EcsTaskParameters:
77
+
TaskDefinitionArn: MyECSTaskDefinition.Arn
78
+
TaskCount: 1
79
+
NetworkConfiguration:
80
+
awsvpcConfiguration: {}
81
+
EnableECSManagedTags: true
82
+
EnableExecuteCommand: false
83
+
```
84
+
85
+
This (partial complete) CloudFormation template creates the necessary resources for the integration. It creates an SQS queue, an ECS cluster, an ECS task definition, and an EventBridge Pipe that connects the SQS queue to the ECS task.
86
+
87
+
This Pipe will automatically trigger the ECS task when a new message arrives in the SQS queue. The ECS task will then start up. It doesn't receive anything from the SQS queue, it will just start up and run the code in the container.
88
+
89
+
If the Pipe is configured correctly, it will start the ECS task when a new message arrives in the SQS queue. The message is not passed to the ECS task, but the task will start up and run the code in the container. When the container is started, the message is already removed from the SQS queue. This means that the ECS task will not receive the message automatically.
90
+
91
+
You can pass the message to the ECS task by using `$.body` in the template. This will pass the message body to the ECS task as an environment variable. You can then read this environment variable in your code and process the message accordingly.
92
+
93
+
```yaml
94
+
MyEventBridgePipe:
95
+
Type: AWS::Events::Pipe
96
+
Properties:
97
+
TargetParameters:
98
+
EcsTaskParameters:
99
+
... # other parameters
100
+
Overrides:
101
+
ContainerOverrides:
102
+
- Name: MyContainer
103
+
Environment:
104
+
- Name: MESSAGE_BODY
105
+
Value: "$.body"
106
+
```
107
+
108
+
It is also possible to pass the message body as a startup argument to the ECS task. In the `ContainerOverrides` section of the `EcsTaskParameters`, you can specify the command to run when the container starts. This allows you to pass the message body as an argument to your application.
109
+
110
+
```yaml
111
+
MyEventBridgePipe:
112
+
Type: AWS::Events::Pipe
113
+
Properties:
114
+
TargetParameters:
115
+
EcsTaskParameters:
116
+
... # other parameters
117
+
Overrides:
118
+
ContainerOverrides:
119
+
- Name: !Ref ContainerName
120
+
Command:
121
+
- "dotnet"
122
+
- "/app/Processor.Host.dll"
123
+
- "--body"
124
+
- "$.body"
125
+
```
126
+
127
+
### No resilience integration
128
+
129
+
When the Pipe can not start the ECS task because, for example: incorrect configuration, unknown task definition, or not the right permission to do so, it will not delete the message from the SQS queue. This means that if the ECS task fails to start, the message will remain in the SQS queue and can be processed again later. However, if the Pipe successfully starts the ECS task, it will delete the message from the SQS queue.
130
+
131
+
> **Warning**: When the Pipe has successfully started the ECS task, it will not wait for the task to complete but it will delete the message from the SQS queue. This means that if the ECS task fails to process the message, the message will be lost. This will make this integration not resilient, a failure in the ECS task will not result in the message being retried.
132
+
133
+
## Conclusion
134
+
135
+
This is not a resilient integration, but a simple way to start a process based on a message that is send to an SQS queue using EventBridge Pipes and ECS. The integration is serverless, meaning that there is no need to run a service 24-7 that is constantly polling for work to do. Instead, the ECS task is triggered when a new message arrives in the SQS queue, making it a cost-effective solution for processing messages.
136
+
137
+

0 commit comments