0
0
AWScloud~5 mins

Fargate serverless containers in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Running containers usually requires managing servers or clusters. Fargate lets you run containers without handling servers, so you focus only on your app.
When you want to run a containerized app but don't want to manage or patch servers.
When you need to quickly deploy a microservice without setting up infrastructure.
When you want to scale containers automatically without manual intervention.
When you want to pay only for the compute your containers use, not idle servers.
When you want to run containers alongside other AWS services easily.
Config File - fargate-task-definition.json
fargate-task-definition.json
{
  "family": "my-fargate-task",
  "networkMode": "awsvpc",
  "requiresCompatibilities": ["FARGATE"],
  "cpu": "256",
  "memory": "512",
  "containerDefinitions": [
    {
      "name": "my-app-container",
      "image": "nginx:1.23",
      "portMappings": [
        {
          "containerPort": 80,
          "protocol": "tcp"
        }
      ]
    }
  ]
}

family: Names the task definition.

networkMode: Uses AWS VPC networking for isolation.

requiresCompatibilities: Specifies this task runs on Fargate.

cpu and memory: Define the resources for the task.

containerDefinitions: Defines the container image and ports.

Commands
This command registers the task definition with ECS so Fargate knows what container to run and how.
Terminal
aws ecs register-task-definition --cli-input-json file://fargate-task-definition.json
Expected OutputExpected
{ "taskDefinition": { "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/my-fargate-task:1", "family": "my-fargate-task", "revision": 1, "status": "ACTIVE" } }
--cli-input-json - Provides the task definition details from a JSON file.
Creates a new ECS cluster where your Fargate tasks will run.
Terminal
aws ecs create-cluster --cluster-name my-fargate-cluster
Expected OutputExpected
{ "cluster": { "clusterArn": "arn:aws:ecs:us-east-1:123456789012:cluster/my-fargate-cluster", "clusterName": "my-fargate-cluster", "status": "ACTIVE" } }
--cluster-name - Names the ECS cluster.
Runs the task on Fargate in the specified cluster and subnet with a public IP so it can be accessed.
Terminal
aws ecs run-task --cluster my-fargate-cluster --launch-type FARGATE --task-definition my-fargate-task --network-configuration "awsvpcConfiguration={subnets=[subnet-0bb1c79de3EXAMPLE],assignPublicIp=ENABLED}"
Expected OutputExpected
{ "tasks": [ { "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/my-fargate-cluster/1234567890abcdef0", "lastStatus": "PROVISIONING" } ], "failures": [] }
--launch-type - Specifies to use Fargate serverless containers.
--network-configuration - Sets the VPC subnet and public IP assignment.
Lists running tasks in the cluster to verify the task started.
Terminal
aws ecs list-tasks --cluster my-fargate-cluster
Expected OutputExpected
{ "taskArns": [ "arn:aws:ecs:us-east-1:123456789012:task/my-fargate-cluster/1234567890abcdef0" ] }
--cluster - Specifies which ECS cluster to list tasks from.
Key Concept

If you remember nothing else from this pattern, remember: Fargate lets you run containers without managing servers by defining a task and running it in a cluster with network settings.

Common Mistakes
Not specifying the network configuration with subnets and public IP when running the task.
Fargate tasks need network settings to connect to the internet or other resources; missing this causes task launch failure.
Always include the --network-configuration flag with valid subnet IDs and assignPublicIp setting.
Using an incompatible launch type or forgetting to set requiresCompatibilities to FARGATE in the task definition.
The task won't run on Fargate if it is not marked compatible, causing errors.
Set requiresCompatibilities to ["FARGATE"] in the task definition JSON.
Summary
Create a task definition JSON file describing your container and resources.
Register the task definition with ECS using the AWS CLI.
Create an ECS cluster to run your Fargate tasks.
Run the task on Fargate with proper network settings to launch your container.
Verify the task is running by listing tasks in the cluster.