AWS CodeDeploy

Automates deploying a new version to compute you already run — EC2, ASG, ECS, Lambda — that Beanstalk doesn't manage. The managed alternative to hand-rolled Ansible / Chef / Puppet / Terraform.

Tự động deploy phiên bản mới lên compute bạn đã chạy sẵn — EC2, ASG, ECS, Lambda — thứ Beanstalk không quản. Là bản managed thay cho tự viết Ansible / Chef / Puppet / Terraform.

Reach for it / not the right toolKhi nào dùng / khi nào không

Reach for CodeDeployChọn CodeDeploy

  • Roll a new version onto a fleet of EC2 you already run, automatically and in controlled phases.Đẩy version mới lên fleet EC2 bạn đã chạy, tự động và theo từng phase có kiểm soát.
  • ECS blue/green or Lambda traffic-shifting with automated rollback on a bad metric.ECS blue/green hoặc Lambda traffic-shifting với rollback tự động khi metric xấu.
  • You use SAM — it wires CodeDeploy in for you.Bạn dùng SAM — nó tự đấu CodeDeploy vào.

Not the right toolKhông phải chỗ dùng

  • The env is managed by Elastic Beanstalk → use Beanstalk's own deploy, not CodeDeploy.Env do Elastic Beanstalk quản → dùng deploy của Beanstalk, không phải CodeDeploy.
  • You need to provision the infrastructure itself → CloudFormation / IaC.Cần provision hạ tầng → CloudFormation / IaC.
  • ASG blue/green without an ELB — not possible; an ELB is mandatory.ASG blue/green không có ELB — không được; ELB là bắt buộc.

Four deploy targets — one glanceBốn target deploy — nhìn nhanh

EC2 ASG ECS Lambda
MechanismCơ chế In-place, in halvesIn-place, theo nửa In-place or blue/greenIn-place hoặc blue/green Blue/green task setsBlue/green task set Traffic shift on aliasTraffic shift trên alias
New capacity?Tạo capacity mới? — New ASG (copied)ASG mới (copy) New task setTask set mới New versionVersion mới
ELB required?Cần ELB? — ✅ for blue/green✅ cho blue/green ✅ ALB —
Verify / rollbackVerify / rollback Hooks per phaseHook mỗi phase Keep old N minGiữ cũ N phút Keep old X min · canaryGiữ cũ X phút · canary Pre/post hooks · CW alarmPre/post hook · CW alarm

CodeDeploy to EC2 — in-place, in halvesCodeDeploy tới EC2 — in-place, theo nửa

Define the deploy with appspec.yml + a deployment config. CodeDeploy updates the fleet in place, one batch at a time, and runs hooks to verify after each phase before moving on. Khai báo deploy bằng appspec.yml + deployment config. CodeDeploy update fleet in place, từng batch một, và chạy hook để verify sau mỗi phase trước khi đi tiếp.

CodeDeploy in-place deployment to a fleet of four EC2 instances — the first half updates from v1 to v2 (going briefly offline), a hook verifies, then the other half updates v1 taken offline v2 Half v1 v1 v2 v2 hook verifies → then the other half Other Half v1 v1 v2 v2

In-place update: the first half of the fleet drops v1, goes offline, comes back on v2; a hook verifies; then the other half repeats. New instances an ASG launches also get the deployment automatically.

Update in-place: nửa đầu fleet bỏ v1, offline, quay lại với v2; hook verify; rồi nửa còn lại lặp lại. Instance mới do ASG launch cũng tự nhận deployment.

CodeDeploy to ASG — blue/greenCodeDeploy tới ASG — blue/green

A brand-new ASG is created (settings copied), running the v2 launch template. Both ASGs sit behind the same ELB; you choose how long to keep the old instances. Must be using an ELB. Một ASG hoàn toàn mới được tạo (copy settings), chạy launch template v2. Cả hai ASG nằm sau cùng một ELB; bạn chọn giữ instance cũ bao lâu. Bắt buộc dùng ELB.

CodeDeploy blue/green to an Auto Scaling Group — an ALB fronts two separate Auto Scaling Groups: the old group on launch template v1 and a newly created group on launch template v2; CodeDeploy spins up the new group, shifts traffic to it, then removes the old one ALB Blue/Green deployment Auto Scaling Group · old EC2 · Launch Template v1 Auto Scaling Group · new EC2 · Launch Template v2 new ASG (copied) CodeDeploy

Two separate ASGs: CodeDeploy stands up a brand-new ASG on launch template v2 (settings copied from the old one) behind the same ALB, shifts traffic over, then tears down the old v1 ASG after your chosen keep-alive window. In-place instead would update the existing EC2 in place (and any new ASG instances too).

Hai ASG tách biệt: CodeDeploy dựng một ASG hoàn toàn mới trên launch template v2 (copy settings từ ASG cũ) sau cùng ALB, chuyển traffic sang, rồi hạ ASG v1 cũ sau khoảng giữ cũ bạn chọn. In-place thì thay vào đó update EC2 hiện có tại chỗ (và cả instance mới của ASG).

CodeDeploy to Lambda — traffic shiftingCodeDeploy tới Lambda — traffic shifting

Shift traffic across a Lambda alias from v1 to v2 gradually. Pre-traffic and post-traffic hooks run tests before and after the shift; a CloudWatch alarm triggers easy automated rollback. SAM uses this natively. Chuyển traffic qua alias Lambda từ v1 sang v2 từ từ. Hook pre-traffic và post-traffic chạy test trước và sau khi shift; CloudWatch alarm kích hoạt rollback tự động dễ dàng. SAM dùng sẵn cơ chế này.

