今天我們要來學習 ECS Task Definition,這就像是為我們的交易程式寫一份詳細的「工作說明書」。還記得小時候幫爸爸種菜時,他總是會仔細告訴我每種作物需要多少水、多少肥料、種在哪裡?Task Definition 就是這樣的概念!
Task Definition 是 ECS 中的核心概念,它定義了:
graph TD
A[Task Definition<br/>工作說明書] --> B[Container Definitions<br/>容器定義]
A --> C[Task Role<br/>執行角色]
A --> D[Network Mode<br/>網路模式]
A --> E[CPU & Memory<br/>資源配置]
A --> F[Volumes<br/>儲存卷]
B --> G[Image URI<br/>容器映像]
B --> H[Port Mappings<br/>埠號對應]
B --> I[Environment Variables<br/>環境變數]
B --> J[Logging<br/>日誌設定]
style A fill:#e3f2fd
style B fill:#f3e5f5
style C fill:#fff3e0
style D fill:#e8f5e8
我們的量化交易系統將包含以下容器:
{
"family": "trading-bot-task",
"taskRoleArn": "arn:aws:iam::123456789012:role/ECSTaskRole",
"executionRoleArn": "arn:aws:iam::123456789012:role/ECSExecutionRole",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "512",
"memory": "1024",
"containerDefinitions": [
{
"name": "trading-bot",
"image": "123456789012.dkr.ecr.us-east-1.amazonaws.com/trading-bot:latest",
"portMappings": [
{
"containerPort": 8080,
"protocol": "tcp"
}
],
"environment": [
{
"name": "ENVIRONMENT",
"value": "production"
},
{
"name": "LOG_LEVEL",
"value": "INFO"
}
],
"secrets": [
{
"name": "BYBIT_API_KEY",
"valueFrom": "arn:aws:secretsmanager:us-east-1:123456789012:secret:trading-secrets:BYBIT_API_KEY"
},
{
"name": "BYBIT_SECRET_KEY",
"valueFrom": "arn:aws:secretsmanager:us-east-1:123456789012:secret:trading-secrets:BYBIT_SECRET_KEY"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/trading-bot",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs"
}
},
"healthCheck": {
"command": [
"CMD-SHELL",
"curl -f http://localhost:8080/health || exit 1"
],
"interval": 30,
"timeout": 5,
"retries": 3,
"startPeriod": 60
}
}
]
}
{
"cpu": "512", // 0.5 vCPU (512 CPU units)
"memory": "1024" // 1 GB RAM
}
為什麼選擇這個配置?
{
"networkMode": "awsvpc"
}
awsvpc 模式的優勢:
graph LR
A[環境變數管理] --> B[一般設定<br/>Environment]
A --> C[機密資訊<br/>Secrets]
B --> D[LOG_LEVEL<br/>ENVIRONMENT<br/>TIMEZONE]
C --> E[API_KEY<br/>SECRET_KEY<br/>DATABASE_PASSWORD]
C --> F[AWS Secrets Manager<br/>安全儲存]
style C fill:#ffcccc
style F fill:#ccffcc
{
"healthCheck": {
"command": [
"CMD-SHELL",
"curl -f http://localhost:8080/health || exit 1"
],
"interval": 30, // 每 30 秒檢查一次
"timeout": 5, // 5 秒超時
"retries": 3, // 失敗 3 次才判定不健康
"startPeriod": 60 // 啟動後 60 秒才開始檢查
}
}
{
"family": "trading-bot-dev",
"cpu": "256",
"memory": "512",
"containerDefinitions": [
{
"name": "trading-bot-dev",
"image": "trading-bot:dev",
"environment": [
{
"name": "ENVIRONMENT",
"value": "development"
},
{
"name": "LOG_LEVEL",
"value": "DEBUG"
}
]
}
]
}
{
"family": "trading-bot-prod",
"cpu": "1024",
"memory": "2048",
"containerDefinitions": [
{
"name": "trading-bot-prod",
"image": "trading-bot:latest",
"environment": [
{
"name": "ENVIRONMENT",
"value": "production"
},
{
"name": "LOG_LEVEL",
"value": "INFO"
}
]
}
]
}
# 註冊 Task Definition
aws ecs register-task-definition \
--cli-input-json file://task-definition.json
# 查看 Task Definition
aws ecs describe-task-definition \
--task-definition trading-bot-task
# 列出所有版本
aws ecs list-task-definitions \
--family-prefix trading-bot-task
# 更新時會建立新版本
aws ecs register-task-definition \
--cli-input-json file://task-definition-v2.json
# 查看特定版本
aws ecs describe-task-definition \
--task-definition trading-bot-task:2
graph LR
A[Git Tag] --> B[Docker Image Tag]
B --> C[Task Definition Revision]
C --> D[ECS Service Update]
A --> E[v1.0.0]
E --> F[trading-bot:v1.0.0]
F --> G[trading-bot-task:1]
style A fill:#e3f2fd
style D fill:#e8f5e8
CPU 和記憶體配置建議:
環境 | CPU | Memory | 適用場景 |
---|---|---|---|
開發 | 256 | 512MB | 基本測試 |
測試 | 512 | 1GB | 功能驗證 |
生產 | 1024 | 2GB | 正式交易 |
{
"secrets": [
{
"name": "DATABASE_PASSWORD",
"valueFrom": "arn:aws:secretsmanager:region:account:secret:name"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/trading-bot",
"awslogs-region": "us-east-1"
}
}
}
{
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/trading-bot",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs",
"awslogs-datetime-format": "%Y-%m-%d %H:%M:%S"
}
}
}
Task 無法啟動
健康檢查失敗
記憶體不足
# 查看 Task 運行狀態
aws ecs describe-tasks \
--cluster trading-cluster \
--tasks arn:aws:ecs:region:account:task/task-id
# 查看容器日誌
aws logs get-log-events \
--log-group-name /ecs/trading-bot \
--log-stream-name ecs/trading-bot/task-id
今天我們學習了如何為我們的量化交易程式寫一份詳細的「工作說明書」- Task Definition。就像爸爸教我種菜時會仔細說明每個步驟一樣,正確的 Task Definition 確保我們的容器能夠按照預期運行。
關鍵要記住的是:
明天我們將學習 ECS Service,了解如何讓我們的 Task Definition 實際運行起來!
下一篇:Day 7 - AWS ECS Service