<p>The way a cluster becomes aware of the EC2 instances associated to it, is a configuration file used by the ECS agent. You can modify this file, located at <code>/etc/ecs/ecs.config</code>, and name a different cluster. Then restart the ECS agent. This will effectively "move" the EC2 instance to the other cluster.</p>

<p>More information about <code>ecs.config</code> in the documentation - <a href="http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-config.html" rel="nofollow noreferrer">http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-config.html</a></p>

<p>After the modification, it is possible to verify by sending an HTTP request to the ECS Agent metadata service and see the new cluster displayed. <a href="http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-update.html" rel="nofollow noreferrer">http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-update.html</a></p>

<p>The ECS cluster itself also needs to be notified. Can use the Register/Deregister API calls for this, for example using the AWS CLI:</p>

<ul>
<li><a href="http://docs.aws.amazon.com/cli/latest/reference/ecs/register-container-instance.html" rel="nofollow noreferrer">http://docs.aws.amazon.com/cli/latest/reference/ecs/register-container-instance.html</a></li>
<li><a href="http://docs.aws.amazon.com/cli/latest/reference/ecs/deregister-container-instance.html" rel="nofollow noreferrer">http://docs.aws.amazon.com/cli/latest/reference/ecs/deregister-container-instance.html</a></li>
</ul>

<p>More housekeeping might be required on the ECS agent itself. Like removing cached checkpoint files, etc. <a href="https://github.com/aws/amazon-ecs-agent/issues/139#issuecomment-123061463" rel="nofollow noreferrer">https://github.com/aws/amazon-ecs-agent/issues/139#issuecomment-123061463</a></p>

CC BY-SA 3.0
2017-09-18T05:19:39.323Z
i modified `/etc/ecs/ecs.config` to `ECS_CLUSTER=devcluster` and restarted ec2 instance. but the instance still appear on `qacluster`
sithumc
CC BY-SA 3.0
2017-09-18T07:06:14.893Z
@sith I updated the answer with details about registration of instances in the ECS Cluster metadata. Might need to be updated as well, or else the cluster does not know about a new instance or about a removed one.
Evgeny Zislis
CC BY-SA 3.0
2017-09-18T04:45:35.697Z
2017-09-18T07:05:36.837Z
2017-09-18T07:05:36.837Z
Evgeny Zislis
<p>Updated for ECS optimized AMI image as of May 2021. The sudo start command is not present and the checkpoint file /var/lib/ecs/data/ecs_agent_data.json changed to agent.db.</p>
<ol>
<li>Edit <code>/etc/ecs/ecs.config</code> as previously described (add ECS_CLUSTER=devcluster).</li>
<li>Rename the agent state file with <code>mv /var/lib/ecs/data/agent.db /var/lib/ecs/data/agent.db.old</code></li>
<li>Restart the ECS agent using <code>sudo systemctl restart ecs</code></li>
</ol>
<p>Check the status of the agent with <code>systemctl status ecs</code>, you want it to display <em>Active: active (running)</em>.</p>
<p>This was a bit frustrating to figure out because the Amazon online documentation I found still displays the old information. Couldn't have done it without devops!</p>

CC BY-SA 4.0
2021-05-26T13:53:19.683Z
2021-05-26T13:53:19.683Z
Mathieu Dugré
<p>You need to follow these steps:</p>

<ul>
<li>Set <code>ECS_CLUSTER=devcluster</code> in <code>/etc/ecs/ecs.config</code></li>
<li>Stop all tasks/containers</li>
<li>Remove checkpoint file - <code>/var/lib/ecs/data/ecs_agent_data.json</code></li>
<li>Start ECS agent again as explained here - <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-install.html" rel="noreferrer">https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-install.html</a></li>
</ul>

<p>To make sure it uses correct cluster, check logs - <code>docker logs ecs-agent</code>. It should have a message like:</p>

<pre><code>[INFO] Registration completed successfully. I am running as 'arn:aws:ecs:eu-west-1:ACCOUNT_ID:container-instance/CLUSTER_ID' in cluster 'devcluster'
</code></pre>

