咨询基于自定义EC2实例部署AWS ECS服务的方法及相关文档
Hey there! As someone who’s walked through setting up ECS on both custom and optimized AMIs, let me break this down for you step by step—perfect for AWS beginners.
First: The Easier Route - Using AWS ECS-Optimized AMI
If you’re just starting out, this is the best way to avoid configuration headaches. AWS pre-packages everything you need for ECS here:
- Launch the Instance: When spinning up an EC2 instance in the AWS Console, search for the "Amazon ECS-optimized Ubuntu AMI" (look for Xenial 16.04 if you prefer that version).
- Attach the Required IAM Role: Before launching, assign the
AmazonEC2ContainerServiceforEC2Roleto your instance. This role gives the ECS agent permission to communicate with the ECS API—without it, your instance won’t join your cluster. If you don’t have this role yet, create it in the IAM Console and attach the managed policy of the same name. - Set Cluster Name via User Data: In the "Advanced Details" section of the instance launch wizard, add this line to auto-register the instance to your ECS cluster:
echo ECS_CLUSTER=your-cluster-name >> /etc/ecs/ecs.config - Verify: Once the instance is running, SSH into it and run
sudo service ecs status—you should see the agent is active and running.
Setting Up ECS on Your Custom Ubuntu 16.04 AMI
If you need to use your existing Ubuntu 16.04 AMI, follow these steps to get the ECS agent up and running:
Step 1: Install Docker
ECS relies on Docker, so first install it on your Ubuntu instance:
sudo apt-get update && sudo apt-get upgrade -y sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable" sudo apt-get update sudo apt-get install -y docker-ce
Verify Docker is working with sudo docker run hello-world.
Step 2: Configure and Launch the ECS Agent
The ECS agent runs as a Docker container. Here’s how to set it up to start automatically:
- Create the ECS config directory and set your cluster name:
sudo mkdir -p /etc/ecs echo ECS_CLUSTER=your-cluster-name | sudo tee /etc/ecs/ecs.config - Create a systemd service file to manage the ECS agent (Ubuntu 16.04 uses systemd):
Create/etc/systemd/system/ecs.servicewith the following content:[Unit] Description=Amazon ECS Agent Requires=docker.service After=docker.service [Service] Restart=always RestartSec=3 EnvironmentFile=/etc/ecs/ecs.config ExecStartPre=-/usr/bin/docker kill ecs-agent ExecStartPre=-/usr/bin/docker rm ecs-agent ExecStartPre=/usr/bin/docker pull amazon/amazon-ecs-agent:latest ExecStart=/usr/bin/docker run --name ecs-agent \ --detach=false \ --restart=on-failure:10 \ --volume=/var/run/docker.sock:/var/run/docker.sock \ --volume=/var/log/ecs/:/log \ --volume=/var/lib/ecs/data:/data \ --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro \ --volume=/run/docker/execdriver/native:/var/lib/docker/execdriver/native:ro \ --publish=127.0.0.1:51678:51678 \ --env-file=/etc/ecs/ecs.config \ amazon/amazon-ecs-agent:latest [Install] WantedBy=multi-user.target - Enable and start the service:
sudo systemctl daemon-reload sudo systemctl enable ecs sudo systemctl start ecs
Step 3: Attach the IAM Role
Just like with the optimized AMI, you must assign the AmazonEC2ContainerServiceforEC2Role to your custom Ubuntu instance. This is non-negotiable—without it, the agent can’t register with ECS or pull task definitions.
Step 4: Verify the Setup
- Check if the ECS agent container is running:
sudo docker ps(look foramazon/amazon-ecs-agent). - Check the service status:
sudo systemctl status ecs. - Head to the ECS Console, navigate to your cluster, and you should see your instance listed under "ECS Instances".
Pro Tip for Beginners
Start with the ECS-optimized AMI first—get your Spring Cloud services up and running smoothly, then tackle customizing an AMI if you need to add pre-installed tools or configurations later. This reduces the chance of hitting unexpected issues early on.
内容的提问来源于stack exchange,提问作者Mr.DevEng

