ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းပြရရင်
AWS Fundamentals မှာ EC2 instance တစ်လုံးပေါ်မှာ app ကို ကိုယ်တိုင် install လုပ်ပြီး run ခဲ့ပါတယ်။ ECS (Elastic Container Service) ကတော့ ခင်ဗျားရဲ့ app ကို container image အဖြစ် ထုပ်ပိုးထားပြီးရင် 'ဒီ image ကို copy ၂ ခု အမြဲ run ထားပေး၊ တစ်ခု သေသွားရင် အသစ် ပြန်တင်ပေး၊ load balancer နောက်မှာ ချိတ်ပေး' လို့ ပြောလိုက်ရုံနဲ့ လုပ်ပေးတဲ့ orchestrator တစ်ခုပါ။
Fargate ကတော့ အဲဒီ container တွေ run ဖို့ လိုတဲ့ server တွေကိုပါ AWS က ကိုင်ပေးတဲ့ mode ပါ။ Instance ရွေးစရာ၊ OS patch လုပ်စရာ၊ disk ပြည့်မှာ စိုးရိမ်စရာ မလိုတော့ပါဘူး။ Container တစ်ခုချင်းစီအတွက် CPU နဲ့ memory ဘယ်လောက်လိုလဲ ပြောလိုက်ရုံပါပဲ။ ဥပမာ တစ်ခုနဲ့ ပြောရရင် EC2 က ကားဝယ်ပြီး ကိုယ်တိုင်မောင်းတာဆိုရင် Fargate က taxi ငှားစီးတာနဲ့ တူပါတယ် — ခရီးသွားတဲ့ အချိန်အတွက်ပဲ ပေးရပြီး ကားပြင်တာ၊ ဆီထည့်တာက ကိုယ့်တာဝန် မဟုတ်တော့ပါဘူး။
Problem → Why → Solution
ပြဿနာ — 'အလုပ်ဖြစ်တယ်' ဆိုတဲ့ task definition က လုံခြုံမှု အပေါက်ကြီး ဖြစ်နေတာ
Team တစ်ခုက ECS ကို ပထမဆုံး စမ်းတဲ့အခါ အောက်က task definition လိုမျိုး ရေးလေ့ရှိပါတယ်။ App က DynamoDB ကို ခေါ်ဖို့ လိုတဲ့အခါ permission ဘယ်ကရမလဲ မသိလို့ developer တစ်ယောက်ရဲ့ access key ကို environment variable ထဲ ထည့်လိုက်ပါတယ်။ Database password ကိုလည်း plain text နဲ့ ထည့်ထားပါတယ်။ Image ကလည်း :latest tag ဖြစ်နေပြီး log configuration လည်း မပါပါဘူး။
Deploy လုပ်ကြည့်တော့ အလုပ်ဖြစ်ပါတယ်။ ဒါပေမဲ့ ecs:DescribeTaskDefinition permission ရှိတဲ့ ဘယ်သူမဆို password နဲ့ key ကို console မှာ ရှင်းရှင်းကြီး မြင်နိုင်ပါတယ်။ Container crash ဖြစ်ရင်လည်း ဘာကြောင့်လဲ ကြည့်စရာ log မရှိပါဘူး။
{
"family": "orders-api",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "512",
"memory": "1024",
"executionRoleArn": "arn:aws:iam::111122223333:role/ecsTaskExecutionRole",
"containerDefinitions": [
{
"name": "web",
"image": "111122223333.dkr.ecr.us-east-1.amazonaws.com/orders-api:latest",
"portMappings": [{ "containerPort": 8080 }],
"environment": [
{ "name": "DB_PASSWORD", "value": "SuperSecret123" },
{ "name": "AWS_ACCESS_KEY_ID", "value": "AKIAIOSFODNN7EXAMPLE" }
]
}
]
}ဘာကြောင့် ဒီလို ဖြစ်ရတာလဲ
ECS မှာ role နှစ်ခု ရှိတာကို မသိကြလို့ပါ။ Execution role က ECS agent ကိုယ်တိုင် image pull ဖို့၊ log ပို့ဖို့၊ secret ဖတ်ဖို့ သုံးတာဖြစ်ပြီး task role ကတော့ ခင်ဗျားရဲ့ app code က AWS API ခေါ်တဲ့အခါ သုံးတာပါ။ taskRoleArn ထည့်လိုက်ရင် SDK က temporary credential ကို အလိုအလျောက် ယူသွားလို့ access key ထည့်စရာ လုံးဝ မလိုပါဘူး။
အဓိက သဘောတရား — ECS ရဲ့ အစိတ်အပိုင်း လေးခု
- Task definition
- Container တစ်ခု သို့မဟုတ် အများကို ဘယ်လို run ရမလဲဆိုတဲ့ versioned JSON blueprint ပါ — image၊ CPU/memory၊ port၊ environment၊ secret၊ log setting နဲ့ role နှစ်ခု။ ပြင်တိုင်း revision အသစ် (orders-api:7, :8 ...) ထွက်ပြီး အဟောင်းကို ပြင်လို့ မရပါဘူး။
ECS မှာ အရာ လေးခုပဲ နားလည်ဖို့ လိုပါတယ်။ Cluster က task တွေ နေထိုင်တဲ့ logical နယ်မြေ (namespace) တစ်ခုသာ ဖြစ်ပြီး Fargate သုံးရင် သူ့ထဲမှာ ကိုယ်ပိုင် server မရှိပါဘူး။ Task definition က blueprint ပါ။ Task ကတော့ အဲဒီ blueprint ကနေ run နေတဲ့ instance တစ်ခု — container တစ်ခု သို့မဟုတ် အတူတူ နေရမယ့် container အုပ်စု (ဥပမာ app နဲ့ log sidecar) ပါ။
Service က အရေးအကြီးဆုံးပါ။ 'orders-api:7 task ကို ၂ ခု အမြဲ run ထား' ဆိုတဲ့ desired state ကို ထိန်းပေးတဲ့ controller ဖြစ်ပါတယ်။ Task တစ်ခု သေသွားရင်၊ health check fail ရင် service က အသစ် တင်ပေးပြီး ALB target group ထဲမှာ register/deregister လုပ်ပေးပါတယ်။ Batch job လိုမျိုး တစ်ခါ run ပြီး ပြီးသွားမယ့် အလုပ်ဆိုရင်တော့ service မလိုဘဲ run-task နဲ့ standalone task အဖြစ် run ပါတယ်။
ECS SERVICE ON FARGATE BEHIND AN ALB
------------------------------------
+---------------- cluster: demo-cluster ----------------+
Internet | service: orders-api (desired=2, taskdef orders-api:7) |
| | |
v | +------------------+ +------------------+ |
+-----+ TG | | task (AZ a) | | task (AZ b) | |
| ALB |--(ip)--+-->| ENI 10.0.11.23 | | ENI 10.0.12.41 | |
+-----+ | | [web container] | | [web container] | |
| | +------------------+ +------------------+ |
+-----------+---------------------------------------^ |
| capacity: FARGATE / FARGATE_SPOT (AWS runs hosts) |
+-------------------------------------------------------+Fargate မှာ task တိုင်းက awsvpc network mode ကို သုံးပြီး ကိုယ်ပိုင် ENI (elastic network interface) နဲ့ private IP တစ်ခုစီ ရပါတယ်။ ဒါကြောင့် security group ကို task အဆင့်မှာ တိုက်ရိုက် တပ်လို့ရပြီး EC2 instance တစ်လုံးပေါ်က container အားလုံး security group တစ်ခုတည်း မျှသုံးရတဲ့ ပြဿနာ မရှိတော့ပါဘူး။ အဲဒီ IP ကို ALB က တိုက်ရိုက် ပို့ရမှာမို့ target group ကို target type 'ip' နဲ့ ဖန်တီးရပါတယ်။
Capacity ကတော့ ဘယ်မှာ run မလဲ ဆိုတာပါ။ Capacity provider strategy နဲ့ FARGATE (on-demand) နဲ့ FARGATE_SPOT ကို ရောသုံးနိုင်ပါတယ်။ Spot က ဈေး အများကြီး သက်သာပေမဲ့ AWS က capacity ပြန်လိုရင် နှစ်မိနစ် သတိပေးပြီး task ကို ရပ်ပစ်နိုင်ပါတယ်။ 'base' က on-demand မှာ အနည်းဆုံး ထားရမယ့် task အရေအတွက် ဖြစ်ပြီး 'weight' က ကျန်တဲ့ task တွေကို ဘယ်လို အချိုးနဲ့ ခွဲမလဲ သတ်မှတ်ပါတယ်။
| Fargate | EC2 capacity | |
|---|---|---|
| Server စီမံခန့်ခွဲမှု | AWS က လုပ်ပေး | AMI၊ patch၊ scaling ကိုယ်တိုင် |
| ငွေတောင်းခံပုံ | Task ရဲ့ vCPU/GB ကို စက္ကန့်အလိုက် | Instance တစ်လုံးလုံးအတွက် (ဗလာ နေရာပါ) |
| GPU / privileged / host access | မရပါ | ရပါတယ် |
| သင့်တော်တဲ့ နေရာ | Web/API၊ worker၊ team အသေး | GPU၊ ကြီးမားပြီး တည်ငြိမ်တဲ့ fleet |
EC2 capacity ကို ဘယ်အချိန် ရွေးသင့်လဲ — GPU လိုတဲ့ inference၊ privileged container လိုတဲ့ tool တွေ၊ ဒါမှမဟုတ် 24 နာရီ load တည်ငြိမ်ပြီး instance တွေကို Savings Plan နဲ့ ပြည့်ပြည့်ဝဝ သုံးနိုင်တဲ့ fleet ကြီးတွေမှာပါ။ ကျန်တဲ့ ကိစ္စ အများစုမှာတော့ Fargate ရဲ့ ဈေးနည်းနည်း ပိုမြင့်တာက ops အလုပ် လျော့သွားတာနဲ့ ကျေနပ်လောက်တဲ့ အပေးအယူ ဖြစ်ပါတယ်။
- Task definition = blueprint
- Service = desired count ထိန်းသူ
- Task = ENI ပါ container အုပ်စု
- Capacity provider = ဘယ်မှာ run မလဲ
- ALB target group = traffic ပို့ရာ
Service ကို 'ဘာ ဖြစ်ချင်လဲ' ပြောပြီး ECS က 'ဘယ်လို ဖြစ်အောင်လုပ်မလဲ' ကို ဆက်တိုက် ထိန်းပေးတဲ့ control loop လို့ မြင်ကြည့်ပါ။
Production-ready task definition ပုံစံ
{
"family": "orders-api",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "512",
"memory": "1024",
"runtimePlatform": { "cpuArchitecture": "ARM64", "operatingSystemFamily": "LINUX" },
"executionRoleArn": "arn:aws:iam::111122223333:role/orders-api-execution",
"taskRoleArn": "arn:aws:iam::111122223333:role/orders-api-task",
"containerDefinitions": [
{
"name": "web",
"image": "111122223333.dkr.ecr.us-east-1.amazonaws.com/orders-api:1.4.2",
"essential": true,
"portMappings": [{ "containerPort": 8080, "protocol": "tcp" }],
"secrets": [
{
"name": "DB_PASSWORD",
"valueFrom": "arn:aws:secretsmanager:us-east-1:111122223333:secret:db-AbCdEf"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/orders-api",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "web"
}
},
"healthCheck": {
"command": ["CMD-SHELL", "curl -f http://localhost:8080/healthz || exit 1"],
"interval": 15,
"timeout": 5,
"retries": 3,
"startPeriod": 30
}
}
]
}Role နှစ်ခုကို ခွဲထားပြီး password ကို 'secrets' ကနေ Secrets Manager ARN နဲ့ ရည်ညွှန်းပါတယ် — ECS က task စတဲ့အချိန် execution role နဲ့ ဖတ်ပြီး env var အဖြစ် ထည့်ပေးလို့ task definition ထဲမှာ value မပေါ်ပါဘူး။ Image tag က version အတိအကျ ဖြစ်ပြီး awslogs driver က stdout ကို CloudWatch Logs ဆီ ပို့ပါတယ်။ ARM64 ကို ရွေးရင် image ကို arm64 အတွက် build လုပ်ထားဖို့ လိုပါတယ်။ Health check command သုံးဖို့ image ထဲမှာ curl ပါရပါမယ်။
Fargate service တစ်ခုကို အစအဆုံး deploy လုပ်ခြင်း
ဒီ script က VPC၊ private subnet နှစ်ခု၊ ALB နဲ့ listener ရှိပြီးသား (AWS Fundamentals lab ကလို) လို့ ယူဆပါတယ်။ Private subnet တွေကနေ ECR နဲ့ CloudWatch Logs ကို ရောက်ဖို့ NAT Gateway သို့မဟုတ် VPC endpoint တွေ ရှိရပါမယ်။ Script က cluster၊ log group၊ task definition၊ target group နဲ့ service ကို အစဉ်လိုက် ဖန်တီးပြီး service stable ဖြစ်တဲ့အထိ စောင့်ပါတယ်။
#!/usr/bin/env bash
set -euo pipefail
CLUSTER=demo-cluster
VPC_ID=vpc-0a1b2c3d4e5f67890
SUBNETS=subnet-0a1b2c3d4e5f60001,subnet-0a1b2c3d4e5f60002
TASK_SG=sg-0a1b2c3d4e5f60003
aws ecs create-cluster --cluster-name $CLUSTER \
--capacity-providers FARGATE FARGATE_SPOT \
--settings name=containerInsights,value=enabled
aws logs create-log-group --log-group-name /ecs/orders-api
aws logs put-retention-policy --log-group-name /ecs/orders-api --retention-in-days 30
TASKDEF_ARN=$(aws ecs register-task-definition --cli-input-json file://taskdef.json \
--query 'taskDefinition.taskDefinitionArn' --output text)
TG_ARN=$(aws elbv2 create-target-group --name orders-api-tg \
--protocol HTTP --port 8080 --vpc-id $VPC_ID --target-type ip \
--health-check-path /healthz \
--query 'TargetGroups[0].TargetGroupArn' --output text)
# then forward an ALB listener rule to $TG_ARN (aws elbv2 create-rule)
NETCFG="awsvpcConfiguration={subnets=[$SUBNETS],securityGroups=[$TASK_SG],"
NETCFG+="assignPublicIp=DISABLED}"
DEPLOY="deploymentCircuitBreaker={enable=true,rollback=true},"
DEPLOY+="maximumPercent=200,minimumHealthyPercent=100"
aws ecs create-service --cluster $CLUSTER --service-name orders-api \
--task-definition "$TASKDEF_ARN" --desired-count 2 \
--capacity-provider-strategy capacityProvider=FARGATE,base=2,weight=1 \
capacityProvider=FARGATE_SPOT,weight=3 \
--network-configuration "$NETCFG" \
--load-balancers targetGroupArn=$TG_ARN,containerName=web,containerPort=8080 \
--health-check-grace-period-seconds 60 \
--deployment-configuration "$DEPLOY" \
--enable-execute-command
aws ecs wait services-stable --cluster $CLUSTER --services orders-api
aws ecs describe-services --cluster $CLUSTER --services orders-api \
--query 'services[0].{status:status,running:runningCount,desired:desiredCount}'Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- create-cluster
--capacity-providers FARGATE FARGATE_SPOTCluster ကို Fargate နဲ့ Fargate Spot capacity provider နှစ်ခုလုံး သုံးခွင့်ပေးပြီး Container Insights ကို ဖွင့်ထားပါတယ် — task အလိုက် CPU/memory metric တွေ ရပါမယ်။
- log group
put-retention-policy ... --retention-in-days 30Log group ကို ကြိုဖန်တီးပြီး retention သတ်မှတ်ပါတယ်။ Default က 'never expire' ဖြစ်လို့ မသတ်မှတ်ရင် log storage ကုန်ကျစရိတ် အမြဲ တက်နေပါမယ်။
- target group
--target-type ipawsvpc task တွေမှာ ကိုယ်ပိုင် IP ရှိလို့ target type က 'ip' ဖြစ်ရပါမယ်။ 'instance' နဲ့ ဖန်တီးမိရင် create-service က fail ပါလိမ့်မယ်။
- capacity strategy
capacityProvider=FARGATE,base=2,weight=1 capacityProvider=FARGATE_SPOT,weight=3ပထမ task ၂ ခုက on-demand Fargate မှာ အမြဲ run ပြီး အဲဒီထက် ပိုလာတဲ့ task တွေကို 1:3 အချိုး (25% on-demand, 75% Spot) နဲ့ ခွဲပါတယ်။ Scale out ဖြစ်ရင် ဈေးသက်သာပြီး Spot ပြတ်တောက်လည်း အနည်းဆုံး ၂ ခု ကျန်နေပါတယ်။
- circuit breaker
deploymentCircuitBreaker={enable=true,rollback=true}Deployment အသစ်က task တွေ ထပ်ခါထပ်ခါ start မဖြစ်ရင် ECS က deployment ကို FAILED လို့ သတ်မှတ်ပြီး နောက်ဆုံး အောင်မြင်ခဲ့တဲ့ revision ကို အလိုအလျောက် ပြန်သွားပါတယ်။ minimumHealthyPercent=100 ကြောင့် task အဟောင်းတွေကို အသစ် healthy မဖြစ်မချင်း မဖြုတ်ပါဘူး။
- ECS Exec + wait
--enable-execute-command / wait services-stableECS Exec ကို ကြိုဖွင့်ထားမှ နောက်မှ task ထဲ shell ဝင်လို့ရပါတယ် (task ပြီးမှ ဖွင့်လို့ မရ၊ task အသစ် လိုပါတယ်)။ wait က running count = desired count ဖြစ်တဲ့အထိ စောင့်ပါတယ်။
နောက်ဆုံး command ရဲ့ sample output:
{
"status": "ACTIVE",
"running": 2,
"desired": 2
}- update-service: revision :8
- Task အသစ် start (max 200%)
- ALB health check စောင့်
- Task အဟောင်း drain + stop
Task အသစ်တွေ healthy
COMPLETED
Task အသစ်တွေ ထပ်ခါ fail
FAILED -> rollback to :7
Task role နဲ့ task execution role — ဘယ်သူက ဘာအတွက်လဲ
| Task execution role | Task role | |
|---|---|---|
| ဘယ်သူ သုံးလဲ | ECS agent / Fargate platform | Container ထဲက ခင်ဗျားရဲ့ app |
| ဘယ်အချိန် | Task စတင်ချိန် + log ပို့ချိန် | App run နေတဲ့ တစ်လျှောက် |
| ပုံမှန် permission | ECR pull၊ logs၊ secret ဖတ် | DynamoDB၊ S3၊ SQS၊ ECS Exec |
Error message ကနေ ဘယ် role ပြဿနာလဲ ခွဲလို့ရပါတယ်။ Task က PENDING ကနေ STOPPED ဖြစ်ပြီး 'CannotPullContainerError' သို့မဟုတ် 'ResourceInitializationError: unable to pull secrets' ဆိုရင် execution role (သို့မဟုတ် network) ပြဿနာပါ။ Task run နေပြီး app log ထဲမှာ 'AccessDeniedException' တွေ့ရင်တော့ task role ပြဿနာပါ။
Role နှစ်ခုလုံးရဲ့ trust policy မှာ principal က ecs-tasks.amazonaws.com ဖြစ်ပါတယ်။ အောက်က task role permission က app ရဲ့ DynamoDB table တစ်ခုတည်းနဲ့ ECS Exec အတွက် လိုတဲ့ ssmmessages action တွေကိုသာ ခွင့်ပြုထားပါတယ်။
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AppTable",
"Effect": "Allow",
"Action": ["dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:Query"],
"Resource": "arn:aws:dynamodb:us-east-1:111122223333:table/orders"
},
{
"Sid": "EcsExec",
"Effect": "Allow",
"Action": [
"ssmmessages:CreateControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:OpenDataChannel"
],
"Resource": "*"
}
]
}Service auto scaling
ECS service ကို Application Auto Scaling နဲ့ scale လုပ်ပါတယ်။ AWS Fundamentals က EC2 Auto Scaling group နဲ့ သဘောတရားချင်း တူပေမဲ့ instance မဟုတ်ဘဲ service ရဲ့ desired count ကို ပြောင်းပေးတာပါ။ Target tracking က အလွယ်ဆုံးပါ — 'ပျမ်းမျှ CPU 60% ဝန်းကျင် ထိန်းထား' လို့ ပြောလိုက်ရင် CloudWatch alarm တွေကို ကိုယ်တိုင် ဖန်တီးပေးပါတယ်။
Web API အတွက် CPU ထက် ALBRequestCountPerTarget က ပိုကောင်းလေ့ ရှိပါတယ် — load တက်လာတာကို CPU မမြင့်ခင် စောစော သိနိုင်လို့ပါ။ Scale-in cooldown ကို scale-out ထက် ပိုရှည်အောင် ထားတာက task တွေ ခဏခဏ အတက်အကျ မဖြစ်အောင် ကာကွယ်ပါတယ်။
RESOURCE_ID=service/demo-cluster/orders-api
aws application-autoscaling register-scalable-target \
--service-namespace ecs --resource-id $RESOURCE_ID \
--scalable-dimension ecs:service:DesiredCount \
--min-capacity 2 --max-capacity 10
cat > cpu60.json <<'EOF'
{
"TargetValue": 60.0,
"PredefinedMetricSpecification": {
"PredefinedMetricType": "ECSServiceAverageCPUUtilization"
},
"ScaleOutCooldown": 60,
"ScaleInCooldown": 300
}
EOF
aws application-autoscaling put-scaling-policy --policy-name cpu60 \
--service-namespace ecs --resource-id $RESOURCE_ID \
--scalable-dimension ecs:service:DesiredCount \
--policy-type TargetTrackingScaling \
--target-tracking-scaling-policy-configuration file://cpu60.jsonECS Exec နဲ့ log တွေကို သုံးပြီး debug လုပ်ခြင်း
Fargate မှာ SSH ဝင်စရာ host မရှိပါဘူး။ ECS Exec က Systems Manager Session Manager ကို နောက်ကွယ်ကနေ သုံးပြီး run နေတဲ့ container ထဲကို shell ဖွင့်ပေးပါတယ်။ လိုအပ်ချက် သုံးခု ရှိပါတယ် — service မှာ --enable-execute-command ဖွင့်ထားရမယ်၊ task role မှာ ssmmessages permission ရှိရမယ်၊ ကိုယ့်စက်မှာ Session Manager plugin install လုပ်ထားရမယ်။ Session တိုင်းကို CloudTrail မှာ မှတ်တမ်းတင်ပြီး output ကို CloudWatch Logs သို့မဟုတ် S3 ဆီ log လုပ်ဖို့ cluster မှာ configure လုပ်နိုင်ပါတယ်။
Log ကြည့်ဖို့ CLI v2 မှာ 'aws logs tail /ecs/orders-api --follow' က အဆင်ပြေဆုံးပါ။ Error ရှာချင်ရင် filter-log-events ကို pattern နဲ့ သုံးပါ။
TASK_ARN=$(aws ecs list-tasks --cluster demo-cluster --service-name orders-api \
--query 'taskArns[0]' --output text)
# why did a task stop? (stoppedReason is the first thing to read)
aws ecs describe-tasks --cluster demo-cluster --tasks $TASK_ARN \
--query 'tasks[0].{last:lastStatus,reason:stoppedReason}'
# open a shell in the running "web" container
aws ecs execute-command --cluster demo-cluster --task $TASK_ARN \
--container web --interactive --command "/bin/sh"
# recent ERROR lines from the container logs
aws logs filter-log-events --log-group-name /ecs/orders-api \
--filter-pattern ERROR --max-items 20Production မှာ
Production မှာ တကယ် ဘယ်လို သုံးကြလဲ
Team အများစုက ECS resource တွေကို console ကနေ မဖန်တီးပါဘူး။ Cluster၊ service၊ role နဲ့ ALB ကို CDK သို့မဟုတ် Terraform နဲ့ ရေးပြီး CI pipeline က image ကို git commit SHA နဲ့ tag လုပ်၊ ECR ကို push၊ task definition revision အသစ် register ပြီး update-service ခေါ်ပါတယ်။ ဒါကြောင့် revision တိုင်းက commit တစ်ခုနဲ့ တိုက်ရိုက် ချိတ်နေပြီး rollback လုပ်ချင်ရင် revision အဟောင်းကို ပြန်ညွှန်ရုံပါပဲ။
VPC endpoint တွေကလည်း production ရဲ့ အရေးကြီးတဲ့ အစိတ်အပိုင်းပါ။ Private subnet ထဲက task တွေက NAT Gateway ကနေ image pull ရင် GB တိုင်းအတွက် processing fee ပေးရပါတယ်။ ECR (api နဲ့ dkr)၊ S3 gateway endpoint နဲ့ CloudWatch Logs endpoint တွေ ထည့်ထားရင် ဈေးသက်သာရုံသာမက internet ထွက်လမ်းကိုလည်း ပိတ်ထားနိုင်ပါတယ်။
- Fargate Spot ကို stateless worker၊ queue consumer နဲ့ dev/test environment တွေမှာ အဓိက သုံးကြပြီး customer-facing API မှာ base နဲ့ on-demand ကို ရောပါတယ်။
- App က SIGTERM ကို ဖမ်းပြီး in-flight request တွေ ပြီးအောင် လုပ်ရပါမယ် — Spot interruption နဲ့ deployment နှစ်ခုလုံးမှာ task ကို SIGTERM နဲ့ ရပ်လို့ပါ။
- ALB target group ရဲ့ deregistration delay ကို default 300 စက္ကန့်ကနေ request အရှည်ဆုံး အချိန်နဲ့ ကိုက်အောင် လျှော့ကြပါတယ် — deploy တွေ ပိုမြန်လာပါတယ်။
- Blue/green deployment လိုရင် CodeDeploy နဲ့ ECS ကို တွဲသုံးကြပါတယ်။ ECS မှာလည်း built-in blue/green deployment option ထွက်လာပါပြီ — ကိုယ်သုံးတဲ့ tool က support လုပ်လား စစ်ပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
အမှား ၁ — Private subnet ကနေ image pull မရ
assignPublicIp=DISABLED နဲ့ private subnet ထဲ task တင်ပြီး NAT Gateway လည်း မရှိ၊ VPC endpoint လည်း မရှိရင် task တွေ 'CannotPullContainerError' နဲ့ ရပ်သွားပါတယ်။ ECR image layer တွေက S3 မှာ သိမ်းထားလို့ ecr.api နဲ့ ecr.dkr interface endpoint အပြင် S3 gateway endpoint ပါ လိုတာကို မေ့လေ့ရှိကြပါတယ်။ Log ပို့ဖို့ logs endpoint နဲ့ secret ဖတ်ဖို့ secretsmanager endpoint လည်း လိုပါတယ်။
အမှား ၂ — Health check grace period မရှိလို့ deploy တိုင်း rollback
Java သို့မဟုတ် Spring app လို စတင်ဖို့ စက္ကန့် ၄၀–၆၀ ကြာတဲ့ app မှာ health-check-grace-period မထားရင် ALB က task ကို စတင်ပြီး ခဏအကြာမှာ unhealthy လို့ ဆုံးဖြတ်ပြီး ECS က ရပ်ပစ်ပါတယ်။ Circuit breaker က ဒါကို 'deployment fail' လို့ မြင်ပြီး rollback လုပ်လို့ code မှာ ဘာမှ မမှားပါဘဲ deploy မအောင်မြင်တာ ဖြစ်ပါတယ်။ Grace period ကို app စတင်ချိန်ထက် နည်းနည်း ပိုထားပါ။
အမှား ၃ — Permission ကို မှားတဲ့ role ထဲ ထည့်ခြင်း
App က S3 ကို ဖတ်ဖို့ AccessDenied ရတဲ့အခါ s3:GetObject ကို execution role ထဲ ထည့်ပြီး ဘာလို့ အလုပ်မဖြစ်လဲ နာရီပေါင်းများစွာ ရှာနေတတ်ကြပါတယ်။ App code က task role ကိုပဲ သုံးပါတယ်။ ပြောင်းပြန်လည်း မှားတတ်ပါတယ် — secret ဖတ်ဖို့ permission ကို task role ထဲ ထည့်ရင် 'secrets' field က အလုပ်မဖြစ်ပါဘူး၊ execution role ထဲမှာ ရှိရပါမယ်။
Best Practices
အကောင်းဆုံး လုပ်ထုံးလုပ်နည်းများ
- Service တစ်ခုစီအတွက် task role သီးသန့် ထားပြီး least privilege ပေးပါ။
- Image ကို version tag သို့မဟုတ် digest နဲ့ ညွှန်ပါ — :latest မသုံးပါနဲ့။
- Circuit breaker ကို rollback=true နဲ့ အမြဲ ဖွင့်ထားပါ။
- Log group တိုင်းမှာ retention သတ်မှတ်ပါ။
- Production မှာ desired count ကို အနည်းဆုံး ၂ ထားပြီး subnet တွေကို AZ မတူအောင် ရွေးပါ။
💰 Cost & Cleanup
Fargate မှာ free tier မရှိပါဘူး — task ရဲ့ vCPU နဲ့ memory ကို image pull စတဲ့အချိန်ကနေ task ရပ်တဲ့အထိ စက္ကန့်အလိုက် (အနည်းဆုံး ၁ မိနစ်) ငွေတောင်းပါတယ်။ Fargate Spot က အဲဒီထက် အများကြီး သက်သာပါတယ်။ ALB က နာရီအလိုက် + LCU အလိုက်၊ NAT Gateway က နာရီအလိုက် + GB အလိုက်၊ CloudWatch Logs က ingest လုပ်တဲ့ GB အလိုက် ကျပါတယ်။ ဒီ lab ကို ပြီးတာနဲ့ အောက်က command တွေနဲ့ ချက်ချင်း ဖျက်ပါ — run နေတဲ့ task ၂ ခုနဲ့ ALB က ၂၄ နာရီ ကျန်နေရင် စရိတ် စုလာပါမယ်။ ဈေးနှုန်း အတိအကျကို AWS pricing page မှာ စစ်ပါ။
စမ်းပြီးရင် ဖျက်ဖို့ command
aws application-autoscaling deregister-scalable-target --service-namespace ecs \
--resource-id service/demo-cluster/orders-api \
--scalable-dimension ecs:service:DesiredCount
aws ecs delete-service --cluster demo-cluster --service orders-api --force
aws ecs wait services-inactive --cluster demo-cluster --services orders-api
aws ecs deregister-task-definition --task-definition orders-api:1
aws ecs delete-cluster --cluster demo-cluster
aws elbv2 delete-target-group --target-group-arn $TG_ARN
aws logs delete-log-group --log-group-name /ecs/orders-apiMini challenge
Mini challenge — CDK နဲ့ Fargate service
ဒီ lesson က CLI deployment ကို AWS CDK app တစ်ခုအဖြစ် ပြန်ရေးပါ။ ALB နောက်မှာ Fargate service တစ်ခု run ပြီး production setting တွေ ပါရပါမယ်။
- Task ၂ ခုကို on-demand Fargate မှာ base အဖြစ် ထားပြီး ကျန်တာကို Fargate Spot 1:3 နဲ့ ခွဲပါ
- Circuit breaker ကို rollback နဲ့ ဖွင့်ပြီး ECS Exec ကို enable လုပ်ပါ
- CPU 60% target tracking နဲ့ task ၂ ခုကနေ ၁၀ ခုအထိ scale လုပ်ပါ
ရလဒ်: cdk synth က ECS cluster၊ service (capacity provider strategy နှစ်ခု၊ DeploymentCircuitBreaker ပါ)၊ ALB၊ target group နဲ့ ScalableTarget/ScalingPolicy ပါတဲ့ template ကို ထုတ်ပေးပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
aws-ecs-patterns ထဲက ApplicationLoadBalancedFargateService ကို သုံးပါ။ Cluster မှာ enableFargateCapacityProviders: true ထည့်ဖို့ မမေ့ပါနဲ့။
အဖြေ ကြည့်ရန်
import { App, Stack } from "aws-cdk-lib";
import * as ec2 from "aws-cdk-lib/aws-ec2";
import * as ecs from "aws-cdk-lib/aws-ecs";
import * as ecsPatterns from "aws-cdk-lib/aws-ecs-patterns";
const app = new App();
const stack = new Stack(app, "OrdersApiStack");
const vpc = new ec2.Vpc(stack, "Vpc", { maxAzs: 2, natGateways: 1 });
const cluster = new ecs.Cluster(stack, "Cluster", {
vpc,
enableFargateCapacityProviders: true,
});
const svc = new ecsPatterns.ApplicationLoadBalancedFargateService(stack, "Api", {
cluster,
cpu: 512,
memoryLimitMiB: 1024,
desiredCount: 2,
taskImageOptions: {
// pin a version tag or digest in production
image: ecs.ContainerImage.fromRegistry("public.ecr.aws/nginx/nginx:stable"),
containerPort: 80,
},
circuitBreaker: { rollback: true },
enableExecuteCommand: true,
capacityProviderStrategies: [
{ capacityProvider: "FARGATE", base: 2, weight: 1 },
{ capacityProvider: "FARGATE_SPOT", weight: 3 },
],
});
svc.targetGroup.configureHealthCheck({ path: "/" });
const scaling = svc.service.autoScaleTaskCount({ minCapacity: 2, maxCapacity: 10 });
scaling.scaleOnCpuUtilization("Cpu60", { targetUtilizationPercent: 60 });
app.synth();Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Service က desired count ကို ထိန်းပြီး task definition revision တွေက immutable blueprint ဖြစ်ပါတယ်။
- Execution role = image/log/secret အတွက် ECS agent၊ task role = app code အတွက်။
- Fargate task တိုင်း ကိုယ်ပိုင် ENI ရလို့ security group ကို task အဆင့်မှာ တပ်ပြီး target type 'ip' သုံးရပါတယ်။
- base + weight capacity strategy နဲ့ Fargate Spot ကို လုံခြုံစွာ ရောသုံးနိုင်ပါတယ်။
- Circuit breaker rollback၊ grace period နဲ့ ECS Exec က deploy နဲ့ debug ကို ဘေးကင်းစေပါတယ်။
Amazon ECS Developer Guide — AWS Documentation
AWS Fargate for Amazon ECS — AWS Documentation
Monitor Amazon ECS containers with ECS Exec — AWS Documentation