Running a 24/7 Service with ECS Fargate

pouyapouya
3 min read

That’s what I want to share with you in this article.
We’ve all faced the pain of working with WebSockets, rate limits, or trying to keep things updated just fast enough to meet the user’s expectations.

Why not just use Lambda?

Great question!
You’re right — in many cases, AWS Lambda is a fantastic option. It’s easy to deploy, easy to debug, and perfect for a lot of scenarios.

But… not always.
When?
Well, Lambda can become expensive when you have high-frequency updates or need to handle lots of requests — say, 1000 requests every 10 seconds. In those cases, Lambda might choke, costs might skyrocket, and you still wouldn’t hit your goal.

Why not batch jobs?

Another good point — but then it’s not really cost-effective either, and you lose the “almost real-time” responsiveness we’re aiming for.

So what’s the solution?

That’s where the magic of Fargate and containers comes in.

I encourage you to check out what ECS (Elastic Container Service) and ECR (Elastic Container Registry) are, but here’s the quick version:

  • ECR is where you upload your Docker image.

  • ECS is where you define and run your services, tasks, schedules, and crons — without worrying about servers.

A Simple Example

Imagine you have a service that needs to connect to another API and fetch data continuously.
If the API doesn’t have a rate limit, you can just loop through the requests, process the data, and then send it off to a database or another service to update something.

That’s exactly what I did in one of my projects — and Fargate made it easy and affordable.

How to Push Your Docker Image to ECR

Here’s a quick way to build and push your Docker image using the CLI:

Build your docker image

docker build -t my-service .

Tag the image for ECR

docker tag my-service:latest .dkr.ecr..amazonaws.com/my-service:latest

Login to ECR

aws ecr get-login-password --region | docker login --username AWS --password-stdin .dkr.ecr..amazonaws.com

Push the image

docker push .dkr.ecr..amazonaws.com/my-service:latest

That’s it — your image is now in ECR.

How to Run Your ECR Image on Fargate (with AWS CLI)

Once your image is in ECR, you can create an ECS task definition and run it on Fargate directly from the CLI.

Here’s a simplified example:

1️⃣ Register the task definition:

aws ecs register-task-definition \
  --cli-input-json file://task-definition.json

The task-definition.json might look like this:

{
  "family": "my-service",
  "networkMode": "awsvpc",
  "requiresCompatibilities": ["FARGATE"],
  "cpu": "256",
  "memory": "512",
  "containerDefinitions": [
    {
      "name": "my-service",
      "image": "<account-id>.dkr.ecr.<region>.amazonaws.com/my-service:latest",
      "essential": true,
      "environment": [
        { "name": "ENV", "value": "envtext" }
      ],
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/ecs/my-service",
          "awslogs-region": "<region>",
          "awslogs-stream-prefix": "ecs"
        }
      }
    }
  ]
}

Run the task:

bashCopyEditaws ecs run-task \
  --cluster <your-cluster-name> \
  --launch-type FARGATE \
  --network-configuration "awsvpcConfiguration={subnets=[subnet-xxxxxxx],securityGroups=[sg-xxxxxxx],assignPublicIp=ENABLED}" \
  --task-definition my-service

This command tells ECS to start a task based on your task definition, in your specified cluster, using Fargate, with the given network settings.

What’s Next?

Once your task is running, ECS and Fargate take care of the rest — no servers to manage, and it scales as needed.
You can also set it up as a service if you want it to restart automatically if it stops.


Final Thoughts

In project I was working, I needed something cost-effective, scalable, and “next-to-real-time.” Fargate was the perfect fit.
Building it taught me a lot about ECS & ECR, and I hope this guide helps you too.

If you have any questions, feel free to reach out — happy to help or just chat about it!

5
Subscribe to my newsletter

Read articles from pouya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

pouya
pouya