
AWS ECS, ECR, and IAM: A Beginner's Guide to Deployment with Minimal AWS Costs
Deploying applications on AWS can seem complicated at first, especially when you encounter services such as Amazon ECS, Amazon ECR, and AWS IAM. These three services work together to help you deploy containerized applications securely and efficiently. This article explains what each service does, how they work together, and how to set up a simple deployment while minimizing AWS credit consumption.
1. Understanding the three AWS services

How they work together

In simple terms:
- Docker packages your application.
- ECR stores the packaged application.
- ECS runs the application.
- IAM controls permissions throughout the process.
AWS describes ECR as a managed image registry and ECS as a container orchestration service.
AWS Product Comparison+1
2. Important ECS concepts
ECS component | Purpose |
|---|---|
Cluster | Logical grouping for your ECS workloads |
Task definition | Blueprint specifying image, CPU, memory, ports, and roles |
Task | A running instance of a task definition |
Service | Maintains the desired number of running tasks |
Fargate | Runs containers without requiring you to manage EC2 servers |
For a beginner's project, ECS with Fargate is a convenient starting point because you don't need to create or maintain virtual machines.
3. Understanding IAM roles and permissions
IAM is particularly important because ECS needs permission to pull images, write logs, and access other AWS resources.
There are two main IAM roles to understand:
Task execution role
Used by the ECS/Fargate infrastructure to perform setup tasks, such as pulling a private ECR image and sending logs to CloudWatch.
Common managed policy:
AmazonECSTaskExecutionRolePolicy Task role
Used by your application code running inside the container to access AWS services.
For example, if your application reads from S3, its task role should have only the required S3 permissions.
Least privilege means granting only the permissions required for a specific task, rather than giving broad administrator access.
For a basic application that only runs a web server, you may not need to attach any additional permissions to its task role.
User Guide
4. Prerequisites
Before starting, make sure you have:
- An AWS account with billing enabled.
- Docker installed on your computer.
- AWS CLI v2 installed and configured.
- An IAM identity with permissions to create the resources described below.
Choose one AWS Region and use it consistently. The examples use
ap-south-1 (Mumbai).Check your AWS CLI configuration:
aws sts get-caller-identityaws configure get regionIf you need to set the default Region:
aws configure set region ap-south-15. Step-by-step setup process
Step 1: Create a sample application
Create a folder named
ecs-demo and add these two files.app.py from http.server import BaseHTTPRequestHandler, HTTPServerclass Handler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(b"Hello from AWS ECS!")HTTPServer(("0.0.0.0", 8080), Handler).serve_forever()Dockerfile FROM python:3.12-slim
WORKDIR /appCOPY app.py .
EXPOSE 8080
CMD ["python", "app.py"]Build and test the image locally:
docker build -t ecs-demo .docker run --rm -p 8080:8080 ecs-demoVisit
http://localhost:8080 to confirm that the application works.Step 2: Create an ECR repository
Create a private repository using the AWS CLI:
aws ecr create-repository \ --repository-name ecs-demo \ --region ap-south-1Get your AWS account ID:
aws sts get-caller-identity \ --query Account \ --output textSet the account ID and repository URI in your terminal:
export AWS_ACCOUNT_ID="YOUR_ACCOUNT_ID"export AWS_REGION="ap-south-1"export ECR_URI="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/ecs-demo"Authenticate Docker to ECR:
aws ecr get-login-password \ --region "$AWS_REGION" \ | docker login \ --username AWS \ --password-stdin \ "$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com"Tag and push the image:
docker tag ecs-demo:latest "$ECR_URI:v1"
docker push "$ECR_URI:v1"Your application image is now stored in ECR.
Step 3: Create the IAM execution role
In the AWS Console:
- Open IAM → Roles → Create role.
- Select AWS service as the trusted entity.
- Select Elastic Container Service.
- Choose the ECS task use case.
- Attach
AmazonECSTaskExecutionRolePolicy.
- Name the role
ecsDemoExecutionRole.
- Create the role.
This role allows ECS to retrieve your private ECR image and publish container logs to CloudWatch.
For a production environment, consider replacing broad managed permissions with a carefully scoped custom policy where appropriate.
Step 4: Create an ECS cluster
In the AWS Console:
- Open Amazon ECS → Clusters.
- Choose Create cluster.
- Enter
ecs-demo-clusteras the cluster name.
- Use the default networking configuration.
- Create the cluster.
Creating an ECS cluster does not require launching an EC2 instance or keeping a Fargate task running.
Step 5: Create a task definition
Navigate to ECS → Task definitions → Create new task definition.
Use these settings:
Setting | Value |
|---|---|
Task definition family | ecs-demo-task |
Launch type | AWS Fargate |
Operating system | Linux |
CPU architecture | x86_64 |
CPU | 0.25 vCPU |
Memory | 0.5 GB |
Task execution role | ecsDemoExecutionRole |
Task role | None |
Container name | ecs-demo |
Image URI | Your ECR URI ending in :v1 |
Container port | 8080 |
Choose CloudWatch logging if you want to inspect application logs. Creating a log group and retaining logs may incur charges, so use a short retention period for experiments.
Step 6: Run your application
For the lowest-cost demonstration, run a standalone task instead of creating a long-running service.
- Open your ECS cluster.
- Choose Run new task.
- Select Fargate as the launch type.
- Select your task definition.
- Select a VPC and subnet.
- Choose a security group allowing inbound TCP port 8080 from your own IP address only.
- Launch the task.
For a public test, use a public subnet with a public IP address and an internet gateway. Avoid creating a NAT Gateway just for this tutorial.
Once the task is running, open its public IP address in your browser:
http://YOUR_TASK_PUBLIC_IP:8080You should see:
Hello from AWS ECS!For production, use an Application Load Balancer, HTTPS, private subnets, and appropriate network controls. These additional resources can increase costs.
6. How to minimize AWS credit consumption
The most important cost-saving decision is to avoid leaving infrastructure running when you are not using it.
Use Fargate for short experiments
Run one small task with 0.25 vCPU and 0.5 GB memory, then stop it when finished.
Keep container images small
Use lightweight base images and remove unused ECR image versions.
Avoid unnecessary infrastructure
Skip NAT Gateways, load balancers, and extra compute resources for a simple learning project.
Set billing alerts
Use AWS Budgets and billing alerts to monitor your credit balance and spending.
Cost considerations
Resource | Cost-saving approach |
|---|---|
ECS | No separate orchestration fee; compute costs depend on the launch type |
Fargate | Stop tasks when idle |
ECR | Keep only a few small images |
CloudWatch Logs | Limit retention and log volume |
Public IPv4 | Avoid unnecessary allocated addresses |
NAT Gateway | Avoid for this simple demo |
Load Balancer | Do not create one unless needed |
ECR offers a limited free storage allowance for eligible accounts, but storage and data transfer can become chargeable. Fargate charges for requested compute resources while tasks run. Exact pricing and Free Tier eligibility depend on your account and Region.
Amazon Web Services, Inc.+2
7. Clean up after testing
When you finish, stop your ECS task:
aws ecs stop-task \ --cluster ecs-demo-cluster \ --task YOUR_TASK_ARN \ --reason "Finished testing"Delete the ECR repository when you no longer need the image:
aws ecr delete-repository \ --repository-name ecs-demo \ --force \ --region ap-south-1Also delete any unused CloudWatch log groups, load balancers, NAT Gateways, and other resources you created during experimentation.
Conclusion
Amazon ECS, ECR, and IAM form the foundation of a secure container deployment on AWS. ECR stores your images, ECS runs them, and IAM provides controlled access to AWS resources.
For beginners, a small Fargate task using a private ECR repository and a dedicated execution role is a practical way to learn the deployment workflow without maintaining servers. Keep the task stopped when idle, avoid unnecessary networking resources, and monitor billing to preserve your AWS credits.
Official AWS resources
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article