
Yep, you already know how to build your container. But now you want to get that container running on AWS with as little fuss as possible, right? No problem. ⚡
In this quick guide, I'll show you how to deploy your container to
AWS Fargate using CloudFormation. It’s fast, easy, and doesn’t require you to wade through endless steps. Just the essentials to get your container online.Before We Begin
We’re using
CloudFormation here because it’s perfect for quickly spinning up your infrastructure with minimal hassle. Sure, there are other tools like Terraform or CDK, but for a straightforward ECS setup, CloudFormation is your go-to.We’re keeping it simple: we’ll create a
VPC, a couple of public subnets, an ECS cluster, and a load balancer to get your containers up and running. No need for extra complexity—just the basics to get your containerized app deployed fast. And yes, we’ve got IAM roles to keep everything secure. Let’s dive in!Table of Contents
This table of contents should make it easy to navigate through the guide.
Architecture Boring Stuff
User Interaction
User Request: A user sends an HTTP request to the Application Load Balancer (ALB) (
ALBDNSName).ALB Routing: The ALB routes the request to the ECS service, which runs your containerized application.
ECS Task Handling: The ECS tasks, running within the Fargate cluster, handle the request, with containers managed by ECS.
ECS and Load Balancer
ECS Fargate Cluster (
ECSFargateCluster): Manages and scales the containers based on defined parameters.Application Load Balancer (
ALB): Distributes incoming traffic to the appropriate ECS tasks, ensuring availability and responsiveness.Target Group (
ALBTargetGroup): Monitors the health of your ECS tasks and directs traffic only to healthy instances.IAM Roles
IAMRoleForECS: Grants permissions for ECS tasks to pull images from ECR, send logs to CloudWatch, and perform other necessary actions.ECRCleanupLambdaRole: Allows the Lambda function to delete images from the ECR repository during the cleanup process.Pipeline User for GitHub Actions
The GitHub Actions workflow uses an IAM user (
PipelineUser) to deploy updates to the ECS service. The workflow includes steps for:Checking out the code.
Building the container image.
Pushing the image to ECR.
Configuring AWS credentials.
Updating the ECS task definition with the new image.
Triggering a new deployment of the ECS service.
Flow
User -> Application Load Balancer -> ECS Service -> ECS Task (Container)
ECS Tasks -> Fargate Cluster (for running containers)
Pipeline User -> GitHub Actions -> ECR and ECS (for deployment)
cloudformation-template.yaml
The same template without creating a new vpc:
cloudformation-template-with-existing-vpc.yaml
Note: It's important to edit the task's port if needed. By default, this is set up with a placeholder using
nginx listening on port 80 and the health check is set to /. However, you can change these settings to fit your requirements.LET'S KICK IT OFF!
Alright, buckle up! If you've got an AWS account and the AWS CLI locked and loaded, we're ready to roll. (Pro tip: jq is handy, but hey, we won't judge if you skip it.)
Step-01: Deploy CloudFormation Stack for ECS Fargate
Note: The stack uses predefined default names for easy identification in the pipeline, u can override it:
ClusterName:
EcsFargateClusterServiceName:
EcsServiceTaskFamilyName:
TaskDefinitionECRRepositoryName:
application-repositoryContainerName:
application-containerStep-02: Create AWS IAM User for Pipeline
In this step, we'll create a Pipeline User that GitHub Actions will use. This user will have the necessary policies attached to push container images to ECR and update our ECS service. Here's how to do it:
Step-03: Add Secrets to GitHub Secrets
After creating the IAM user and generating the access keys, follow these steps to add these credentials to your GitHub
repository secrets for use in GitHub Actions:
Navigate to Your GitHub Repository:
Go to the main page of your repository on GitHub.
Access Repository Settings:
Click on the Settings tab at the top of the repository page.
Navigate to Secrets:
In the left sidebar, click on Secrets and variables > Actions.
Add New Repository Secrets:
Click New repository secret and add the following:
AWS_ACCESS_KEY_ID: Enter your
AccessKeyId value.AWS_SECRET_ACCESS_KEY: Enter your
SecretAccessKey value.By doing this, you'll securely store the necessary credentials for your GitHub Actions workflow.
Step-04: Github Actions
Time to automate the deployment with GitHub Actions! Here's how you can set up your pipeline:
Copy the following YAML content into
.github/workflows/deploy.yml:Important: The container image in the ECR repository is updated with each push to the
main branch. The service name EcsService, cluster name EcsFargateCluster, and other values are predefined in your CloudFormation stack, ensuring smooth integration with your AWS infrastructure.Now, commit and push your changes to trigger the pipeline:
Once pushed, the pipeline will automatically build, push your Docker image to ECR, and update your ECS Fargate service with the latest code. 🚀
Cleaning Up
To delete the CloudFormation stack, use the following command:
BLAZINGGG ENJOYY 🎉🔥
You can see your pipeline triggered, the Docker image built, pushed to ECR, and your ECS Fargate service updated. This workflow will automatically deploy your changes to the Fargate cluster every time you push to the main branch. 🚀
To check if everything's working, grab the ALB DNS Name from the stack output or the AWS console and make an HTTP request to your application. 🌐
Pricing
Here’s an estimated cost breakdown for the AWS ECS infrastructure setup you provided, considering that you have a single ECS Fargate task running:
1. VPC and Subnets
VPC: No additional cost.
Subnets: No direct cost for subnets.
2. Internet Gateway
Cost: $0.045 per hour.
3. ECS Fargate
vCPU (0.5 vCPU): $0.04048 per vCPU-hour.
Memory (1 GB): $0.004445 per GB-hour.
For a single task running 24/7:
vCPU Cost (0.5 vCPU): 0.5 * 24 * 30 * $0.04048 = $14.58 per month.
Memory Cost (1 GB): 1 * 24 * 30 * $0.004445 = $3.20 per month.
4. Application Load Balancer (ALB)
ALB: $0.0225 per hour.
Data Processed by ALB: $0.008 per GB.
For 24/7 operation:
ALB Cost: 24 * 30 * $0.0225 = $16.20 per month.
5. ECR Storage
Cost: $0.10 per GB per month.
6. CloudWatch Logs
Storage: $0.03 per GB.
Ingested Logs: $0.50 per GB.
Total Estimated Monthly Cost
Adding up the individual components, here’s an estimated monthly cost:
ECS Fargate: ~$17.78
ALB: ~$16.20
Internet Gateway: ~$32.40 (if used)
ECR Storage: Variable depending on image size.
CloudWatch Logs: Variable depending on log volume.
Estimated Total: ~$66.38 per month (excluding data transfer and storage variations).
Alternative Pricing without Internet Gateway:
If you use the default AWS network configuration and don’t need an Internet Gateway:
Estimated Total: ~$34.58 per month (excluding Internet Gateway cost).
Conclusion
While serverless and Fargate offer great convenience, costs can escalate quickly. For instance, running a
0.5 vCPU and 1GB memory task costs around $18/month just for compute. Additional services like the Application Load Balancer (ALB) and CloudWatch Logs further increase expenses. If you're working on smaller projects or development environments, consider skipping features like the Internet Gateway or ALB to save costs. Always tailor your setup to your specific needs to optimize cloud spending.And there you have it! A blazing fast, simple way to deploy your containers on AWS ECS Fargate. Enjoy!
Happy deploying :)
0 likes · 11 views · Nov 17, 2025
Deployed a container on AWS Fargate using CloudFormation for fast, easy setup.