How to run containers on EC2 in ECS ?

0

I referred to https://docs.aws.amazon.com/zh_tw/AmazonECS/latest/developerguide/getting-started-fargate.html and have successfully run it on Fargate. However, I want to modify it to run on EC2, but I failed. In the Task interface, I see the last status constantly in Provisioning. How can I proceed to successfully run it on EC2? The steps I failed are as follows:

  1. Modify the requiresCompatibilities section to EC2.
  2. Add an Auto Scaling Group (ASG) with Min Size: 0, Max Size: 3, Desired size: 0, AMI: Amazon Linux 2023 (X86_64), Instance type: t2.medium.
  3. In the Infrastructure, add ASG capacity providers.
  4. Create a service:
  • In Compute configuration (advanced), choose Capacity provider strategy.
  • Select the ASG capacity providers added in step 3, with Base: 0 and Weight: 1.
  • For Deployment configuration, select Application type as Service.
  • Desired tasks: 1.

Below is the modified configuration file.

{
    "family": "sample-ec2", 
    "networkMode": "awsvpc", 
    "containerDefinitions": [
        {
            "name": "ec2-app", 
            "image": "public.ecr.aws/docker/library/httpd:latest", 
            "portMappings": [
                {
                    "containerPort": 80, 
                    "hostPort": 80, 
                    "protocol": "tcp"
                }
            ], 
            "essential": true, 
            "entryPoint": [
                "sh",
		"-c"
            ], 
            "command": [
                "/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' >  /usr/local/apache2/htdocs/index.html && httpd-foreground\""
            ]
        }
    ], 
    "requiresCompatibilities": [
        "EC2"
    ], 
    "cpu": "256", 
    "memory": "512"
}
asked a month ago401 views
2 Answers
0
Accepted Answer

Hello.

First, try creating an EC2 launch type ECS cluster by following the steps in the document below.
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create-ec2-cluster-console-v2.html

Also, were there any errors when you started the ECS task?

profile picture
EXPERT
answered a month ago
profile picture
EXPERT
reviewed a month ago
  • Hi, Riku I have created a cluster according to https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create-ec2-cluster-console-v2.html. I haven't received any errors from ECS tasks. It's just that the status remains stuck at 'Provisioning' continuously. I'm very confused and not sure where the problem. It's worth noting that it has been in the 'Provisioning' state for 2 hours now.

  • Is the EC2 instance running the task started? Also, if it is running, is a public IP set to access the container image from EC2? If you do not have a public IP etc., please try using NAT Gateway etc. Please also check if any other IAM policies required for the task execution role are attached. https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_execution_IAM_role.html

  • I have confirmed that instances are activated with public IPv4 addresses and are running in the infrastructure. There are two machines for container instances, both in an active state. Regarding permissions, it appears that both ecsTaskExecutionRole and ecsInstanceRole align with the documentation. They have been assigned the following policies: ecsTaskExecutionRole : AmazonECSTaskExecutionRolePolicy ( type : AWS managed) ecsInstanceRole: AmazonEC2ContainerServiceforEC2Role ( type: AWS managed)

0

Currently, ECS can work on EC2, so I propose updating the solution to this issue as follows:

  1. Modify the AMI of Launch Templates to Amazon ECS-Optimized Amazon Linux 2023 (AL2023) x86_64 AMI instead of the commonly used Amazon Linux 2023.
  2. Switch to larger EC2 instances. I suspect this is related to ECS requiring additional operational resources.
  3. Adjust Task definitions by adding "cpu": 1024, "memory": 2048. Although I have already defined "cpu": "1024", "memory": "2048", I still need to define them again. I am unsure why this is necessary. I referred to https://docs.aws.amazon.com/zh_tw/AmazonECS/latest/developerguide/getting-started-ecs-ec2-v2.html and noticed it defines them twice. Hence, I followed suit. If anyone knows the reason behind this, please enlighten me.
answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions