Thuta Learning
AWS Advanced
ContainersDevOpsအဆင့်မြင့်

ECR နဲ့ container image လုံခြုံရေး

AWS Advancedသင်ခန်းစာ 1545 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • get-login-password နဲ့ ECR ကို login ဝင်ပြီး image ကို push/pull လုပ်နိုင်မယ်
  • Tag immutability နဲ့ digest ကို သုံးပြီး deploy လုပ်တဲ့ image ကို တိတိကျကျ သတ်မှတ်နိုင်မယ်
  • Valid lifecycle policy JSON ရေးပြီး preview နဲ့ စစ်နိုင်မယ်
  • Basic scanning နဲ့ Amazon Inspector enhanced scanning ကို နှိုင်းယှဉ်ရွေးချယ်နိုင်မယ်
  • Pull-through cache နဲ့ cross-account repository policy ကို configure လုပ်နိုင်မယ်

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းပြရရင်

ECR (Elastic Container Registry) က ကိုယ့် AWS account ထဲမှာ ရှိတဲ့ private Docker registry ပါ။ Docker Hub လိုပဲ image တွေကို push/pull လုပ်ပေမဲ့ IAM နဲ့ ထိန်းချုပ်ပြီး ECS၊ EKS၊ Lambda တို့က တိုက်ရိုက် pull လုပ်နိုင်ပါတယ်။

Image security ဆိုတာကတော့ မေးခွန်း သုံးခုကို ဖြေနိုင်ဖို့ပါ — image ထဲမှာ ဘာတွေ ပါလဲ (vulnerability ရှိတဲ့ package ပါလား)၊ မလိုတာတွေ ဖယ်ထားပြီးပြီလား၊ production မှာ run နေတာက ကိုယ် build ခဲ့တဲ့ image အတိအကျ ဟုတ်ရဲ့လား။ ဥပမာ ပေးရရင် ECR က ဂိုဒေါင်ဖြစ်ပြီး scanning က ဝင်လာတဲ့ ပစ္စည်းတိုင်းကို စစ်တဲ့ ဂိတ်စောင့်၊ tag immutability က ပုံးပေါ်က label ကို ဘယ်သူမှ ဖြုတ်ပြီး ပြန်ကပ်လို့ မရအောင် လုပ်ထားတာနဲ့ တူပါတယ်။

Problem → Why → Solution

ပြဿနာ — ':latest' တစ်ခုတည်းနဲ့ ဘာ run နေလဲ ဘယ်သူမှ မသိ

Team တစ်ခုက CI တိုင်းမှာ image ကို :latest နဲ့ push ပါတယ်။ တစ်နေ့မှာ ECS service က scale out ဖြစ်ပြီး task အသစ် ၂ ခုက :latest ကို pull လုပ်ပါတယ် — ဒါပေမဲ့ ခုနက merge လုပ်ပြီး test မလုပ်ရသေးတဲ့ code က :latest ကို ပြန်ရေးထားပြီးပါပြီ။ Service တစ်ခုတည်းထဲမှာ code ဗားရှင်း နှစ်မျိုး run နေပြီး request တစ်ဝက်လောက် fail ပါတယ်။ Rollback လုပ်ချင်တော့ 'အရင် :latest' ဆိုတာ ဘယ် image လဲ ဘယ်သူမှ မသိပါဘူး။

တစ်ချိန်တည်းမှာပဲ repository ထဲမှာ tag မရှိတော့တဲ့ (untagged) image ထောင်ချီ စုနေပြီး storage ကုန်ကျစရိတ် တက်နေပါတယ်။ Critical CVE တစ်ခု ထွက်လာတဲ့အခါ ဘယ် image တွေ ထိခိုက်လဲ ဆိုတာကိုလည်း မဖြေနိုင်ပါဘူး။

ci-bad.sh
REGISTRY=111122223333.dkr.ecr.us-east-1.amazonaws.com
docker build -t $REGISTRY/orders-api:latest .
docker push $REGISTRY/orders-api:latest   # overwrites yesterday's :latest
aws ecs update-service --cluster demo-cluster --service orders-api \
  --force-new-deployment                   # "deploy whatever latest is now"

aws ecr describe-images --repository-name orders-api \
  --filter tagStatus=UNTAGGED --query 'length(imageDetails)'
# 1874

ဘာကြောင့် ဒီလို ဖြစ်ရတာလဲ

Tag က ပြောင်းလို့ရတဲ့ pointer တစ်ခုသာ ဖြစ်ပြီး image ရဲ့ အမှန်တကယ် identity က sha256 digest ပါ။ Repository က default အနေနဲ့ mutable ဖြစ်လို့ tag ကို ထပ်ခါ ပြန်ရေးလို့ရပြီး ပြန်ရေးခံရတဲ့ image အဟောင်းက untagged ဖြစ်ကျန်ခဲ့ပါတယ်။ Lifecycle policy မရှိရင် အဲဒါတွေကို ဘယ်သူမှ မဖျက်ပါဘူး။

အဓိက သဘောတရား — registry၊ tag၊ digest၊ lifecycle နဲ့ scanning

Image digest
Image manifest ရဲ့ sha256 hash (ဥပမာ sha256:4f1c...) ပါ။ Content တစ်ဘိုက်ပြောင်းရင် digest ပြောင်းလို့ digest နဲ့ pull လုပ်ရင် image အတိအကျ တစ်ခုတည်းကိုသာ ရပါမယ်။

AWS account တစ်ခုမှာ region တစ်ခုစီအတွက် private registry တစ်ခု ရှိပြီး (111122223333.dkr.ecr.us-east-1.amazonaws.com) အဲဒီထဲမှာ repository တွေ ဖန်တီးပါတယ်။ Docker client က IAM ကို တိုက်ရိုက် မသိလို့ 'aws ecr get-login-password' က IAM credential ကို သုံးပြီး ၁၂ နာရီ သက်တမ်းရှိတဲ့ token တစ်ခု ထုတ်ပေးပြီး docker login ကို pipe လုပ်ပါတယ်။ ECS နဲ့ EKS ကတော့ ဒီအလုပ်ကို execution role သို့မဟုတ် node role နဲ့ အလိုအလျောက် လုပ်ပါတယ်။

Tag immutability ကို IMMUTABLE ထားရင် ရှိပြီးသား tag ကို ထပ် push လို့ မရတော့ပါဘူး။ ဒါကြောင့် tag တစ်ခု (ဥပမာ git SHA) က အမြဲ image တစ်ခုတည်းကိုသာ ညွှန်ပါတယ်။ Deploy လုပ်တဲ့အခါ digest နဲ့ ညွှန်တာက ပိုတောင် တိကျပါတယ်။

text
IMAGE SUPPLY CHAIN WITH ECR
---------------------------
  CI build              ECR repo: orders-api (IMMUTABLE, KMS)
 +---------+  push   +------------------------------------------+
 | git sha |-------->| 3f9a2c1 -> sha256:4f1c..  scan: 0 CRIT   |
 | 3f9a2c1 |         | 8b7e0d4 -> sha256:a90e..  scan: 1 HIGH   |
 +---------+         | <untagged> x 40  --lifecycle--> expired  |
                     +----------------+-------------------------+
                                      | pull by digest
             Inspector (enhanced) ----+  re-scans on new CVEs
                                      v
                       ECS task / EKS pod / Lambda

Lifecycle policy က rule တွေကို rulePriority နံပါတ် ငယ်စဉ်ကြီးလိုက် စစ်ပြီး 'untagged ဖြစ်တာ ၇ ရက်ကျော်ရင် ဖျက်' သို့မဟုတ် 'release- နဲ့ စတဲ့ image အသစ်ဆုံး ၃၀ ပဲ ထား' လိုမျိုး rule တွေနဲ့ image ဟောင်းတွေကို အလိုအလျောက် expire လုပ်ပါတယ်။ Expire လုပ်တာက နောက်ကွယ်မှာ asynchronous ဖြစ်လို့ ချက်ချင်း မပျောက်နိုင်ပါဘူး။

Scanning မှာ နှစ်မျိုး ရှိပါတယ်။ Basic scanning က push လုပ်ချိန် (သို့မဟုတ် manual) မှာ OS package vulnerability တွေကို တစ်ကြိမ်တည်း စစ်ပါတယ်။ Enhanced scanning က Amazon Inspector နဲ့ ပေါင်းပြီး OS package အပြင် Python၊ Java၊ Node.js စတဲ့ language package တွေကိုပါ စစ်ပါတယ်။ CVE အသစ် ထွက်လာတိုင်း ရှိပြီးသား image တွေကို ထပ်စစ်ပေးလို့ 'ဒီ CVE က ဘယ် image တွေကို ထိလဲ' ဆိုတဲ့ မေးခွန်းကို ဖြေနိုင်ပါတယ်။

Basic scanningEnhanced (Inspector)
စစ်တဲ့ packageOS packageOS + language package
စစ်တဲ့ အချိန်Push ချိန် သို့မဟုတ် manualPush ချိန် + CVE အသစ်တိုင်း ဆက်တိုက်
ကုန်ကျစရိတ်အပို ကြေး မရှိInspector ကနေ scan လုပ်တဲ့ image အလိုက်

Immutability မှာလည်း အပေးအယူ ရှိပါတယ်။ Tool တချို့က 'prod' သို့မဟုတ် 'stable' လို tag တစ်ခုကို image အသစ်ဆီ ရွှေ့ပြီး deploy လုပ်ဖို့ မျှော်လင့်ထားပါတယ်။ အဲဒီလို workflow ကို immutable repository မှာ မလုပ်နိုင်လို့ deploy pipeline ကို digest သို့မဟုတ် version tag အသစ်နဲ့ ပြောင်းရေးရပါမယ်။ ဒါက အစပိုင်းမှာ အလုပ်ပိုသလို ခံစားရပေမဲ့ rollback လုပ်တဲ့နေ့မှာ အကျိုးရှိတာကို တွေ့ရပါလိမ့်မယ်။

စိတ်ထဲမှာ ပုံဖော်ကြည့်ရန်
  1. Build + tag with git SHA
  2. Push to IMMUTABLE repo
  3. Scan (Inspector re-scans)
  4. Deploy by digest
  5. Lifecycle expires the rest

Tag က လူဖတ်ဖို့၊ digest က စက်တွေ ယုံကြည်ဖို့ပါ။

Lifecycle policy JSON ပုံစံ

lifecycle.json
{
  "rules": [
    {
      "rulePriority": 1,
      "description": "Delete untagged images 7 days after push",
      "selection": {
        "tagStatus": "untagged",
        "countType": "sinceImagePushed",
        "countUnit": "days",
        "countNumber": 7
      },
      "action": { "type": "expire" }
    },
    {
      "rulePriority": 2,
      "description": "Keep the 30 newest release images",
      "selection": {
        "tagStatus": "tagged",
        "tagPrefixList": ["release-"],
        "countType": "imageCountMoreThan",
        "countNumber": 30
      },
      "action": { "type": "expire" }
    },
    {
      "rulePriority": 3,
      "description": "Keep at most 200 images of any kind",
      "selection": {
        "tagStatus": "any",
        "countType": "imageCountMoreThan",
        "countNumber": 200
      },
      "action": { "type": "expire" }
    }
  ]
}

Rule ၁ က untagged image တွေကို ၇ ရက်ပြည့်ရင် ဖျက်ပါတယ်။ Rule ၂ က 'tagged' ဖြစ်တဲ့အတွက် tagPrefixList (သို့မဟုတ် tagPatternList) မဖြစ်မနေ လိုပြီး release- image အသစ်ဆုံး ၃၀ ကိုပဲ ထားပါတယ်။ tagStatus 'any' ပါတဲ့ rule က rulePriority နံပါတ် အကြီးဆုံး (နောက်ဆုံး) ဖြစ်ရပါမယ် — မဟုတ်ရင် policy ကို လက်မခံပါဘူး။ Apply မလုပ်ခင် start-lifecycle-policy-preview နဲ့ ဘာတွေ ဖျက်ခံရမလဲ ကြိုကြည့်ပါ။

လုံခြုံတဲ့ repository တစ်ခုနဲ့ push-scan-deploy flow

ဒီ script က CI pipeline တစ်ခုထဲမှာ run မယ့် ပုံစံပါ။ Repository ကို immutable tag၊ scan on push နဲ့ KMS encryption နဲ့ ဖန်တီးပြီး lifecycle policy တပ်ပါတယ်။ ပြီးရင် git SHA နဲ့ tag လုပ်ပြီး push၊ scan ပြီးတဲ့အထိ စောင့်ပြီး severity count ကို ကြည့်၊ နောက်ဆုံးမှာ deploy လုပ်မယ့် digest ကို ထုတ်ပါတယ်။ Repository ဖန်တီးတာကို တစ်ကြိမ်ပဲ လုပ်ရပါတယ် — လက်တွေ့မှာ IaC ထဲ ထားပါ။

build-push-scan.sh
#!/usr/bin/env bash
set -euo pipefail
REGION=us-east-1
REPO=orders-api
REGISTRY=111122223333.dkr.ecr.$REGION.amazonaws.com
TAG=$(git rev-parse --short HEAD)

aws ecr create-repository --repository-name $REPO \
  --image-tag-mutability IMMUTABLE \
  --image-scanning-configuration scanOnPush=true \
  --encryption-configuration encryptionType=KMS
aws ecr put-lifecycle-policy --repository-name $REPO \
  --lifecycle-policy-text file://lifecycle.json

aws ecr get-login-password --region $REGION \
  | docker login --username AWS --password-stdin $REGISTRY
docker build --platform linux/arm64 -t $REGISTRY/$REPO:$TAG .
docker push $REGISTRY/$REPO:$TAG

aws ecr wait image-scan-complete --repository-name $REPO --image-id imageTag=$TAG
aws ecr describe-image-scan-findings --repository-name $REPO \
  --image-id imageTag=$TAG \
  --query 'imageScanFindings.findingSeverityCounts'

DIGEST=$(aws ecr describe-images --repository-name $REPO \
  --image-ids imageTag=$TAG --query 'imageDetails[0].imageDigest' --output text)
echo "deploy: $REGISTRY/$REPO@$DIGEST"

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. create-repository--image-tag-mutability IMMUTABLE --encryption-configuration encryptionType=KMS

    Tag ကို ပြန်ရေးလို့ မရအောင် လုပ်ပြီး image layer တွေကို AWS managed KMS key နဲ့ encrypt လုပ်ပါတယ် (default က AES-256)။ Customer managed key သုံးရင် pull လုပ်မယ့် role တွေကို key policy မှာ kms:Decrypt ပေးဖို့ လိုပါတယ်။

  2. get-login-password| docker login --username AWS --password-stdin $REGISTRY

    Username က အမြဲ 'AWS' ဖြစ်ပြီး password ကို stdin ကနေ ပေးလို့ shell history ထဲ မကျန်ပါဘူး။ Token က ၁၂ နာရီ သက်တမ်းရှိပါတယ်။

  3. tag = git SHATAG=$(git rev-parse --short HEAD)

    Image တစ်ခုချင်းကို commit တစ်ခုနဲ့ ချိတ်ပါတယ်။ Repository က immutable ဖြစ်လို့ တူညီတဲ့ SHA ကို ထပ် push ရင် 'tag already exists' error နဲ့ ငြင်းပါမယ်။

  4. wait + scan findingsaws ecr wait image-scan-complete ...

    Scan ပြီးတဲ့အထိ စောင့်ပြီး severity အလိုက် finding အရေအတွက်ကို ယူပါတယ်။ CI မှာ CRITICAL တစ်ခုခုရှိရင် pipeline ကို fail လုပ်တဲ့ gate အဖြစ် သုံးကြပါတယ်။

  5. digest--query 'imageDetails[0].imageDigest'

    Deploy လုပ်မယ့် task definition သို့မဟုတ် Kubernetes manifest ထဲမှာ repo@sha256:... ပုံစံနဲ့ ထည့်ပါ — ဒါဆို scan လုပ်ခဲ့တဲ့ image အတိအကျ run မှာ သေချာပါတယ်။

You should see
Sample output (scan ရလဒ်နဲ့ digest ကွာနိုင်ပါတယ်):
{
    "HIGH": 1,
    "MEDIUM": 4,
    "LOW": 9
}
deploy: 111122223333.dkr.ecr.us-east-1.amazonaws.com/orders-api@sha256:4f1c9e...

Enhanced scanning ကို registry တစ်ခုလုံးမှာ ဖွင့်ခြင်း

Scanning configuration က registry အဆင့် (region တစ်ခုစီ) မှာ သတ်မှတ်တာပါ။ ENHANCED ကို ရွေးလိုက်ရင် repository တစ်ခုချင်းရဲ့ scanOnPush setting အစား registry rule တွေက အလုပ်လုပ်ပါတယ်။ CONTINUOUS_SCAN ရွေးထားတဲ့ repository တွေကို Inspector က CVE အသစ်တွေ ထွက်လာတိုင်း ပြန်စစ်ပြီး finding တွေကို Inspector console၊ Security Hub နဲ့ EventBridge ဆီ ပို့ပါတယ်။ Image ဟောင်း အများကြီး ရှိတဲ့ registry မှာ ဆက်တိုက် scan လုပ်ရင် Inspector ကုန်ကျစရိတ် တက်နိုင်လို့ lifecycle policy နဲ့ တွဲသုံးပါ။

enhanced-scanning.sh
aws inspector2 enable --resource-types ECR

cat > scan-rules.json <<'EOF'
[
  {
    "scanFrequency": "CONTINUOUS_SCAN",
    "repositoryFilters": [{ "filter": "prod-*", "filterType": "WILDCARD" }]
  },
  {
    "scanFrequency": "SCAN_ON_PUSH",
    "repositoryFilters": [{ "filter": "*", "filterType": "WILDCARD" }]
  }
]
EOF
aws ecr put-registry-scanning-configuration --scan-type ENHANCED \
  --rules file://scan-rules.json

Pull-through cache — public image တွေကို ကိုယ့် registry ကနေ pull ခြင်း

Base image တွေကို Docker Hub ကနေ တိုက်ရိုက် pull ရင် rate limit ထိနိုင်၊ upstream ပျက်ရင် build ရပ်နိုင်၊ private subnet ကနေ internet ထွက်ဖို့ NAT လိုပါတယ်။ Pull-through cache rule တစ်ခု ဖန်တီးလိုက်ရင် ကိုယ့် ECR URL ကနေ upstream image ကို pull လုပ်လို့ရပြီး ပထမအကြိမ်မှာ ECR က upstream ကနေ ယူပြီး ကိုယ့် registry ထဲ repository အသစ်အဖြစ် သိမ်းထားပါတယ်။ နောက်ပိုင်း pull တွေက ECR ကနေပဲ လာပြီး ECR က upstream ကို အချိန်အပိုင်းအခြားနဲ့ ပြန်စစ်ပါတယ်။

ECR Public၊ registry.k8s.io နဲ့ Quay လို upstream တွေက authentication မလိုပါဘူး။ Docker Hub နဲ့ GitHub Container Registry လို upstream တွေအတွက်တော့ credential ကို Secrets Manager မှာ 'ecr-pullthroughcache/' prefix နဲ့ သိမ်းပြီး ARN ကို ပေးရပါတယ်။ Cache ထဲ ရောက်လာတဲ့ image တွေကိုလည်း ကိုယ့် scanning နဲ့ lifecycle rule တွေက ထိန်းချုပ်လို့ ပြင်ပ image တွေကိုပါ စစ်လို့ရလာပါတယ်။

pull-through-cache.sh
REGISTRY=111122223333.dkr.ecr.us-east-1.amazonaws.com
SECRETS=arn:aws:secretsmanager:us-east-1:111122223333:secret

aws ecr create-pull-through-cache-rule --ecr-repository-prefix ecr-public \
  --upstream-registry-url public.ecr.aws
aws ecr create-pull-through-cache-rule --ecr-repository-prefix docker-hub \
  --upstream-registry-url registry-1.docker.io \
  --credential-arn "$SECRETS:ecr-pullthroughcache/docker-hub-AbCdEf"

# <prefix>/<upstream repository path>:<tag>
docker pull $REGISTRY/ecr-public/nginx/nginx:stable
docker pull $REGISTRY/docker-hub/library/python:3.12-slim

ပထမဆုံး pull အတွက် permission

Cache repository မရှိသေးရင် ပထမဆုံး pull လုပ်တဲ့ identity က ecr:CreateRepository နဲ့ ecr:BatchImportUpstreamImage permission လိုပါတယ်။ CI role ကို ဒီ permission ပေးထားပြီး runtime role တွေကိုတော့ pull permission ပဲ ပေးပါ။

Cross-account pull

Multi-account setup မှာ image တွေကို 'shared services' သို့မဟုတ် 'tooling' account တစ်ခုထဲမှာ build ပြီး dev၊ staging၊ prod account တွေက pull လုပ်လေ့ ရှိပါတယ်။ ဒီအတွက် ခွင့်ပြုချက် နှစ်ဖက်လုံး လိုပါတယ် — image ရှိတဲ့ account ဘက်က repository policy (resource policy) က prod account ကို pull ခွင့်ပေးရမယ်၊ prod account ဘက်က role ရဲ့ identity policy ကလည်း အဲဒီ repository ကို pull ခွင့်နဲ့ ecr:GetAuthorizationToken ကို ပေးရမယ်။ Organization တစ်ခုလုံးကို ခွင့်ပြုချင်ရင် Principal '*' နဲ့ aws:PrincipalOrgID condition ကို သုံးနိုင်ပါတယ်။

repo-policy.sh
# in the image account (111122223333): let the prod account pull
cat > repo-policy.json <<'EOF'
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ProdPull",
      "Effect": "Allow",
      "Principal": { "AWS": "arn:aws:iam::444455556666:root" },
      "Action": [
        "ecr:BatchGetImage",
        "ecr:GetDownloadUrlForLayer",
        "ecr:BatchCheckLayerAvailability"
      ]
    }
  ]
}
EOF
aws ecr set-repository-policy --repository-name orders-api \
  --policy-text file://repo-policy.json
prod-execution-role-ecr.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "EcrToken",
      "Effect": "Allow",
      "Action": "ecr:GetAuthorizationToken",
      "Resource": "*"
    },
    {
      "Sid": "PullOrdersApi",
      "Effect": "Allow",
      "Action": [
        "ecr:BatchGetImage",
        "ecr:GetDownloadUrlForLayer",
        "ecr:BatchCheckLayerAvailability"
      ],
      "Resource": "arn:aws:ecr:us-east-1:111122223333:repository/orders-api"
    }
  ]
}