CC BY-SA 3.0
2018-01-24T15:54:30.597Z
You should avoid publishing any ressource arn without masking your account id and the ressource id in my opinion. I've edited and cleared the the revision history.
Tensibai
CC BY-SA 3.0
2018-01-24T22:31:32.480Z
@Tensibai you're totally right. But it was actually masked as I have replaced last 6 digits with zeros ;) But this change would not harm.
lagivan
CC BY-SA 3.0
2018-01-24T23:13:03.400Z
There's a small chance it match someone else account number, prefer replacing with xxxx than with 0 to avoid bots scanning the internet launching attacks :)
Tensibai
CC BY-SA 3.0
2018-01-24T15:44:53.327Z
2018-01-24T15:52:54.710Z
2018-01-24T15:52:54.710Z
lagivan
<p>Assume I have two clusters <code>devcluster</code> and <code>qacluster</code> on <a href="https://aws.amazon.com/ecs/" rel="nofollow noreferrer">ECS</a>. 
How to remove an EC2 instance <em>(ecs-optimized amazon-linux)</em> from <code>qacluster</code>  and add to <code>devcluster</code>?</p>

<amazon-ecs>
How to move EC2 instance to another ECS cluster
sithumc
# How to move EC2 instance to another ECS cluster

(Asked by: username_0 on 2017-09-17)

Assume I have two clusters `devcluster` and `qacluster` on [ECS](https://aws.amazon.com/ecs/).
How to remove an EC2 instance *(ecs-optimized amazon-linux)* from `qacluster` and add to `devcluster`?

**Tags:** amazon-ecs


---

## 3 answers

### Answer by username_3 on 2018-01-24. Score: 6 | Accepted answer

You need to follow these steps:

Set ECS_CLUSTER=devcluster in /etc/ecs/ecs.config
Stop all tasks/containers
Remove checkpoint file - /var/lib/ecs/data/ecs_agent_data.json
Start ECS agent again as explained here - https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-install.html

To make sure it uses correct cluster, check logs - docker logs ecs-agent. It should have a message like:
[INFO] Registration completed successfully. I am running as 'arn:aws:ecs:eu-west-1:ACCOUNT_ID:container-instance/CLUSTER_ID' in cluster 'devcluster'

> **Comments:**
> **username_4**: You should avoid publishing any ressource arn without masking your account id and the ressource id in my opinion. I've edited and cleared the the revision history.  
> **username_3**: @username_4 you're totally right. But it was actually masked as I have replaced last 6 digits with zeros ;) But this change would not harm.  
> **username_4**: There's a small chance it match someone else account number, prefer replacing with xxxx than with 0 to avoid bots scanning the internet launching attacks :)  

---

### Answer by username_1 on 2017-09-18. Score: 2

The way a cluster becomes aware of the EC2 instances associated to it, is a configuration file used by the ECS agent. You can modify this file, located at `/etc/ecs/ecs.config`, and name a different cluster. Then restart the ECS agent. This will effectively "move" the EC2 instance to the other cluster.

More information about `ecs.config` in the documentation - <http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-config.html>

After the modification, it is possible to verify by sending an HTTP request to the ECS Agent metadata service and see the new cluster displayed. <http://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-update.html>

The ECS cluster itself also needs to be notified. Can use the Register/Deregister API calls for this, for example using the AWS CLI:

* <http://docs.aws.amazon.com/cli/latest/reference/ecs/register-container-instance.html>
* <http://docs.aws.amazon.com/cli/latest/reference/ecs/deregister-container-instance.html>

More housekeeping might be required on the ECS agent itself. Like removing cached checkpoint files, etc. <https://github.com/aws/amazon-ecs-agent/issues/139#issuecomment-123061463>

> **Comments:**
> **username_0**: i modified `/etc/ecs/ecs.config` to `ECS_CLUSTER=devcluster` and restarted ec2 instance. but the instance still appear on `qacluster`  
> **username_1**: @sith I updated the answer with details about registration of instances in the ECS Cluster metadata. Might need to be updated as well, or else the cluster does not know about a new instance or about a removed one.  

---

### Answer by username_2 on 2021-05-26. Score: 0

Updated for ECS optimized AMI image as of May 2021. The sudo start command is not present and the checkpoint file /var/lib/ecs/data/ecs\_agent\_data.json changed to agent.db.

1. Edit `/etc/ecs/ecs.config` as previously described (add ECS\_CLUSTER=devcluster).
2. Rename the agent state file with `mv /var/lib/ecs/data/agent.db /var/lib/ecs/data/agent.db.old`
3. Restart the ECS agent using `sudo systemctl restart ecs`

Check the status of the agent with `systemctl status ecs`, you want it to display *Active: active (running)*.

This was a bit frustrating to figure out because the Amazon online documentation I found still displays the old information. Couldn't have done it without devops!


---

