ရိုးရိုးလေး ရှင်းပြမယ်
ဆိုင်ဝန်ထမ်း အလှည့်ပြောင်းသလိုပါပဲ
ဆိုင်တစ်ဆိုင်မှာ အလှည့်ပြောင်းတဲ့အခါ အလှည့်သစ် ဝန်ထမ်းတွေ ရောက်လာပြီး အဟောင်းတွေနဲ့ ခဏ အတူတူ လုပ်ကြတယ်။ အသစ်တွေ အဆင်ပြေတာ သေချာမှ အဟောင်းတွေ ပြန်တယ်။ အသစ်တွေ နေမကောင်းဖြစ်နေရင် အဟောင်းတွေ ဆက်နေပြီး ဆိုင်ကို ဘယ်တော့မှ မပိတ်ရဘူး။ ဒါက rolling update နဲ့ circuit breaker ပါ။
Blue/green ကတော့ ဘေးမှာ တူညီတဲ့ ဆိုင်ခွဲတစ်ခု ဖွင့်ပြီး customer ၁၀% ကို အရင်ပို့ကြည့်တာပါ။ အဆင်ပြေရင် အားလုံးကို ပြောင်းပို့တယ်၊ ဆိုင်ဟောင်းကို ၁၅ မိနစ်လောက် ဖွင့်ထားသေးလို့ ပြဿနာရှိရင် ချက်ချင်း ပြန်လှည့်လို့ ရပါတယ်။
Problem → Why → Solution
':latest' ကို ထပ်ရေးပြီး အားလုံးကို restart လုပ်ခြင်း
AWS Fundamentals မှာ ALB နဲ့ Auto Scaling ကို သင်ခဲ့ပြီးပါပြီ။ Container ပြောင်းလာတဲ့ team တော်တော်များများရဲ့ ပထမ 'deploy' က အောက်ကလို ဖြစ်ပါတယ် - developer laptop ကနေ ':latest' ကို ထပ်ရေးပြီး force-new-deployment လုပ်တာပါ။ CI ထဲမှာ ဆိုရင်လည်း IAM user ရဲ့ access key ကို GitHub secret ထဲ နှစ်နဲ့ချီ ထားထားတတ်ပါတယ်။
ဒီနည်းမှာ အဖြေမရှိတဲ့ မေးခွန်း သုံးခု ရှိပါတယ်။ အခု ဘယ် commit run နေလဲ? ဘာကို rollback လုပ်မလဲ - ':latest' က version ဟောင်းကို မညွှန်တော့ပါဘူး။ Image အသစ်က start မဖြစ်ရင် ဘယ်သူ ရပ်ပေးမလဲ?
# "Deploy" = overwrite :latest and restart everything at once
docker build -t 111122223333.dkr.ecr.us-east-1.amazonaws.com/web:latest .
docker push 111122223333.dkr.ecr.us-east-1.amazonaws.com/web:latest
aws ecs update-service --cluster web --service web --force-new-deployment
# Which code is running now? Which commit was running an hour ago?
# What do we roll back TO? ":latest" no longer points at it.ဘာကြောင့် အန္တရာယ်များလဲ
Mutable tag ဆိုတော့ artifact နဲ့ code ချိတ်ဆက်မှု ပျောက်သွားတယ်။ Long-lived key ဆိုတော့ leak ဖြစ်ရင် ဘယ်သူမဆို deploy လုပ်နိုင်တယ်။ Rollout ကို စောင့်ကြည့်တဲ့ automation မရှိတော့ health check fail တဲ့ task တွေ အဆက်မပြတ် restart ဖြစ်နေပြီး လူတစ်ယောက် သတိထားမိမှ ရပ်ပါတယ်။
Deployment safety ရဲ့ အလွှာ သုံးခု
ပထမအလွှာက immutable artifact ပါ။ Image ကို git commit SHA နဲ့ tag လုပ်ပြီး ECR repository မှာ tag immutability ဖွင့်ထားရင် tag တစ်ခုက image တစ်ခုတည်းကိုပဲ အမြဲညွှန်ပါတယ်။ Task definition ထဲမှာ SHA ပါတော့ 'ဘာ run နေလဲ' ကို အမြဲဖြေနိုင်ပြီး rollback ဆိုတာ SHA ဟောင်းကို ပြန် deploy တာပဲ ဖြစ်ပါတယ်။
ဒုတိယအလွှာက CI identity ပါ။ GitHub Actions က job တိုင်းအတွက် OIDC token ထုတ်ပေးပြီး IAM role က 'ဒီ repo ရဲ့ production environment' ကလာတဲ့ token ကိုပဲ ယုံပါတယ်။ Credential က တစ်နာရီအတွင်း သက်တမ်းကုန်ပြီး သိမ်းထားစရာ secret မရှိပါဘူး။
တတိယအလွှာက rollout ကိုယ်တိုင်ပါ။ minHealthyPercent 100 နဲ့ maxHealthyPercent 200 ဆိုတော့ ECS က task အသစ်တွေကို အရင်တင်ပြီး ALB health check အောင်မှ အဟောင်းတွေကို ဖြုတ်ပါတယ်။ Deployment circuit breaker က task အသစ်တွေ steady state မရောက်ဘဲ ထပ်ခါထပ်ခါ fail ရင် deployment ကို fail လုပ်ပြီး နောက်ဆုံး အောင်မြင်ခဲ့တဲ့ version ကို rollback လုပ်ပါတယ်။ Circuit breaker က 'start မဖြစ်ဘူး' ကို ဖမ်းပါတယ်။ ဒါပေမယ့် start ဖြစ်ပြီး 5xx တွေ ပြန်နေရင်တော့ မဖမ်းပါဘူး - အဲဒါကို CloudWatch deployment alarm က ဖမ်းပါတယ်။ Rollout အတွင်း alarm ALARM ဖြစ်ရင် ECS က rollback လုပ်ပါတယ်။
Blue/green က နောက်ထပ် တစ်ဆင့်ပါ။ Version အသစ် (green) ကို သီးခြား target group မှာ အပြည့်တင်ပြီး test listener ကနေ စမ်းလို့ရတယ်၊ ပြီးမှ traffic ကို တစ်စိတ်တစ်ပိုင်း (canary) ဒါမှမဟုတ် တစ်ခါတည်း ပြောင်းပါတယ်။ Blue က တစ်ချိန်လုံး အသင့်ရှိနေလို့ rollback က traffic ပြန်လှည့်ရုံ ဖြစ်ပြီး စက္ကန့်ပိုင်းပဲ ကြာပါတယ်။ ပေးရတဲ့တန်ဖိုးက deployment အတွင်း capacity နှစ်ဆ ကုန်ကျတာနဲ့ ရှုပ်ထွေးမှု ပိုများတာပါ။
FROM COMMIT TO TRAFFIC
----------------------
git push main
|
v
GitHub Actions --OIDC token--> STS --> role github-deploy-web (1h)
| docker build/push web:<sha> (ECR: immutable tags, scan)
| cdk deploy Web-Service -c imageTag=<sha>
v
ECS service 'web' (rolling: min 100% / max 200%)
| new tasks <sha> start --> ALB health check /healthz
|
+-- tasks never healthy ------> circuit breaker --> ROLLBACK
+-- 5xx or p99 alarm fires ---> deployment alarm --> ROLLBACK
+-- healthy, alarms OK -------> old tasks drained --> COMPLETED- Deployment circuit breaker
- ECS rolling deployment တစ်ခုမှာ task အသစ်တွေ steady state မရောက်ဘဲ fail ဖြစ်နေတာ threshold တစ်ခု ကျော်သွားရင် deployment ကို FAILED လို့ သတ်မှတ်ပြီး rollback ဖွင့်ထားရင် နောက်ဆုံး COMPLETED ဖြစ်ခဲ့တဲ့ deployment ကို ပြန်သွားပေးတဲ့ ECS feature ပါ။
| Strategy | Rollback | ဘယ်အချိန် သုံးမလဲ |
|---|---|---|
| Rolling + circuit breaker + alarms | Task အသစ်တွေကို အဟောင်းနဲ့ ပြန်အစားထိုး (မိနစ်ပိုင်း) | Service အများစုအတွက် default ကောင်း |
| CodeDeploy blue/green | Traffic ပြန်လှည့် (စက္ကန့်ပိုင်း) | Canary/linear shift, test listener, manual approval လိုရင် |
| ECS native blue/green | Traffic ပြန်လှည့် (စက္ကန့်ပိုင်း) | CodeDeploy မပါဘဲ ECS ထဲမှာပဲ blue/green လိုချင်ရင် (documentation စစ်ပါ) |
- Tag image with the git SHA
- CI assumes role via OIDC
- New tasks start beside old ones
- Breaker + alarms decide
- Pass: drain old / Fail: rollback
Deploy တစ်ခုရဲ့ ရလဒ်ကို လူက ဆုံးဖြတ်တာ မဟုတ်ဘဲ health check နဲ့ alarm တွေက ဆုံးဖြတ်ရပါမယ်။
Production image အတွက် Dockerfile ပုံစံ
# Build stage: dev dependencies never reach production
FROM public.ecr.aws/docker/library/node:20-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build && npm prune --omit=dev
# Runtime stage: small, non-root, one process
FROM public.ecr.aws/docker/library/node:20-alpine
WORKDIR /app
ENV NODE_ENV=production PORT=8080
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
USER node
EXPOSE 8080
CMD ["node", "dist/server.js"]Multi-stage build က build tool နဲ့ dev dependency တွေကို runtime image ထဲ မပါအောင် လုပ်ပြီး image သေးလို့ pull မြန်ပါတယ်။ USER node က root မဟုတ်ဘဲ run ပါတယ်။ Base image ကို ECR Public ကနေ ယူထားလို့ Docker Hub rate limit ကို ရှောင်နိုင်ပါတယ်။ App က 8080 မှာ listen လုပ်ပြီး /healthz endpoint ရှိရပါမယ်။
Registry, CI role နဲ့ service ပါတဲ့ CDK app
App ကို stack နှစ်ခု ခွဲထားပါတယ်။ Web-Registry (ECR repository နဲ့ GitHub OIDC role) က တစ်ကြိမ်ပဲ deploy လုပ်ပြီး ကြာကြာ ရှိနေမှာပါ။ Web-Service က commit တိုင်းမှာ image tag အသစ်နဲ့ ပြန် deploy ဖြစ်ပါတယ်။ Image tag ကို CDK context (-c imageTag=...) ကနေ ယူပါတယ်။
import { App, Duration, Stack, StackProps } from "aws-cdk-lib";
import * as cloudwatch from "aws-cdk-lib/aws-cloudwatch";
import * as ec2 from "aws-cdk-lib/aws-ec2";
import * as ecr from "aws-cdk-lib/aws-ecr";
import * as ecs from "aws-cdk-lib/aws-ecs";
import * as patterns from "aws-cdk-lib/aws-ecs-patterns";
import * as elbv2 from "aws-cdk-lib/aws-elasticloadbalancingv2";
import * as iam from "aws-cdk-lib/aws-iam";
import { Construct } from "constructs";
// Only this repo's "production" environment may assume the deploy role.
const GITHUB_SUB = "repo:example-org/web:environment:production";
// Long-lived: the registry and the CI identity outlive every deployment.
class RegistryStack extends Stack {
readonly repo: ecr.Repository;
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
this.repo = new ecr.Repository(this, "Repo", {
repositoryName: "web",
imageScanOnPush: true,
imageTagMutability: ecr.TagMutability.IMMUTABLE,
lifecycleRules: [{ maxImageCount: 50 }],
});
const github = new iam.OpenIdConnectProvider(this, "GitHub", {
url: "https://token.actions.githubusercontent.com",
clientIds: ["sts.amazonaws.com"],
});
const deployRole = new iam.Role(this, "DeployRole", {
roleName: "github-deploy-web",
maxSessionDuration: Duration.hours(1),
assumedBy: new iam.WebIdentityPrincipal(github.openIdConnectProviderArn, {
StringEquals: { "token.actions.githubusercontent.com:aud": "sts.amazonaws.com" },
StringLike: {
"token.actions.githubusercontent.com:sub": GITHUB_SUB,
},
}),
});
this.repo.grantPullPush(deployRole);
// cdk deploy works by assuming the bootstrap roles, nothing more.
deployRole.addToPolicy(
new iam.PolicyStatement({
actions: ["sts:AssumeRole"],
resources: [`arn:aws:iam::${this.account}:role/cdk-hnb659fds-*`],
}),
);
}
}
interface ServiceProps extends StackProps {
repo: ecr.IRepository;
imageTag: string;
}
class ServiceStack extends Stack {
constructor(scope: Construct, id: string, props: ServiceProps) {
super(scope, id, props);
const vpc = new ec2.Vpc(this, "Vpc", { maxAzs: 2, natGateways: 1 });
const cluster = new ecs.Cluster(this, "Cluster", {
vpc,
clusterName: "web",
containerInsightsV2: ecs.ContainerInsights.ENABLED,
});
const web = new patterns.ApplicationLoadBalancedFargateService(this, "Web", {
cluster,
serviceName: "web",
cpu: 512,
memoryLimitMiB: 1024,
desiredCount: 2,
minHealthyPercent: 100, // keep full capacity while new tasks start
maxHealthyPercent: 200,
circuitBreaker: { enable: true, rollback: true },
healthCheckGracePeriod: Duration.seconds(30),
taskImageOptions: {
image: ecs.ContainerImage.fromEcrRepository(props.repo, props.imageTag),
containerPort: 8080,
environment: { APP_VERSION: props.imageTag },
},
});
web.targetGroup.configureHealthCheck({
path: "/healthz",
healthyThresholdCount: 2,
interval: Duration.seconds(15),
});
web.targetGroup.setAttribute("deregistration_delay.timeout_seconds", "30");
const scaling = web.service.autoScaleTaskCount({ minCapacity: 2, maxCapacity: 10 });
scaling.scaleOnCpuUtilization("Cpu", { targetUtilizationPercent: 60 });
// Alarms that a bad deployment trips before customers notice.
const errors5xx = new cloudwatch.Alarm(this, "Target5xx", {
alarmName: "web-target-5xx",
// 5xx returned by the containers themselves (not by the ALB)
metric: web.loadBalancer.metrics.httpCodeTarget(
elbv2.HttpCodeTarget.TARGET_5XX_COUNT,
{ period: Duration.minutes(1), statistic: "Sum" },
),
threshold: 10,
evaluationPeriods: 2,
treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING,
});
const latency = new cloudwatch.Alarm(this, "P99Latency", {
alarmName: "web-p99-latency",
metric: web.targetGroup.metrics.targetResponseTime({
period: Duration.minutes(1),
statistic: "p99",
}),
threshold: 1.5, // seconds
evaluationPeriods: 3,
});
// ECS watches these during a rollout and rolls back if one fires.
web.service.enableDeploymentAlarms([errors5xx.alarmName, latency.alarmName], {
behavior: ecs.AlarmBehavior.ROLLBACK_ON_ALARM,
});
}
}
const app = new App();
const env = { account: process.env.CDK_DEFAULT_ACCOUNT, region: "us-east-1" };
const registry = new RegistryStack(app, "Web-Registry", { env });
new ServiceStack(app, "Web-Service", {
env,
repo: registry.repo,
imageTag: app.node.tryGetContext("imageTag") ?? "bootstrap",
});
app.synth();Browser ထဲမှာ run လို့ မရပါ
ဒီ code ဟာ AWS account အစစ်ပေါ်မှာ run ရမယ့် code ဖြစ်လို့ browser playground ထဲမှာ run လို့ မရပါ။ ကိုယ့် terminal (သို့) AWS CloudShell ထဲမှာ စမ်းပါ။
Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- ECR repository
imageTagMutability: ecr.TagMutability.IMMUTABLETag တစ်ခုကို ထပ်ရေးလို့ မရတော့လို့ SHA tag တစ်ခုက image တစ်ခုတည်းကို အမြဲညွှန်ပါတယ်။ Scan on push က vulnerability တွေကို push တိုင်း စစ်ပြီး lifecycle rule က image ၅၀ ထက် ပိုရင် အဟောင်းတွေ ဖျက်ပါတယ်။
- OIDC deploy role
"token.actions.githubusercontent.com:sub": GITHUB_SUBRole က audience sts.amazonaws.com ဖြစ်ပြီး sub က 'repo:example-org/web:environment:production' ဖြစ်တဲ့ token ကိုပဲ ယုံပါတယ်။ Fork တွေ၊ တခြား branch တွေ၊ တခြား repo တွေ assume လုပ်လို့ မရပါဘူး။ Permission က ECR push နဲ့ CDK bootstrap role တွေကို assume လုပ်ခွင့်ပဲ ရှိပါတယ်။
- Rolling settings
minHealthyPercent: 100, maxHealthyPercent: 200Deploy အတွင်း capacity မကျအောင် task အသစ်တွေကို အရင်တင်ပါတယ်။ healthCheckGracePeriod က app start ချိန်အတွင်း ALB health check fail တာကို ignore လုပ်ပေးပါတယ်။ Deregistration delay 30 စက္ကန့်က in-flight request တွေ ပြီးဖို့ အချိန်ပေးပါတယ်။
- Circuit breaker
circuitBreaker: { enable: true, rollback: true }Task အသစ်တွေ start မဖြစ်ရင် (image pull fail, crash loop, health check fail) deployment ကို FAILED လုပ်ပြီး version ဟောင်းကို ပြန်သွားပါတယ်။ CloudFormation stack update ကလည်း fail ဖြစ်လို့ pipeline က အနီရောင် ပြပါတယ်။
- Deployment alarms
web.service.enableDeploymentAlarms([...], { behavior: ROLLBACK_ON_ALARM })Target 5xx (container ကိုယ်တိုင် ပြန်တဲ့ 5xx) နဲ့ p99 latency alarm နှစ်ခုကို ECS က rollout အတွင်း စောင့်ကြည့်ပါတယ်။ တစ်ခုခု ALARM ဖြစ်ရင် rollback ပါတယ်။ treatMissingData NOT_BREACHING က traffic မရှိတဲ့အချိန် alarm မမြည်အောင်ပါ။
Sample output (ARN နဲ့ DNS name တွေ ကွဲပြားပါမယ်):
✅ Web-Service
Outputs:
Web-Service.WebLoadBalancerDNS... = Web-S-WebLB-0a1b2c3d.us-east-1.elb.amazonaws.com
Web-Service.WebServiceURL... = http://Web-S-WebLB-0a1b2c3d.us-east-1.elb.amazonaws.com- New task definition registered
- New tasks start (max 200%)
- ALB health checks pass?
- Deployment alarms quiet?
- Old tasks drained
အားလုံး အောင်မြင်
rolloutState COMPLETED
Breaker (သို့) alarm
FAILED -> previous version restored
Pipeline: GitHub Actions + OIDC
Workflow က secret တစ်ခုမှ မသုံးပါဘူး။ 'id-token: write' permission ကြောင့် job က OIDC token တောင်းနိုင်ပြီး configure-aws-credentials action က အဲဒီ token ကို STS AssumeRoleWithWebIdentity နဲ့ temporary credential အဖြစ် လဲပါတယ်။ 'environment: production' က role ရဲ့ sub condition နဲ့ ကိုက်ရပါမယ် - GitHub မှာ production environment ကို required reviewer နဲ့ ကာကွယ်ထားရင် လူတစ်ယောက် approve လုပ်မှ job က role ကို ရပါတယ်။
'concurrency' က deploy နှစ်ခု တပြိုင်နက် မ run အောင်လုပ်ပြီး cancel-in-progress false က rollout တစ်ဝက်မှာ ဖြတ်မပစ်အောင်ပါ။ Circuit breaker (သို့) alarm rollback ဖြစ်ရင် cdk deploy က fail ဖြစ်လို့ workflow က အနီရောင် ဖြစ်ပါတယ်။
name: deploy-web
on:
push:
branches: [main]
permissions:
id-token: write # lets the job request a GitHub OIDC token
contents: read
concurrency:
group: deploy-web-production
cancel-in-progress: false # never kill a rollout halfway
jobs:
deploy:
runs-on: ubuntu-latest
environment: production # must match the role's "sub" condition
env:
AWS_REGION: us-east-1
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::111122223333:role/github-deploy-web
aws-region: ${{ env.AWS_REGION }}
- id: ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Build and push an immutable image
env:
REGISTRY: ${{ steps.ecr.outputs.registry }}
run: |
docker build -t "$REGISTRY/web:$GITHUB_SHA" app/
docker push "$REGISTRY/web:$GITHUB_SHA"
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Roll out (circuit breaker + alarms guard it)
working-directory: infra
run: |
npm ci
npx cdk deploy Web-Service -c imageTag="$GITHUB_SHA" \
--require-approval neverBlue/green: CodeDeploy နည်းလမ်း
CodeDeploy blue/green မှာ ECS service ရဲ့ deployment controller ကို CODE_DEPLOY ပြောင်းရပါတယ်။ Target group နှစ်ခု (blue, green)၊ production listener နဲ့ test listener (8080) လိုပါတယ်။ Deployment တစ်ခုစတိုင်း CodeDeploy က green task set ကို တင်ပြီး test listener ကို green ဆီ လှည့်တယ်၊ approval စောင့်တယ် (ဒီ app မှာ မိနစ် ၁၀)၊ ပြီးမှ production traffic ကို CANARY_10PERCENT_5MINUTES အတိုင်း - ၁၀% ကို ၅ မိနစ်၊ ပြီးမှ ကျန်တာ အားလုံး - ပြောင်းပါတယ်။ Blue ကို ၁၅ မိနစ် ဆက်ထားပြီး alarm မြည်ရင် traffic ကို blue ဆီ ပြန်လှည့်ပါတယ်။
import { App, Duration, Stack, StackProps } from "aws-cdk-lib";
import * as cloudwatch from "aws-cdk-lib/aws-cloudwatch";
import * as codedeploy from "aws-cdk-lib/aws-codedeploy";
import * as ec2 from "aws-cdk-lib/aws-ec2";
import * as ecs from "aws-cdk-lib/aws-ecs";
import * as patterns from "aws-cdk-lib/aws-ecs-patterns";
import * as elbv2 from "aws-cdk-lib/aws-elasticloadbalancingv2";
import { Construct } from "constructs";
class BlueGreenStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const vpc = new ec2.Vpc(this, "Vpc", { maxAzs: 2, natGateways: 1 });
const cluster = new ecs.Cluster(this, "Cluster", { vpc });
// Same pattern, but CodeDeploy (not ECS) now controls deployments.
const web = new patterns.ApplicationLoadBalancedFargateService(this, "Web", {
cluster,
desiredCount: 2,
deploymentController: { type: ecs.DeploymentControllerType.CODE_DEPLOY },
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry("public.ecr.aws/nginx/nginx:stable"),
containerPort: 80,
},
});
// Green target group + a test listener to smoke-test green before cutover.
const green = new elbv2.ApplicationTargetGroup(this, "Green", {
vpc,
port: 80,
protocol: elbv2.ApplicationProtocol.HTTP,
targetType: elbv2.TargetType.IP,
healthCheck: { path: "/" },
});
const testListener = web.loadBalancer.addListener("Test", {
port: 8080,
protocol: elbv2.ApplicationProtocol.HTTP,
defaultTargetGroups: [green],
});
const errors = new cloudwatch.Alarm(this, "Green5xx", {
metric: green.metrics.httpCodeTarget(elbv2.HttpCodeTarget.TARGET_5XX_COUNT, {
period: Duration.minutes(1),
statistic: "Sum",
}),
threshold: 5,
evaluationPeriods: 1,
treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING,
});
new codedeploy.EcsDeploymentGroup(this, "Deploy", {
service: web.service,
blueGreenDeploymentConfig: {
blueTargetGroup: web.targetGroup,
greenTargetGroup: green,
listener: web.listener,
testListener,
deploymentApprovalWaitTime: Duration.minutes(10),
terminationWaitTime: Duration.minutes(15),
},
deploymentConfig: codedeploy.EcsDeploymentConfig.CANARY_10PERCENT_5MINUTES,
alarms: [errors],
autoRollback: { failedDeployment: true, deploymentInAlarm: true },
});
}
}
const app = new App();
new BlueGreenStack(app, "Web-BlueGreen");
app.synth();# Green is up and receiving TEST traffic on :8080. Smoke-test it:
curl -fsS "http://$ALB_DNS:8080/"
# Happy? Let CodeDeploy shift production traffic (canary 10%, then 100%)
aws deploy continue-deployment --deployment-id "$DEPLOYMENT_ID" \
--deployment-wait-type READY_WAIT
# Not happy? Stop and roll back: production traffic is still on blue
aws deploy stop-deployment --deployment-id "$DEPLOYMENT_ID" --auto-rollback-enabledCODE_DEPLOY controller ရဲ့ ကန့်သတ်ချက်
CODE_DEPLOY controller သုံးရင် ECS circuit breaker ကို သုံးလို့ မရပါဘူး၊ rollback ကို CodeDeploy က ကိုင်ပါတယ်။ ပထမ deploy ပြီးနောက် version အသစ်တွေကို ပုံမှန် service update နဲ့ မဟုတ်ဘဲ task definition အသစ်ကို ညွှန်တဲ့ AppSpec ပါတဲ့ CodeDeploy deployment တစ်ခု ဖန်တီးပြီး rollout လုပ်ရပါတယ်။ ဒါကြောင့် pipeline design ပြောင်းပါတယ် - ဆုံးဖြတ်ချက်မချခင် documentation ကို စစ်ပါ။
ECS native blue/green
ECS ကိုယ်တိုင်ကလည်း ECS deployment controller ထဲမှာ blue/green deployment strategy ကို ပံ့ပိုးလာပါပြီ - bake time နဲ့ lifecycle hook တွေပါပြီး CodeDeploy မလိုပါဘူး။ CDK မှာ deploymentStrategy property နဲ့ ပေါ်လာပါတယ်။ Feature အသစ်ဖြစ်လို့ load balancer အမျိုးအစား၊ region နဲ့ CDK version ပံ့ပိုးမှုကို ECS developer guide မှာ စစ်ပြီးမှ သုံးပါ။
Rollout ကို စောင့်ကြည့်ခြင်းနဲ့ rollback အကြောင်းရင်း ရှာခြင်း
Rollback ဖြစ်သွားရင် ပထမဆုံး service event တွေကို ဖတ်ပါ - ECS က 'circuit breaker triggered' (သို့) 'alarm ... in ALARM state' လို စာသားနဲ့ အကြောင်းပြပါတယ်။ ပြီးရင် stop ဖြစ်သွားတဲ့ task ရဲ့ stoppedReason နဲ့ exit code ကို ကြည့်ပါ။ Exit code 137 က များသောအားဖြင့် memory ကျော်လို့ kill ခံရတာ၊ 'CannotPullContainerError' က image tag မှား (သို့) execution role permission မရှိတာ ဖြစ်တတ်ပါတယ်။
# One PRIMARY deployment = steady. PRIMARY + ACTIVE = rollout in progress.
aws ecs describe-services --cluster web --services web \
--query 'services[0].deployments[].[status,rolloutState,runningCount]'
# Why did it roll back? The service events say so in plain words.
aws ecs describe-services --cluster web --services web \
--query 'services[0].events[:5].message'
# Tasks that died during the rollout, and why
aws ecs list-tasks --cluster web --service-name web --desired-status STOPPED
aws ecs describe-tasks --cluster web --tasks "$TASK_ARN" \
--query 'tasks[0].[stoppedReason,containers[0].exitCode]'
# Block until the service is steady (handy as a pipeline gate)
aws ecs wait services-stable --cluster web --services webProduction မှာ
Production team တွေ ဘယ်လို လုပ်ကြလဲ
Production မှာ image ကို တစ်ကြိမ်ပဲ build လုပ်ပြီး staging နဲ့ production နှစ်ခုလုံးကို SHA တူတူနဲ့ promote လုပ်ပါတယ်။ Environment တစ်ခုချင်းအတွက် ပြန် build လုပ်ရင် staging မှာ စမ်းခဲ့တဲ့ artifact နဲ့ production artifact မတူတော့ပါဘူး။ Account ခွဲထားရင် staging နဲ့ production account တစ်ခုချင်းမှာ OIDC role သီးခြား ရှိပြီး production role ကို protected environment ကပဲ assume လုပ်နိုင်ပါတယ်။
Deployment alarm တွေကို 'ဒီ alarm ကြောင့် rollback လုပ်ရင် လက်ခံနိုင်လား' ဆိုတဲ့ မေးခွန်းနဲ့ ရွေးပါတယ်။ Downstream dependency ပြဿနာကြောင့် မြည်တတ်တဲ့ alarm ကို ထည့်ရင် ကိုယ့် code မှန်နေရဲ့သားနဲ့ deploy တိုင်း rollback ဖြစ်ပါတယ်။ Team အများစုက target 5xx rate နဲ့ latency ကိုပဲ သုံးပြီး business metric (checkout success rate) ကို ထပ်ထည့်ကြပါတယ်။
Rollback ကိုလည်း လစဉ် လေ့ကျင့်ပါတယ် - /healthz ကို fail ခိုင်းတဲ့ build တစ်ခုကို staging မှာ ရည်ရွယ်ချက်ရှိရှိ deploy လုပ်ပြီး circuit breaker က အလုပ်လုပ်လား စစ်ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားများ
Health check က dependency တွေကို စစ်နေခြင်း
/healthz က database ကိုပါ ping လုပ်နေရင် database ခဏ နှေးတာနဲ့ task အားလုံး unhealthy ဖြစ်ပြီး ALB က အားလုံးကို ဖြုတ်ပစ်ပါတယ် - အပြည့်အဝ outage ပါ။ Load balancer health check က 'ဒီ process က request လက်ခံနိုင်လား' ကိုပဲ စစ်သင့်ပြီး dependency အခြေအနေကို metric နဲ့ alarm က ကိုင်ရပါမယ်။
OIDC trust ကို wildcard နဲ့ ဖွင့်ထားခြင်း
sub condition ကို 'repo:example-org/*' (သို့) လုံးဝ မထားရင် organization ထဲက ဘယ် repo, ဘယ် branch, ဘယ် pull request workflow ကမဆို production role ကို assume လုပ်နိုင်ပါတယ်။ sub ကို repo နဲ့ environment (သို့) branch အထိ တိတိကျကျ ကန့်သတ်ပြီး audience condition ကိုလည်း ထည့်ပါ။
Circuit breaker တစ်ခုတည်းကို အားကိုးခြင်း
Circuit breaker က start မဖြစ်တဲ့ task တွေကိုပဲ ဖမ်းပါတယ်။ Health check အောင်ပေမယ့် checkout endpoint မှာ 500 ပြန်နေတဲ့ version ကို လုံးဝ မမြင်ပါဘူး။ Deployment alarm မပါရင် အဲဒီ version က production ထဲ အပြည့် ရောက်သွားပါမယ်။
Best Practices
Best practices
- Image ကို git SHA နဲ့ tag လုပ်ပြီး ECR tag immutability ဖွင့်ပါ
- CI ကို OIDC role နဲ့ပဲ ချိတ်ပြီး sub ကို repo + environment အထိ ကန့်သတ်ပါ
- Circuit breaker (rollback) နဲ့ deployment alarm နှစ်ခုလုံး သုံးပါ
- Instant rollback, test traffic, canary လိုမှ blue/green ကို ရွေးပါ
💰 Cost & Cleanup
ဒီ lab မှာ ငွေကုန်တာတွေက Fargate task (vCPU နဲ့ GB-hour အလိုက်၊ task ၂ ခု အမြဲ run)၊ ALB (နာရီကြေး + LCU)၊ NAT Gateway (နာရီကြေး + process လုပ်တဲ့ GB)၊ ECR storage၊ CloudWatch alarm နဲ့ Container Insights metric တွေ ဖြစ်ပါတယ်။ Blue/green stack ကို ထပ် deploy လုပ်ရင် ALB, NAT, task တွေ နှစ်ဆ ဖြစ်ပါတယ်။ ဒီ resource တွေက free tier ထဲ အများစု မပါလို့ ရက်အနည်းငယ် ထားရင်တောင် သိသာတဲ့ ငွေ ကုန်နိုင်ပါတယ်။ စမ်းပြီးတာနဲ့ ချက်ချင်း ဖျက်ပါ။
စမ်းပြီးရင် ဖျက်ဖို့ command
cdk destroy Web-Service Web-BlueGreen
# The registry stack keeps images; delete them with the repository
aws ecr delete-repository --repository-name web --force
cdk destroy Web-RegistryMini challenge
Pipeline ကို rollback သိအောင်လုပ်ပြီး smoke test ထည့်ပါ
Workflow ရဲ့ deploy step ပြီးနောက် step နှစ်ခု ထည့်ပါ - rollout က တကယ် COMPLETED ဖြစ်ကြောင်း စစ်တာနဲ့ live app က deploy လုပ်လိုက်တဲ့ SHA ကို ပြန်ပေးကြောင်း စစ်တာပါ။
- Service stable ဖြစ်တဲ့အထိ စောင့်ပြီး ပထမ deployment ရဲ့ rolloutState ကို စစ်ရမယ်
- COMPLETED မဟုတ်ရင် job fail ရမယ်
- /version endpoint ကို retry နဲ့ ခေါ်ပြီး GITHUB_SHA နဲ့ တိုက်စစ်ရမယ်
ရလဒ်: Deploy အောင်မြင်ရင် 'rollout state: COMPLETED' နဲ့ 'live version: <sha>' ကို log ထဲမှာ မြင်ရပါမယ်။ Rollback ဖြစ်ခဲ့ရင် job က အနီရောင် ဖြစ်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
'aws ecs wait services-stable' ပြီးမှ describe-services ရဲ့ --query 'services[0].deployments[0].rolloutState' ကို သုံးပါ။ App က APP_VERSION env var ကို /version မှာ ပြန်ပေးရပါမယ်။
အဖြေ ကြည့်ရန်
- name: Prove the rollout finished and was not rolled back
run: |
aws ecs wait services-stable --cluster web --services web
state=$(aws ecs describe-services --cluster web --services web \
--query 'services[0].deployments[0].rolloutState' --output text)
echo "rollout state: $state"
test "$state" = "COMPLETED"
- name: Smoke test the live version
env:
URL: ${{ vars.WEB_URL }}
run: |
for i in 1 2 3 4 5; do
body=$(curl -fsS "$URL/version") && break
sleep 10
done
echo "live version: $body"
test "$body" = "$GITHUB_SHA"Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Immutable SHA tag, OIDC identity, guarded rollout ဆိုတဲ့ အလွှာ သုံးခုလုံး လိုပါတယ်
- Circuit breaker က 'start မဖြစ်' ကို ဖမ်းပြီး deployment alarm က 'start ဖြစ်ပေမယ့် မှားနေ' ကို ဖမ်းပါတယ်
- minHealthyPercent 100 / maxHealthyPercent 200 က deploy အတွင်း capacity မကျအောင် ထိန်းပါတယ်
- Blue/green က စက္ကန့်ပိုင်း rollback ပေးပေမယ့် capacity နှစ်ဆနဲ့ ရှုပ်ထွေးမှု ပိုပေးရပါတယ်
Amazon ECS deployment types — AWS Documentation
Configuring OpenID Connect in AWS (GitHub Docs) — AWS Documentation
AWS CodeDeploy: blue/green deployments for Amazon ECS — AWS Documentation