Minimal base image နဲ့ image signing

Vulnerability ကို အမြန်ဆုံး လျှော့နည်းက image ထဲ package နည်းအောင် ထားတာပါ။ Multi-stage build မှာ compiler နဲ့ build tool တွေကို ပထမ stage မှာပဲ သုံးပြီး runtime stage ကို distroless သို့မဟုတ် scratch လို shell၊ package manager မပါတဲ့ image ပေါ်မှာ တည်ဆောက်ပါတယ်။ Full OS image တစ်ခုမှာ CVE ရာနဲ့ချီ ပါနိုင်ပေမဲ့ distroless static image မှာ အများအားဖြင့် လက်ချိုးရေလို့ ရလောက်ပဲ ကျန်ပါတယ်။ အပေးအယူကတော့ container ထဲ shell မရှိလို့ ECS Exec နဲ့ CMD-SHELL health check တွေ အလုပ်မလုပ်တော့တာပါ — ALB health check ကို အားကိုးရပါမယ်။

Image signing က 'ဒီ image ကို ငါတို့ pipeline က တကယ် build ခဲ့တာလား' ကို သက်သေပြပါတယ်။ AWS Signer က Notation (Notary Project) နဲ့ ECR image တွေကို sign လုပ်နိုင်ပြီး Sigstore cosign လို tool တွေကလည်း signature ကို ECR ထဲမှာ OCI artifact အဖြစ် သိမ်းနိုင်ပါတယ်။ Signature ကို deploy ချိန်မှာ စစ်တာ (EKS admission controller လိုမျိုး) ကတော့ သီးခြား setup လိုပါတယ်။

Dockerfile
# build stage: full toolchain, never shipped
FROM golang:1.24 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" \
    -o /out/orders-api ./cmd/orders-api

# runtime stage: no shell, no package manager, runs as non-root
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/orders-api /orders-api
USER nonroot:nonroot
EXPOSE 8080
ENTRYPOINT ["/orders-api"]

Production မှာ

Production မှာ တကယ် ဘယ်လို သုံးကြလဲ

Organization အများစုမှာ ECR repository တွေကို shared-services account တစ်ခုမှာ စုထားပြီး CI pipeline တစ်ခုတည်းကသာ push ခွင့်ရှိပါတယ်။ Workload account တွေက Organization-wide repository policy နဲ့ pull ပဲ လုပ်နိုင်ပါတယ်။ Developer တွေ laptop ကနေ prod repository ကို push လုပ်ခွင့် မရှိပါဘူး — ဒါက supply chain ကို ကာကွယ်တဲ့ အရေးကြီးဆုံး ထိန်းချုပ်မှုတစ်ခုပါ။

CI pipeline ထဲမှာ scan gate ထားကြပါတယ်။ CRITICAL finding ရှိရင် deploy မလုပ်ဘဲ fix မရနိုင်သေးတဲ့ finding တွေအတွက် Inspector မှာ suppression rule ကို ရက်သတ်မှတ်ပြီး ထားကြပါတယ်။ Enhanced scanning ရဲ့ EventBridge event တွေကို Slack သို့မဟုတ် ticket system ဆီ route လုပ်ပြီး production မှာ run နေတဲ့ image ကို ထိခိုက်တဲ့ CVE အသစ် ထွက်လာရင် owner team ကို တိုက်ရိုက် အသိပေးပါတယ်။ Multi-region deploy လုပ်ရင် replication configuration နဲ့ image တွေကို region တစ်ခုကနေ တစ်ခုဆီ အလိုအလျောက် copy လုပ်ပြီး cross-region pull ကုန်ကျစရိတ်နဲ့ latency ကို ရှောင်ပါတယ်။

  • Repository နာမည်ကို team/service ပုံစံ (payments/orders-api) နဲ့ ပေးပြီး IAM policy တွေမှာ prefix wildcard နဲ့ team တစ်ခုချင်းအလိုက် ခွင့်ပြုချက် ခွဲပါတယ်။
  • Repository creation template တွေနဲ့ pull-through cache သို့မဟုတ် replication က အလိုအလျောက် ဖန်တီးတဲ့ repository တွေမှာလည်း immutability၊ encryption နဲ့ lifecycle setting တွေ တူညီအောင် ထိန်းပါတယ်။
  • Base image ကို လစဉ် သို့မဟုတ် CVE အသစ်ထွက်တိုင်း rebuild လုပ်တဲ့ scheduled pipeline ထားပါတယ် — app code မပြောင်းလည်း image ကို refresh လုပ်ဖို့ပါ။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