CodeDeploy to Lambda — a CICD trigger starts CodeDeploy, which shifts traffic on a Lambda alias from v1 to v2, runs pre-traffic and post-traffic hook functions that run tests, and rolls back automatically on a CloudWatch alarm CloudWatch Alarm CodeDeploy trigger deployment (CICD) Lambda Alias v1 v2 traffic shifting with alias Pre-Traffic Hook λ Post-Traffic Hook λ run tests run tests

CICD triggers CodeDeploy, which shifts alias traffic v1 → v2. Pre- and post-traffic hook functions run tests around the shift; if a CloudWatch alarm fires, CodeDeploy rolls back automatically.

CICD trigger CodeDeploy, nó shift traffic alias v1 → v2. Hook pre/post-traffic chạy test quanh lúc shift; nếu CloudWatch alarm kêu, CodeDeploy tự rollback.

Step-by-stepTừng bước

  1. 1 · Pre-traffic hook — a Lambda that runs before any traffic moves. Deploy v2 but keep it dark; the hook exercises v2 in isolation (smoke tests, schema checks, warm-up). If it fails, the deployment aborts and no traffic is ever shifted. It reports its result back with PutLifecycleEventHookExecutionStatus. 1 · Pre-traffic hook — một Lambda chạy trước khi traffic dịch chuyển. Deploy v2 nhưng giữ tối; hook chạy v2 tách biệt (smoke test, kiểm tra schema, warm-up). Nếu fail, deployment huỷ và không shift traffic nào cả. Nó báo kết quả về bằng PutLifecycleEventHookExecutionStatus.
  2. 2 · Shift traffic — pre-hook passed, so CodeDeploy moves the alias from v1 → v2 per the config (Canary… / Linear… / all-at-once). Throughout the shift a CloudWatch alarm watches; if it fires, CodeDeploy rolls the alias back to v1 automatically. 2 · Shift traffic — pre-hook pass nên CodeDeploy chuyển alias v1 → v2 theo config (Canary… / Linear… / all-at-once). Suốt quá trình shift, một CloudWatch alarm canh; nếu kêu, CodeDeploy tự rollback alias về v1.
  3. 3 · Post-traffic hook (optional) — a Lambda that runs after the shift completes, when v2 is taking 100% of traffic. Use it for end-to-end validation, integration/smoke tests against live traffic, or cleanup. Failing it also triggers a rollback. 3 · Post-traffic hook (tuỳ chọn) — một Lambda chạy sau khi shift xong, khi v2 nhận 100% traffic. Dùng để validate end-to-end, chạy integration/smoke test trên traffic thật, hoặc dọn dẹp. Fail cũng kích hoạt rollback.

CodeDeploy to ECSCodeDeploy tới ECS

Blue/green for containers: a new task set runs the new task definition next to the old one behind the same ALB; traffic shifts across, and the old set is killed only after X stable minutes — a window to catch problems. Blue/green cho container: một task set mới chạy task definition mới cạnh cái cũ sau cùng ALB; traffic chuyển sang, và set cũ chỉ bị xoá sau X phút ổn định — khoảng thời gian để bắt lỗi.

CodeDeploy blue/green to ECS — an ALB fronts an ECS service that holds two task sets: the old one on task definition 1 and a new one on task definition 2; CodeDeploy shifts traffic (shown mid-shift at 60 percent old, 40 percent new) then terminates the old set ALB Blue/Green · traffic shifting ECS Service Task Set 1 task def 1 · old Task Set 2 task def 2 · new CodeDeploy 60% 40%

Set up in the ECS service definition: CodeDeploy creates a new task set (task definition 2) beside the old one, re-routes ALB traffic to it (shown mid-shift, 60% old / 40% new), then terminates the old set after X stable minutes. Canary configs like Canary10Percent5Minutes control the pace.

Cấu hình trong ECS service definition: CodeDeploy tạo task set mới (task definition 2) cạnh cái cũ, re-route traffic ALB sang nó (đang shift, 60% cũ / 40% mới), rồi xoá set cũ sau X phút ổn định. Config canary như Canary10Percent5Minutes quyết định tốc độ.

  • Blue/green for ECS & Fargate — set up inside the ECS service definition, fronted by an ALB. Blue/green cho ECS & Fargate — cấu hình trong ECS service definition, đứng sau ALB.
  • A new task set is created; traffic is re-routed to it. If everything is stable for X minutes, the old task set is terminated — so you have time to notice issues. Một task set mới được tạo; traffic re-route sang nó. Nếu ổn định trong X phút, task set cũ bị terminate — để bạn có thời gian phát hiện lỗi.
  • Supports canary deployments — e.g. Canary10Percent5Minutes (10% first, rest after 5 min). Hỗ trợ canary — vd Canary10Percent5Minutes (10% trước, phần còn lại sau 5 phút).
Traffic-shift configs (Lambda & ECS)Config traffic-shift (Lambda & ECS) Canary — a first % now, the rest after N min. Linear — equal % every N min. All-at-once — flip everything immediately. Naming pattern: Canary10Percent5Minutes, Linear10PercentEvery1Minute. Canary — một % trước, phần còn lại sau N phút. Linear — mỗi N phút cùng một %. All-at-once — chuyển hết ngay. Cách đặt tên: Canary10Percent5Minutes, Linear10PercentEvery1Minute.