[AWS ECS] 將建立的 EC2 instance 加入指定的 Cluster

Create Cluster

先到 ECS 的介面,選擇 Cluster 去建立一個新的 Cluster,Cluster name 輸入完以後可以直接建立

Launch Container Instance into cluster

http://docs.aws.amazon.com/AmazonECS/latest/developerguide/launch_container_instance.html

#!/bin/bash
echo ECS_CLUSTER=[CLUSTER_NAME] >> /etc/ecs/ecs.config # 不可以寫成 ecs_config 喔!
  • Windows 的話是:
shell
<powershell>
Import-Module ECSTools
Initialize-ECSAgent -Cluster ‘[CLUSTER_NAME]’ -EnableTaskIAMRole
</powershell>

NOTICE 1

etc/ecs/ecs.confog 這是 Optimized Linux 已經開好的檔案[由安裝 ecs-init 建立),使用別的 AMI 建立的 instance 沒有這個檔案,所以如果是其他 AMI 得話這步驟可以跳過,啟動 instance 後在 `sudo start ecs`[1] 之前再自己去建立並且填入資料

NOTICE 2

正常來說應該這樣就會在 Cluster 那邊見到有一個 Registered Container instances 了,但是卻沒有

bash
[ec2-user@ip-172–31–20–243 ~]$ cat /var/log/ecs/ecs-init.log
2017–11–21T09:07:12Z [INFO] Starting Amazon EC2 Container Service Agent
2017–11–21T09:07:14Z [INFO] Agent exited with code 1
2017–11–21T09:07:14Z [INFO] Container name: /ecs-agent
2017–11–21T09:07:14Z [INFO] Removing existing agent container ID: 3c5290616ea532736f2d8d02465b223a0d148eea4741b3453b7381f3ff563f26
2017–11–21T09:07:14Z [INFO] Starting Amazon EC2 Container Service Agent
2017–11–21T09:07:15Z [INFO] Agent exited with code 1
2017–11–21T09:07:15Z [INFO] Container name: /ecs-agent
2017–11–21T09:07:15Z [INFO] Removing existing agent container ID: 2f946d1903237e5380387032faef443e6a649e11f1066e7bceb7257420321949
2017–11–21T09:07:15Z [INFO] Starting Amazon EC2 Container Service Agent
2017–11–21T09:07:15Z [INFO] Agent exited with code 1
2017–11–21T09:07:15Z [INFO] Container name: /ecs-agent
2017–11–21T09:07:15Z [INFO] Removing existing agent container ID: 7d68f5125d14e73b820ce1eeef48f933c564e6ced416512ce7728ee61154ce2c
2017–11–21T09:07:15Z [INFO] Starting Amazon EC2 Container Service Agent
2017–11–21T09:07:16Z [INFO] Agent exited with code 1
2017–11–21T09:07:16Z [INFO] Container name: /ecs-agent
2017–11–21T09:07:16Z [INFO] Removing existing agent container ID: 8f69f81fa411317e28864da8a536b031cc2cc1bfbc6936400eb8e8b44abed603
2017–11–21T09:07:16Z [INFO] Starting Amazon EC2 Container Service Agent
.
.
.
$ [ec2-user@ip-172–31–20–243 ~]$ cat /var/log/ecs/ecs-agent.log.2017–11–21–09
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
2017–11–21T09:11:50Z [INFO] Registering Instance with ECS
2017–11–21T09:11:50Z [ERROR] Could not register: NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
2017–11–21T09:11:50Z [CRITICAL] Could not create cluster: NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
2017–11–21T09:11:50Z [ERROR] Error registering: NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
2017–11–21T09:11:51Z [INFO] Loading configuration
2017–11–21T09:11:51Z [INFO] Loading state! module=”statemanager”
2017–11–21T09:11:51Z [INFO] Event stream ContainerChange start listening…
2017–11–21T09:11:51Z [WARN] Error getting valid credentials (AKID ): NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
2017–11–21T09:11:51Z [INFO] Registering Instance with ECS
2017–11–21T09:11:51Z [ERROR] Could not register: NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
2017–11–21T09:11:51Z [CRITICAL] Could not create cluster: NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
2017–11–21T09:11:51Z [ERROR] Error registering: NoCredentialProviders: no valid providers in chain. Deprecated.
For verbose messaging see aws.Config.CredentialsChainVerboseErrors
.
.
.

NOTICE 3

如果是用一般的 AMI 起的 EC2 instance,到了 attach IAM role 完還是會沒有出現在 ECS cluster 中,這就是為什麼推薦使用 ECS optimized AMI 的原因

NOTICE 4

Windows 的 EC2 instance 註冊到 Cluster 需要一段時間 (5~10 min)

ecsInstanceRole IAM Role

{
“Version”: “2012–10–17”,
“Statement”: [
{
“Effect”: “Allow”,
“Action”: [
“ecs:CreateCluster”,
“ecs:DeregisterContainerInstance”,
“ecs:DiscoverPollEndpoint”,
“ecs:Poll”,
“ecs:RegisterContainerInstance”,
“ecs:StartTelemetrySession”,
“ecs:Submit*”,
“ecr:GetAuthorizationToken”,
“ecr:BatchCheckLayerAvailability”,
“ecr:GetDownloadUrlForLayer”,
“ecr:BatchGetImage”,
“logs:CreateLogStream”,
“logs:PutLogEvents”
],
“Resource”: “*”
}
]
}

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store