အမှား ၁ — Repository policy ပဲ ထည့်ပြီး cross-account pull မရ

Image account မှာ repository policy ထည့်ပြီးပြီ၊ prod account ကနေ pull တော့ 'no basic auth credentials' သို့မဟုတ် AccessDenied ရပါတယ်။ Cross-account access မှာ resource policy နဲ့ identity policy နှစ်ဖက်လုံး ခွင့်ပြုရပါတယ် — prod ဘက် role မှာ pull action တွေနဲ့ ecr:GetAuthorizationToken မရှိရင် မရပါဘူး။ Repository က customer managed KMS key သုံးရင် key policy မှာလည်း prod role ကို kms:Decrypt ပေးရပါတယ်။

အမှား ၂ — Lifecycle policy က run နေတဲ့ image ကို ဖျက်သွားခြင်း

'tagStatus any၊ နောက်ဆုံး ၂၀ ပဲ ထား' လို ရိုးရှင်းတဲ့ rule က build များတဲ့ repository မှာ အပတ်အနည်းငယ်အတွင်း ဖြေးဖြေး deploy လုပ်တဲ့ service ရဲ့ image ကို ဖျက်သွားနိုင်ပါတယ်။ Run နေတဲ့ task က မထိခိုက်ပေမဲ့ scale out သို့မဟုတ် task အစားထိုးတဲ့အခါ CannotPullContainerError ဖြစ်ပါတယ်။ Release image တွေအတွက် count ကို ရက်ရောရောထားပြီး apply မလုပ်ခင် lifecycle preview ကို အမြဲ run ပါ။

အမှား ၃ — Scan result ကို ကြည့်ပဲ ကြည့်ပြီး ဘာမှ မလုပ်ခြင်း

Scan on push ဖွင့်ထားပေမဲ့ ဘယ်သူမှ finding တွေကို မကြည့်ရင် အကျိုးမရှိပါဘူး။ ပြီးတော့ basic scan က push ချိန်မှာ တစ်ခါပဲ စစ်လို့ လေးလ အရင်က 'clean' ဖြစ်ခဲ့တဲ့ image က ဒီနေ့ critical CVE ပါနေနိုင်ပါတယ်။ CI gate၊ continuous scanning နဲ့ alert routing သုံးပြီး finding ကို owner ဆီ ရောက်အောင် လုပ်ပါ။

Best Practices

အကောင်းဆုံး လုပ်ထုံးလုပ်နည်းများ

  • Repository အားလုံးကို IMMUTABLE ထားပြီး git SHA နဲ့ tag လုပ်ပါ၊ deploy ကို digest နဲ့ လုပ်ပါ။
  • Repository တိုင်းမှာ untagged cleanup rule အနည်းဆုံး တစ်ခု ပါတဲ့ lifecycle policy ထားပါ။
  • Production repository တွေမှာ enhanced continuous scanning ဖွင့်ပါ။
  • Third-party base image တွေကို pull-through cache ကနေသာ pull ပါ။
  • Runtime image ကို distroless သို့မဟုတ် minimal base ပေါ်မှာ non-root user နဲ့ build ပါ။

💰 Cost & Cleanup

ECR က သိမ်းထားတဲ့ GB-month အလိုက်နဲ့ region/internet ကို ထွက်တဲ့ data transfer အလိုက် ငွေတောင်းပါတယ် — region တစ်ခုတည်းထဲက ECS/EKS ဆီ pull တာက data transfer ကြေး မရှိပါဘူး။ Free tier မှာ private repository storage အနည်းငယ် (ပထမ ၁၂ လအတွင်း) ပါပါတယ်။ Basic scanning က အခမဲ့ဖြစ်ပြီး enhanced scanning ကိုတော့ Amazon Inspector က scan လုပ်တဲ့ image အလိုက် ငွေတောင်းပါတယ်။ Lab ပြီးရင် repository၊ pull-through cache rule နဲ့ cache က ဖန်တီးထားတဲ့ repository တွေကို ဖျက်ပြီး enhanced scanning ကို ပိတ်ပါ။ ဈေးနှုန်း အတိအကျကို pricing page မှာ စစ်ပါ။

စမ်းပြီးရင် ဖျက်ဖို့ command

cleanup.sh
aws ecr delete-repository --repository-name orders-api --force
aws ecr delete-pull-through-cache-rule --ecr-repository-prefix ecr-public
aws ecr delete-pull-through-cache-rule --ecr-repository-prefix docker-hub
aws ecr delete-repository --repository-name ecr-public/nginx/nginx --force
aws ecr delete-repository --repository-name docker-hub/library/python --force
aws ecr put-registry-scanning-configuration --scan-type BASIC
aws inspector2 disable --resource-types ECR

Mini challenge

Mini challenge — PR build တွေအတွက် lifecycle policy

Pull request တိုင်းအတွက် image တစ်ခု (pr-<number>-<sha> tag) push လုပ်တဲ့ repository တစ်ခုမှာ storage အရမ်း တက်နေပါတယ်။ Lifecycle policy တစ်ခု ရေးပြီး preview နဲ့ စစ်ပါ။

  • Untagged image တွေကို push ပြီး ၁ ရက်မှာ ဖျက်ပါ
  • pr- နဲ့ စတဲ့ image တွေကို push ပြီး ၁၄ ရက်မှာ ဖျက်ပါ
  • ဘယ် tag မဆို image စုစုပေါင်း ၃၀၀ ထက် မပိုစေပါနဲ့
  • Valid JSON ဖြစ်ပြီး 'any' rule ကို နောက်ဆုံး priority ထားပါ

ရလဒ်: get-lifecycle-policy-preview ရဲ့ ရလဒ်မှာ untagged image တွေနဲ့ ၁၄ ရက်ကျော် pr- image တွေကို 'EXPIRE' အဖြစ် ပြပြီး release image တွေ မပါပါဘူး။

အရိပ်အမြွက် ကြည့်ရန်

pr- rule မှာ countType 'sinceImagePushed' နဲ့ tagPrefixList ကို သုံးပါ။ Apply မလုပ်ခင် 'aws ecr start-lifecycle-policy-preview' နဲ့ စမ်းပါ။

အဖြေ ကြည့်ရန်
lifecycle-pr.json
{
  "rules": [
    {
      "rulePriority": 1,
      "description": "Untagged: 1 day",
      "selection": {
        "tagStatus": "untagged",
        "countType": "sinceImagePushed",
        "countUnit": "days",
        "countNumber": 1
      },
      "action": { "type": "expire" }
    },
    {
      "rulePriority": 2,
      "description": "PR images: 14 days",
      "selection": {
        "tagStatus": "tagged",
        "tagPrefixList": ["pr-"],
        "countType": "sinceImagePushed",
        "countUnit": "days",
        "countNumber": 14
      },
      "action": { "type": "expire" }
    },
    {
      "rulePriority": 10,
      "description": "Hard cap: 300 images",
      "selection": {
        "tagStatus": "any",
        "countType": "imageCountMoreThan",
        "countNumber": 300
      },
      "action": { "type": "expire" }
    }
  ]
}

Quick Quiz 1 / 3

IMMUTABLE repository မှာ ရှိပြီးသား tag 'v1.4.2' နဲ့ image ကို ထပ် push လုပ်ရင် ဘာဖြစ်မလဲ?

Quick Quiz 2 / 3

Container ထဲမှာ pip နဲ့ install လုပ်ထားတဲ့ Python library ရဲ့ CVE ကို ရှာပြီး CVE အသစ်ထွက်ရင် image ဟောင်းတွေကို ထပ်စစ်ပေးချင်ပါတယ်။ ဘာကို သုံးရမလဲ?

Quick Quiz 3 / 3

Repository policy က prod account (444455556666) ကို pull ခွင့် ပေးထားပေမဲ့ prod ECS task တွေ image pull မရပါဘူး။ အဖြစ်နိုင်ဆုံး အကြောင်းရင်းက?

မှတ်ထားရမယ့် အချက်များ

  • get-login-password က ၁၂ နာရီ token ထုတ်ပြီး docker login ဆီ pipe လုပ်ပါတယ်။
  • IMMUTABLE tag + git SHA + digest နဲ့ deploy = ဘာ run နေလဲ အမြဲ သိနိုင်။
  • Lifecycle rule တွေကို priority အစဉ်လိုက် စစ်ပြီး 'any' rule က နောက်ဆုံး ဖြစ်ရပါတယ်။
  • Enhanced scanning (Inspector) က language package တွေကို စစ်ပြီး CVE အသစ်တွေနဲ့ ဆက်တိုက် ပြန်စစ်ပါတယ်။
  • Cross-account pull မှာ repository policy နဲ့ identity policy နှစ်ခုလုံး လိုပါတယ်။

Amazon ECR User Guide — AWS Documentation

Lifecycle policies in Amazon ECR — AWS Documentation

Scan images for vulnerabilities in Amazon ECR — AWS Documentation

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။