4

I am looking at a use case for fargate where I have some external trigger (perhaps a CloudWatch event) that triggers a Fargate task which opens a websocket connection, listens for a few hours and writes data to S3 and then ultimately closes the websocket after a few hours.

The websocket could either be closed by some external trigger, after a certain # of hours (generic timer) OR when it receives a specific message from the socket it closes.

Is this the right use case for fargate? The external trigger would pass in the websocket connection string and the secret auth token to authenticate could be stored with AWS KMS.

External trigger -> opens ws:// connection (through fargate) -> writes to S3

If this is the right use case, any advice on how to approach this? If this isn't the right use case, is there another service that is better for a persistent connection that is only a few hours?

WIT
  • 1,043
  • 2
  • 15
  • 32

1 Answers1

7

Fargate is just a managed service to run containers. The is no limitation on how long a task will run. You can tread it as an EC2 instance managed by AWS if you plan to deploy only docker images. Check the common use cases for Fargate. From your question, it's not clear if you need a different workload of occasional bursts. To you need to scale fast or you run only one task at a time.

On the other side, EC2 instances have various types and, for example, t3.small will be cheaper comparing to Fargate pricing. Also, mind free-tier EC2 instance types and reserved instances. Compute Savings Plan is available for both Fargate and EC2. You can launch and shout down EC2 instances on schedule as well.

In summary, choose Fargate if you don't want to spend time on maintenance and need a scalable solution out of the box; choose EC2 if you need only one websocket connection and would like to make it cheaper. Both approaches handle long websocket connections well unless you use spot instances.

Yann
  • 2,426
  • 1
  • 16
  • 33
  • Can you pass dynamic parameters to a fargate task? – WIT Jan 29 '20 at 15:16
  • Sorry, I'm not sure I understand what you mean by dynamic parameters, but tasks in Fargate have the same `docker run`. You can pass `command` https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#container_definition_environment Check this answer for a code example https://stackoverflow.com/a/57616235/6628583 – Yann Jan 29 '20 at 15:40
  • understood, last question and I am not sure if you know — no worries if not. If I was listening to a websocket for a few hours, kicking it off w/ fargate on an EC2 instance, would you recommend writing to S3 or streaming to kinesis/S3? I am not sure what the kinesis piece gets me in this case. – WIT Jan 29 '20 at 16:58
  • You can put the data stream directly to an S3 object using multipart upload https://docs.aws.amazon.com/AmazonS3/latest/dev/uploadobjusingmpu.html minimum size of a part is 5MB – Yann Jan 29 '20 at 22:20