I have created a python application with a pipeline that after pushing to github, containerizes the project and pushes to dockerhub. The docker container runs successfully locally but I want to deploy it to aws ECS EC2 launch type (because of free tier) so I create a cluster, Task definition pointing to the dockerhub registry, ASG, ALB and try to create a service or task.
However, on creating the service, cloudfront keeps rolling back during EC2 service creation. If I setup a task then it gets stuck on provisioning.
Dockerfile
FROM python:3.12-alpine
WORKDIR /app
COPY . /app
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 80
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
Any help or suggestions would be greatly appreciated
2
Answers
The problem is not with your image; instead, it arises from the configuration of your CPU and memory. Please allocate the appropriate CPU and memory resources to your task definition. Additionally, ensure that your EC2 instance has sufficient capacity to handle the task.
You have shared the configuration of your container, but please provide a screenshot of your task’s CPU and memory settings for a clear understanding of the issue. Also, could you specify the instance type you are using?
There are a few things you can do in order to troubleshoot the issue:
Test locally
Try to run the container locally and make sure it respond on exposed port successfully:
Check ALB healthcheck
Ensure that Load Balancer healthcheck configured property, and check health status of the application once it get deployed. If your applicaiton is not able to pass healthchecks then ECS will automatically shutdown the container, and because you have Deployment Circuit Breaker in place it won’t try to redeploy it.
Check that ASG works
Another common issue is that ASG spins up a new EC2 instance that never registered in ECS cluster. If this is a case you need to make sure:
Check task resource requirements and EC2 available resources
There is a probablity that your EC2 instance doesn’t have enough resources in order to deploy a task. So, make sure that’s not the case. You can check resource requirements in the ECS Task definition.
Please let me know if any of these help, or if you have any further questions.