Logo

dev-resources.site

for different kinds of informations.

AWS Fargate Basics: A Crash Course

Published at
1/11/2025
Categories
aws
fargate
crashcourse
Author
Sarva Bharan
Categories
3 categories in total
aws
open
fargate
open
crashcourse
open
AWS Fargate Basics: A Crash Course

AWS Fargate is like hiring a valet for your containers. You focus on the container (app), and Fargate handles the infrastructure for running it. Think of it as a serverless compute engine specifically for containerized workloads.

Image Fargate vs EC2

Key Fargate Concepts to Know

  1. Cluster: Logical grouping of tasks/services. You need a cluster for Fargate.
  2. Task: A single running container or a set of tightly coupled containers.
  3. Task Definition: The "recipe" for your task—what container to use, memory/CPU requirements, environment variables, etc.
  4. Service: Long-running tasks with scaling and load balancing (e.g., an API).
  5. Launch Type: For Fargate, use FARGATE as the type (instead of EC2).

Setting Up Fargate in AWS

Here’s a simple guide to get your Fargate task/service up and running:

1. Prepare Your Container Image

  • Dockerize your app: Ensure your application is packaged in a Docker image.
  # Example: Dockerfile for a Node.js app
  FROM node:16
  WORKDIR /usr/src/app
  COPY package*.json ./
  RUN npm install
  COPY . .
  CMD ["node", "app.js"]
  EXPOSE 3000
  • Push to ECR (Elastic Container Registry):
  aws ecr create-repository --repository-name my-app
  docker tag my-app:latest <your-account-id>.dkr.ecr.<region>.amazonaws.com/my-app
  docker push <your-account-id>.dkr.ecr.<region>.amazonaws.com/my-app

2. Define a Fargate Task

Go to ECS Console → Task Definitions → Create a new task.

  • Launch Type: Fargate.
  • Container Definition: Add your container. Specify the image URI from ECR.
    • CPU/Memory: Set based on your workload.
    • Port Mappings: Map exposed ports (e.g., 3000:3000 for a Node.js app).

3. Create a Cluster

Go to ECS Console → Clusters → Create a new cluster.

  • Select Networking only (Fargate).
  • Name your cluster (e.g., my-fargate-cluster).

4. Deploy Your Service

Go to ECS Console → Services → Create.

  • Cluster: Select your cluster.
  • Task Definition: Choose the task you defined earlier.
  • Service Type:
    • Use Service for APIs or long-running workloads.
    • Use Task for one-time jobs.
  • Scaling: Set desired and max tasks for auto-scaling.

5. Networking Setup

  • Assign a VPC and subnets for your service.
  • Enable a security group for access (e.g., allow port 3000 for HTTP traffic).

6. Test Your Service

  • Once deployed, note the service’s public IP or load balancer endpoint.
  • Access it via your browser or curl.
  curl http://<public-ip>:3000

Real-World Usage Examples

  • API Deployment: Host your containerized API without managing infrastructure.
  • Data Processing: Run batch jobs like image resizing or log analysis.
  • Event-Driven Tasks: Use with Lambda for asynchronous processing (e.g., Fargate processes incoming SNS messages).

Tips and Best Practices

  1. Right-Size Tasks: Avoid over-allocating memory/CPU for cost efficiency.
  2. Secure Networking: Restrict public access with VPC/private subnets.
  3. Monitoring: Use CloudWatch Logs to track task performance.
  4. Autoscaling: Set thresholds to scale up/down based on demand.

Cheers🥂

Featured ones: