skip to Main Content

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"]

Compute configuration:
enter image description here

Task definition
enter image description here

Cloudfront events
enter image description here

Any help or suggestions would be greatly appreciated

2

Answers


  1. 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?

    Login or Signup to reply.
  2. 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:

    docker run --rm --it -p 8080:80 johnlawliet/fastapi_app
    curl http://localhost:8080
    

    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:

    1. EC2 instances that launched by ASG has propeply configured security groups, so ECS can access them.
    2. Include the following bootstrap script in order to register an instance in the correct ECS cluster:
    cluster_name="fastapiCluster" # should match your ECS cluster name
    echo "ECS_CLUSTER=${cluster_name}" >> /etc/ecs/ecs.config
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search