Thuta Learning
AWS Advanced
IaC & DeliveryDevOpsအဆင့်မြင့်

CloudFormation အနက်ရှိုင်း — Change set, Drift, Rollback နဲ့ StackSets

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

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

  • Parameters, Mappings, Conditions, Outputs/Exports ပါတဲ့ environment တစ်ခုထက်ပိုပြီး သုံးလို့ရတဲ့ template ရေးနိုင်မယ်
  • Update မလုပ်ခင် change set နဲ့ Replacement ဖြစ်မဖြစ် ကြိုစစ်နိုင်မယ်
  • Drift detection run ပြီး console ကနေ လက်နဲ့ ပြင်ထားတာတွေကို ရှာနိုင်မယ်
  • Rollback state တွေ (ROLLBACK_COMPLETE, UPDATE_ROLLBACK_FAILED) ကနေ ပြန်ထွက်နိုင်မယ်
  • DeletionPolicy, UpdateReplacePolicy, stack policy နဲ့ data ကို ကာကွယ်ပြီး nested stack နဲ့ StackSets ကို ဘယ်အချိန်သုံးမလဲ ရွေးနိုင်မယ်

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

CloudFormation ကို ရိုးရိုးလေး ပြန်စဉ်းစားကြည့်ရအောင်

CloudFormation template ကို အိမ်ဆောက်ဖို့ ပုံကြမ်း (blueprint) လို့ မြင်ကြည့်ပါ။ Stack ကတော့ အဲဒီ ပုံကြမ်းအတိုင်း တကယ်ဆောက်ထားတဲ့ အိမ်ပါ။ ပုံကြမ်းကို ပြင်ပြီး 'update' လုပ်တိုင်း CloudFormation က အိမ်ကို ပုံကြမ်းအသစ်နဲ့ ကိုက်အောင် ပြင်ပေးပါတယ်။

ပြဿနာက ပြင်ဆင်မှုတချို့ဟာ နံရံကို ဆေးသုတ်တာလို ရိုးရှင်းပေမယ့် တချို့ကတော့ 'ဒီအခန်းကို ဖြိုပြီး အသစ်ပြန်ဆောက်' ဆိုတဲ့ အဓိပ္ပာယ် ဖြစ်နေတတ်ပါတယ် — အခန်းထဲက ပရိဘောဂတွေ (ကိုယ့် data) ပါ ပါသွားပါတယ်။ ဒီသင်ခန်းစာက ဆောက်လုပ်ရေး မစခင် ဘာတွေ ပြောင်းမလဲ ကြိုကြည့်တာ (change set)၊ တစ်ယောက်ယောက် လက်နဲ့ ပြင်ထားတာကို ရှာတာ (drift)၊ မှားသွားရင် ပြန်လှည့်တာ (rollback) နဲ့ အရေးကြီးတဲ့ အခန်းတွေကို ဖြိုခွင့်မပေးတာ (DeletionPolicy, stack policy) တို့ကို သင်ပေးပါမယ်။

Problem → Why → Solution

'နာမည်လေး ပြောင်းရုံပါ' ဆိုတဲ့ update

AWS Fundamentals မှာ CloudFormation stack တစ်ခု create လုပ်ပြီး delete လုပ်ခဲ့ဖူးပါတယ်။ Production မှာ အန္တရာယ်အကြီးဆုံးက create မဟုတ်ဘဲ update ပါ။ Developer တစ်ယောက်က DynamoDB table name ကို orders-prod ကနေ orders-v2-prod လို့ 'သပ်ရပ်အောင်' ပြောင်းပြီး update-stack ကို တိုက်ရိုက် run လိုက်တယ်ဆိုပါစို့။

TableName ဟာ replacement property ဖြစ်ပါတယ်။ CloudFormation က table အသစ် (ဗလာ) တစ်ခု ဆောက်၊ reference တွေကို အသစ်ဆီ လွှဲ၊ ပြီးတော့ cleanup အဆင့်မှာ table အဟောင်းကို data အကုန်နဲ့ ဖျက်ပစ်ပါတယ်။ Error တစ်ခုမှ မပြပါဘူး — stack က UPDATE_COMPLETE ဖြစ်ပြီး order မှတ်တမ်း အားလုံး ပျောက်သွားပါတယ်။

friday-deploy.sh
# Friday 17:55 - "just a small rename", pushed straight to production
aws cloudformation update-stack --stack-name orders-data-prod \
  --template-body file://orders-data.yaml \
  --parameters ParameterKey=EnvName,ParameterValue=prod
# TableName changed from orders-prod to orders-v2-prod. That property needs
# replacement: CloudFormation creates a NEW empty table, switches references,
# then DELETES the old table and every item in it.

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

update-stack က ဘာ ပြောင်းမလဲကို မပြဘဲ တန်းလုပ်ပါတယ်၊ template ထဲမှာလည်း data ကို ကာကွယ်ပေးမယ့် policy တစ်ခုမှ မပါပါဘူး။ Resource property တိုင်းမှာ 'Update requires' အမျိုးအစား (No interruption, Some interruption, Replacement) ရှိပြီး Replacement ဆိုတာ resource အသစ်နဲ့ အဟောင်းကို ဖျက်တာပါ။ Change set က ဒါကို ကြိုပြပေးပြီး UpdateReplacePolicy: Retain က အဟောင်းကို မဖျက်ဘဲ ထားခဲ့ပေးပါတယ်။

Template anatomy နဲ့ stack lifecycle

Template တစ်ခုမှာ မဖြစ်မနေ လိုတာက Resources section တစ်ခုတည်းပါ။ ကျန်တာတွေက template ကို environment တစ်ခုထက်ပိုပြီး ပြန်သုံးလို့ရအောင် လုပ်ပေးပါတယ်။ Template တစ်ခုမှာ resource 500 အထိ ထည့်လို့ရပေမယ့် အဲဒီ အရွယ်ရောက်ခင်ကတည်းက ခွဲသင့်ပါပြီ။ Section တွေကို ချိတ်ဆက်ပေးတာကတော့ !Ref, !GetAtt, !Sub, !If လို intrinsic function တွေနဲ့ AWS::StackName, AWS::AccountId လို pseudo parameter တွေပါ။

Sectionဘာအတွက်လဲ
ParametersDeploy လုပ်ချိန်မှာ ထည့်ပေးရတဲ့ input (EnvName, AWS::EC2::VPC::Id, SSM parameter type)
MappingsKey အလိုက် တန်ဖိုး ရှာတဲ့ ဇယား (env တစ်ခုချင်းစီရဲ့ size, retention)
ConditionsParameter ပေါ်မူတည်ပြီး resource/property ကို ဖွင့်/ပိတ် (IsProd)
Resourcesတကယ် ဆောက်မယ့် AWS resource တွေ — တစ်ခုတည်းသော မဖြစ်မနေ section
OutputsStack ကနေ ထုတ်ပေးတဲ့ တန်ဖိုး၊ Export ပါရင် တခြား stack က !ImportValue နဲ့ ယူသုံးလို့ရ

Export လုပ်ထားတဲ့ နာမည်ဟာ account နဲ့ region တစ်ခုအတွင်း unique ဖြစ်ရပါတယ်။ တခြား stack တစ်ခုက import လုပ်နေသရွေ့ အဲဒီ export ကို ပြောင်းလို့ ဖျက်လို့ မရပါဘူး — ဒါက stack နှစ်ခုကြားမှာ တင်းကျပ်တဲ့ contract တစ်ခု ဖြစ်စေပါတယ်။ !ImportValue က region မကျော်နိုင်ပါဘူး။

Stack ကို update လုပ်တဲ့အခါ CloudFormation က template အဟောင်းနဲ့ အသစ်ကို နှိုင်းယှဉ်ပြီး resource တစ်ခုချင်းအတွက် Add, Modify, Remove ကို ဆုံးဖြတ်ပါတယ်။ Modify ထဲမှာမှ property တစ်ခုချင်းရဲ့ update behavior ပေါ်မူတည်ပြီး in-place ပြင်မလား၊ replacement လုပ်မလား ဖြစ်ပါတယ်။ Change set ဆိုတာ ဒီ အစီအစဉ်ကို execute မလုပ်ခင် ကြည့်ခွင့်ပေးတဲ့ 'diff' ပါပဲ။

Operation တိုင်းဟာ state machine တစ်ခုကို ဖြတ်သွားပါတယ်။ Create အောင်မြင်ရင် CREATE_COMPLETE ဖြစ်ပြီး ကျရှုံးရင် (default အားဖြင့်) ROLLBACK_COMPLETE ဆီ ရောက်ပါတယ် — ဒီ state က stack ကို ဖျက်ပြီး ပြန်ဆောက်ဖို့ပဲ ရပါတယ်။ Update ကျရှုံးရင်တော့ UPDATE_ROLLBACK_COMPLETE ဆီ ပြန်ရောက်ပြီး stack ဟာ အရင်အခြေအနေအတိုင်း ဆက်သုံးလို့ ရပါတယ်။ Rollback ကိုယ်တိုင် ကျရှုံးရင် UPDATE_ROLLBACK_FAILED ဆိုတဲ့ 'ပိတ်မိနေတဲ့' state ဆီ ရောက်ပါတယ်။

text
STACK STATES YOU WILL MEET
--------------------------
  create-stack
      |
      v
 CREATE_IN_PROGRESS --ok--> CREATE_COMPLETE <------------------+
      | fail                     |                             |
      v                          | update / execute-change-set |
 ROLLBACK_IN_PROGRESS            v                             |
      |                   UPDATE_IN_PROGRESS --ok--> UPDATE_COMPLETE
      v                          | fail
 ROLLBACK_COMPLETE               v
 (delete and recreate)    UPDATE_ROLLBACK_IN_PROGRESS
                                 |            \
                                 v             \ rollback fails
                    UPDATE_ROLLBACK_COMPLETE    v
                    (old state, usable)   UPDATE_ROLLBACK_FAILED
                                          (fix, continue-update-rollback)
Update တိုင်းရဲ့ လမ်းကြောင်း
  1. Edit the template in Git
  2. Create a change set
  3. Read Action and Replacement
  4. Execute, watch the events
  5. Detect drift on a schedule

Replacement: True ကို stateful resource (database, table, bucket) ပေါ်မှာ မြင်တာနဲ့ ရပ်ပြီး ပြန်စဉ်းစားပါ။

Template section တွေ တစ်နေရာထဲမှာ

anatomy.yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: Minimal template showing every common section

Parameters:            # inputs at deploy time
  EnvName:
    Type: String
    AllowedValues: [dev, prod]
    Default: dev

Mappings:              # static lookup tables
  EnvConfig:
    dev:
      RetentionSeconds: 345600
    prod:
      RetentionSeconds: 1209600

Conditions:            # booleans derived from parameters
  IsProd: !Equals [!Ref EnvName, prod]

Resources:             # the only required section
  WorkQueue:
    Type: AWS::SQS::Queue
    Properties:
      MessageRetentionPeriod: !FindInMap [EnvConfig, !Ref EnvName, RetentionSeconds]
      SqsManagedSseEnabled: !If [IsProd, true, false]

Outputs:               # values other stacks or people can read
  WorkQueueUrl:
    Value: !Ref WorkQueue
    Export:
      Name: !Sub "${AWS::StackName}-WorkQueueUrl"

Parameter က input၊ Mapping က ဇယား၊ Condition က boolean၊ Resource က တကယ် ဆောက်မယ့်အရာ၊ Output က ထုတ်ပေးမယ့် တန်ဖိုးပါ။ !FindInMap က env အလိုက် တန်ဖိုး ရှာပေးပြီး !If က condition ပေါ်မူတည်ပြီး တန်ဖိုး ရွေးပေးပါတယ်။

Data ကို ကာကွယ်ထားတဲ့ orders-data stack

ဒီ template က orders table, order queue နဲ့ dead-letter queue ကို ဆောက်ပါတယ်။ dev နဲ့ prod ကို template တစ်ခုတည်းနဲ့ ထိန်းပြီး prod မှာပဲ point-in-time recovery နဲ့ deletion protection ကို ဖွင့်ထားပါတယ်။ Table မှာ DeletionPolicy နဲ့ UpdateReplacePolicy နှစ်ခုလုံးကို Retain ထားလို့ stack ကို ဖျက်ဖျက်၊ replacement ဖြစ်ဖြစ် data မပျောက်ပါဘူး။

orders-data.yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: Orders data layer - DynamoDB table plus work queue with a DLQ

Parameters:
  EnvName:
    Type: String
    AllowedValues: [dev, prod]
    Description: Deployment environment

Mappings:
  EnvConfig:
    dev:
      MaxReceiveCount: 3
      RetentionSeconds: 345600
    prod:
      MaxReceiveCount: 5
      RetentionSeconds: 1209600

Conditions:
  IsProd: !Equals [!Ref EnvName, prod]

Resources:
  OrdersTable:
    Type: AWS::DynamoDB::Table
    DeletionPolicy: Retain
    UpdateReplacePolicy: Retain
    Properties:
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: pk
          AttributeType: S
        - AttributeName: sk
          AttributeType: S
      KeySchema:
        - AttributeName: pk
          KeyType: HASH
        - AttributeName: sk
          KeyType: RANGE
      PointInTimeRecoverySpecification:
        PointInTimeRecoveryEnabled: !If [IsProd, true, false]
      DeletionProtectionEnabled: !If [IsProd, true, false]
      Tags:
        - Key: env
          Value: !Ref EnvName

  OrdersDLQ:
    Type: AWS::SQS::Queue
    Properties:
      MessageRetentionPeriod: 1209600
      SqsManagedSseEnabled: true

  OrdersQueue:
    Type: AWS::SQS::Queue
    Properties:
      SqsManagedSseEnabled: true
      MessageRetentionPeriod: !FindInMap [EnvConfig, !Ref EnvName, RetentionSeconds]
      RedrivePolicy:
        deadLetterTargetArn: !GetAtt OrdersDLQ.Arn
        maxReceiveCount: !FindInMap [EnvConfig, !Ref EnvName, MaxReceiveCount]

Outputs:
  TableName:
    Value: !Ref OrdersTable
    Export:
      Name: !Sub "${AWS::StackName}-TableName"
  QueueUrl:
    Value: !Ref OrdersQueue
    Export:
      Name: !Sub "${AWS::StackName}-QueueUrl"
  QueueName:
    Value: !GetAtt OrdersQueue.QueueName
    Export:
      Name: !Sub "${AWS::StackName}-QueueName"

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

  1. Parameters / AllowedValuesAllowedValues: [dev, prod]

    Typo (prd, production) ကို deploy မစခင် ငြင်းပယ်ပါတယ်။ Default မထားတာက prod ကို မတော်တဆ deploy မလုပ်မိအောင် တမင်ပါ — EnvName ကို အမြဲ ထည့်ပေးရပါတယ်။

  2. Mappings + !FindInMap!FindInMap [EnvConfig, !Ref EnvName, RetentionSeconds]

    env အလိုက် ကွာတဲ့ တန်ဖိုးတွေကို parameter အများကြီး မလုပ်ဘဲ ဇယားတစ်ခုထဲ စုထားပါတယ်။ prod မှာ message ကို ၁၄ ရက်၊ dev မှာ ၄ ရက် ထားပါတယ်။

  3. Conditions + !IfPointInTimeRecoveryEnabled: !If [IsProd, true, false]

    Point-in-time recovery နဲ့ deletion protection ကို prod မှာပဲ ဖွင့်ပါတယ်။ Condition ကို resource တစ်ခုလုံးမှာ (Condition: IsProd) တပ်ပြီး resource ကိုယ်တိုင်ကို ဖွင့်/ပိတ်လို့လည်း ရပါတယ်။

  4. DeletionPolicy / UpdateReplacePolicyDeletionPolicy: Retain UpdateReplacePolicy: Retain

    DeletionPolicy က stack ဖျက်တဲ့အခါ (သို့) resource ကို template ထဲက ဖယ်တဲ့အခါ အလုပ်လုပ်ပါတယ်။ UpdateReplacePolicy က replacement update ကြောင့် resource အဟောင်းကို ဖျက်မယ့်အခါ အလုပ်လုပ်ပါတယ်။ Problem section က data ပျောက်တာကို ဒုတိယတစ်ခုက တားပေးပါတယ်။

  5. Outputs / ExportName: !Sub "${AWS::StackName}-QueueName"

    Export name ထဲ stack name ထည့်ထားလို့ orders-data-dev နဲ့ orders-data-prod က account တစ်ခုတည်းမှာ ပဋိပက္ခ မဖြစ်ပါဘူး။ တခြား stack က !ImportValue orders-data-prod-QueueName နဲ့ ယူသုံးနိုင်ပါတယ်။

You should see
Sample output:

$ aws cloudformation deploy --template-file orders-data.yaml \
    --stack-name orders-data-dev --parameter-overrides EnvName=dev
Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - orders-data-dev

$ aws cloudformation list-exports --query "Exports[].Name" --output text
orders-data-dev-QueueName  orders-data-dev-QueueUrl  orders-data-dev-TableName

Change set — execute မလုပ်ခင် diff ကို ဖတ်ပါ

aws cloudformation deploy က နောက်ကွယ်မှာ change set ကို ဆောက်ပြီး ချက်ချင်း execute လုပ်ပါတယ်။ Production မှာတော့ အဆင့်နှစ်ဆင့် ခွဲပါ — change set ကို ဆောက်၊ လူ (သို့) pipeline က ဖတ်ပြီးမှ execute လုပ်ပါ။ deploy ကိုပဲ သုံးချင်ရင် --no-execute-changeset flag နဲ့ ဆောက်ရုံပဲ ဆောက်ခိုင်းလို့ ရပါတယ်။

ဖတ်ရမယ့် column အရေးအကြီးဆုံးကတော့ Replacement ပါ — True (သေချာ အသစ်ဆောက်မယ်)၊ Conditional (တခြား တန်ဖိုး ပေါ်မူတည်ပြီး ဖြစ်နိုင်တယ်)၊ False (in-place)။

change-set.sh
STACK=orders-data-prod
aws cloudformation create-change-set --stack-name "$STACK" \
  --change-set-name rename-table-2026-11-08 \
  --template-body file://orders-data.yaml \
  --parameters ParameterKey=EnvName,ParameterValue=prod
aws cloudformation wait change-set-create-complete --stack-name "$STACK" \
  --change-set-name rename-table-2026-11-08

# Read the plan: which resources change, and are any replaced?
aws cloudformation describe-change-set --stack-name "$STACK" \
  --change-set-name rename-table-2026-11-08 \
  --query "Changes[].ResourceChange.{Action:Action,Id:LogicalResourceId,\
Replace:Replacement}" --output table

# Only after a human (or a policy check) approves:
aws cloudformation execute-change-set --stack-name "$STACK" \
  --change-set-name rename-table-2026-11-08

Pipeline ထဲမှာ အလိုအလျောက် တားပါ

describe-change-set ရဲ့ output ကို jq နဲ့ စစ်ပြီး stateful resource (AWS::DynamoDB::Table, AWS::RDS::DBInstance, AWS::S3::Bucket) တစ်ခုခုမှာ Replacement True ပါရင် pipeline ကို fail လုပ်ခိုင်းတာ အသုံးများပါတယ်။ လူက မေ့တတ်ပေမယ့် script က မမေ့ပါဘူး။

Drift detection — လက်နဲ့ ပြင်ထားတာတွေကို ရှာခြင်း

Incident ကာလမှာ တစ်ယောက်ယောက်က console ကနေ security group rule ထည့်တာ၊ queue ရဲ့ visibility timeout ပြောင်းတာ ဖြစ်တတ်ပါတယ်။ Template နဲ့ တကယ့် resource မကိုက်တော့တာကို drift လို့ ခေါ်ပါတယ်။ နောက် deploy မှာ CloudFormation က template ထဲက တန်ဖိုးကို ပြန်ရေးလိုက်လို့ hotfix ပျောက်သွားတာ ဒါမှမဟုတ် update ကျရှုံးတာ ဖြစ်တတ်ပါတယ်။

Drift detection က template မှာ သတ်မှတ်ထားတဲ့ property တွေကို တကယ့် configuration နဲ့ တိုက်စစ်ပြီး resource တစ်ခုချင်းကို IN_SYNC, MODIFIED, DELETED, NOT_CHECKED လို့ အမှတ်ပေးပါတယ်။ Template မှာ မပါတဲ့ resource အသစ်တွေကိုတော့ မရှာပေးပါဘူး။ Resource type အားလုံး support မလုပ်တာကိုလည်း သတိပြုပါ။

drift.sh
DRIFT_ID=$(aws cloudformation detect-stack-drift --stack-name orders-data-prod \
  --query StackDriftDetectionId --output text)
aws cloudformation describe-stack-drift-detection-status \
  --stack-drift-detection-id "$DRIFT_ID" \
  --query "{Status:DetectionStatus,Drift:StackDriftStatus}"

# Show only what changed, with the expected vs actual property values
aws cloudformation describe-stack-resource-drifts --stack-name orders-data-prod \
  --stack-resource-drift-status-filters MODIFIED DELETED \
  --query "StackResourceDrifts[].{Id:LogicalResourceId,Diff:PropertyDifferences}"

Drift ကို တွေ့ရင် ရွေးစရာ နှစ်ခုပဲ ရှိပါတယ် — ပြောင်းလဲမှုက မှန်ရင် template ထဲ ထည့်ပြီး ပြန် deploy လုပ်ပါ၊ မမှန်ရင် template ကို ပြန် deploy လုပ်ပြီး ပြင်ဆင်မှုကို ဖျက်ပါ။ AWS Config ရဲ့ managed rule (cloudformation-stack-drift-detection-check) နဲ့ အချိန်ဇယားအလိုက် အလိုအလျောက် စစ်လို့လည်း ရပါတယ်။

Rollback နဲ့ ပိတ်မိနေတဲ့ stack တွေ

Stateဘာဖြစ်တာလဲဘာလုပ်ရမလဲ
ROLLBACK_COMPLETEပထမဆုံး create ကျရှုံး၊ resource တွေ ပြန်ဖျက်ပြီးEvent ထဲက ပထမ error ကို ဖတ်၊ stack ကို ဖျက်ပြီး ပြန်ဆောက်
UPDATE_ROLLBACK_COMPLETEUpdate ကျရှုံး၊ အရင်အခြေအနေ ပြန်ရောက်Stack သုံးလို့ရဆဲ၊ template ပြင်ပြီး ထပ် update
UPDATE_ROLLBACK_FAILEDRollback ကိုယ်တိုင် ကျရှုံး (ဥပမာ လက်နဲ့ ဖျက်ထားတဲ့ resource)အကြောင်းရင်းကို ပြင်၊ continue-update-rollback (လိုရင် --resources-to-skip)

Event list မှာ အောက်ဆုံးက ပထမဆုံး 'FAILED' ဖြစ်တဲ့ event ကို အမြဲ ရှာပါ။ နောက်က 'Resource creation cancelled' တွေက အကျိုးဆက်တွေသာ ဖြစ်ပါတယ်။ Development မှာ --disable-rollback နဲ့ deploy လုပ်ရင် ကျရှုံးတဲ့ resource တွေကို စစ်ဆေးဖို့ ထားခဲ့ပေးပြီး ပြင်ပြီးမှ ထပ် update လုပ်တာ (သို့) aws cloudformation rollback-stack နဲ့ လက်နဲ့ rollback လုပ်တာ ရပါတယ်။ Update တစ်ခုအတွင်း CloudWatch alarm ကို rollback trigger အဖြစ် ချိတ်ထားရင် alarm မြည်တာနဲ့ CloudFormation က အလိုအလျောက် ပြန်လှည့်ပေးပါတယ်။

unstick.sh
# Find the first failure (events are newest-first, so read from the bottom)
aws cloudformation describe-stack-events --stack-name orders-data-prod \
  --query "StackEvents[?contains(ResourceStatus,'FAILED')].\
[Timestamp,LogicalResourceId,ResourceStatusReason]" --output text

# Stuck in UPDATE_ROLLBACK_FAILED because OrdersDLQ was deleted by hand:
aws cloudformation continue-update-rollback --stack-name orders-data-prod \
  --resources-to-skip OrdersDLQ

Data ကို ကာကွယ်ခြင်း — DeletionPolicy, stack policy, termination protection

Policy တန်ဖိုးအဓိပ္ပာယ်
DeleteResource ကို ဖျက် (resource type အများစုရဲ့ default)
RetainStack ကနေ ဖယ်ပေမယ့် resource ကို account ထဲ ထားခဲ့ — နောက်မှ လက်နဲ့ ဖျက်ရ
Snapshotမဖျက်ခင် snapshot ယူ (RDS, EBS volume, ElastiCache, Redshift လို snapshot ရတဲ့ type တွေပဲ)

Stack policy ကတော့ stack update တွေကို ကန့်သတ်တဲ့ JSON document ပါ။ Stack policy တစ်ခု သတ်မှတ်လိုက်တာနဲ့ resource အားလုံးဟာ default အားဖြင့် update မလုပ်နိုင်တော့လို့ Allow statement ကို တမင် ထည့်ပေးရပါတယ်။ အောက်ကဥပမာက အားလုံးကို update ခွင့်ပြုပြီး OrdersTable ကိုပဲ replacement နဲ့ delete မလုပ်နိုင်အောင် တားပါတယ်။ Stack policy ကို ဖျက်လို့ မရပါဘူး၊ အသစ်နဲ့ အစားထိုးလို့ပဲ ရပါတယ်။ တကယ်လိုအပ်တဲ့ update တစ်ခုအတွက်တော့ --stack-policy-during-update-body နဲ့ ယာယီ ကျော်လို့ ရပါတယ်။

Termination protection ကတော့ stack တစ်ခုလုံးကို delete-stack ကနေ ကာကွယ်ပါတယ်။ Production stack တိုင်းမှာ ဖွင့်ထားသင့်ပါတယ်။

protect.sh
cat > stack-policy.json <<'EOF'
{
  "Statement": [
    { "Effect": "Allow", "Action": "Update:*", "Principal": "*", "Resource": "*" },
    {
      "Effect": "Deny",
      "Action": ["Update:Replace", "Update:Delete"],
      "Principal": "*",
      "Resource": "LogicalResourceId/OrdersTable"
    }
  ]
}
EOF
aws cloudformation set-stack-policy --stack-name orders-data-prod \
  --stack-policy-body file://stack-policy.json
aws cloudformation update-termination-protection --enable-termination-protection \
  --stack-name orders-data-prod

Nested stack နဲ့ StackSets

ဒီနှစ်ခုက နာမည်ဆင်ပေမယ့် ပြဿနာ မတူတာကို ဖြေရှင်းပါတယ်။ Nested stack (AWS::CloudFormation::Stack) က template ကြီးတစ်ခုကို ပြန်သုံးလို့ရတဲ့ အစိတ်အပိုင်းတွေအဖြစ် ခွဲတာပါ — parent တစ်ခုက child တွေကို deploy လုပ်ပြီး child output ကို !GetAtt Child.Outputs.Name နဲ့ ယူပါတယ်။ အားလုံး account တစ်ခု၊ region တစ်ခုထဲမှာပဲ ရှိပြီး တစ်ပြိုင်နက် update ဖြစ်ပါတယ်။ Local file ကနေ S3 ပေါ် တင်ဖို့ aws cloudformation package ကို သုံးပါတယ်။

StackSets ကတော့ template တစ်ခုတည်းကို account အများကြီး၊ region အများကြီးဆီ ဖြန့်တာပါ — ဥပမာ account တိုင်းမှာ CloudTrail, Config rule, baseline IAM role ထားချင်တဲ့အခါ။ SERVICE_MANAGED permission model က AWS Organizations နဲ့ ပေါင်းပြီး OU ထဲ account အသစ် ဝင်လာတာနဲ့ အလိုအလျောက် deploy လုပ်ပေးပါတယ်။ Operation preference တွေနဲ့ တစ်ပြိုင်နက် ဘယ်နှ account လုပ်မလဲ၊ ဘယ်နှခု ကျရှုံးရင် ရပ်မလဲ ထိန်းပါတယ်။

orders-parent.yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: Parent stack composing reusable child templates

Parameters:
  EnvName:
    Type: String
    AllowedValues: [dev, prod]

Resources:
  DataStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://cfn-tpl-111122223333.s3.amazonaws.com/orders-data.yaml
      Parameters:
        EnvName: !Ref EnvName

  AlarmsStack:
    Type: AWS::CloudFormation::Stack
    Properties:
      TemplateURL: https://cfn-tpl-111122223333.s3.amazonaws.com/alarms.yaml
      Parameters:
        QueueName: !GetAtt DataStack.Outputs.QueueName

Outputs:
  OrdersTableName:
    Value: !GetAtt DataStack.Outputs.TableName
stackset.sh
# One baseline template -> every account in an OU, two regions
aws cloudformation create-stack-set --stack-set-name org-baseline \
  --template-body file://baseline.yaml \
  --permission-model SERVICE_MANAGED \
  --auto-deployment Enabled=true,RetainStacksOnAccountRemoval=false \
  --capabilities CAPABILITY_NAMED_IAM

aws cloudformation create-stack-instances --stack-set-name org-baseline \
  --deployment-targets OrganizationalUnitIds=ou-ab12-34cd56ef \
  --regions us-east-1 ap-southeast-1 \
  --operation-preferences MaxConcurrentPercentage=25,FailureTolerancePercentage=10
Nested stackStackSets
ပြဿနာTemplate ကြီးလွန်း၊ ပြန်သုံးချင်Account/region အများကြီးမှာ အတူတူ
ScopeAccount တစ်ခု၊ region တစ်ခုAccount အများ x region အများ
Data ပေးပို့ပုံParameter + !GetAtt OutputsParameter (stack instance အလိုက် override)

Production မှာ

Production team တွေ CloudFormation ကို ဘယ်လို ကိုင်တွယ်လဲ

ရင့်ကျက်တဲ့ team တွေမှာ console ကနေ stack ကို update လုပ်ခွင့် လူတိုင်းမှာ မရှိပါဘူး။ Template တွေ Git ထဲမှာ ရှိပြီး pull request တိုင်းမှာ cfn-lint (syntax နဲ့ property စစ်ဖို့) နဲ့ cfn-guard ဒါမှမဟုတ် Checkov (policy စစ်ဖို့ — 'bucket တိုင်း encrypt လုပ်ထားရမယ်' စသဖြင့်) ကို run ပါတယ်။ Merge ပြီးရင် pipeline က change set ဆောက်၊ replacement ပါမပါ စစ်၊ prod အတွက် လူ approval ယူ၊ ပြီးမှ execute လုပ်ပါတယ်။

Stack တွေကို lifecycle အလိုက် ခွဲပါတယ် — network stack (တစ်နှစ်မှ တစ်ခါလောက် ပြောင်း)၊ data stack (ခဏခဏ မပြောင်း၊ Retain အပြည့်)၊ application stack (တစ်နေ့ ဆယ်ခါ deploy)။ ဒါမှ app deploy တစ်ခု မှားတာက database ကို မထိနိုင်ပါဘူး။ Organization အဆင့်မှာတော့ StackSets (သို့) Control Tower customization နဲ့ account အသစ်တိုင်းကို baseline တူတူ ရအောင် လုပ်ပါတယ်။ Drift detection ကို အပတ်စဉ် run ပြီး ရလဒ်ကို ticket အဖြစ် ဖွင့်ပါတယ်။ Production stack တိုင်းမှာ termination protection ဖွင့်ထားမထား ဆိုတာကိုလည်း script နဲ့ အလိုအလျောက် စစ်ဆေးလေ့ ရှိပါတယ်။

အမှားများ

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

  • Stateful resource မှာ UpdateReplacePolicy မထားခြင်း — DeletionPolicy: Retain ထားရုံနဲ့ replacement update ကနေ မကာကွယ်ပါဘူး။ Table, bucket, database တိုင်းမှာ နှစ်ခုလုံး ထားပါ။
  • Custom physical name (TableName, BucketName) ကို မလိုဘဲ သတ်မှတ်ခြင်း — နာမည်တူ resource နှစ်ခု တစ်ပြိုင်နက် မရှိနိုင်လို့ replacement လိုတဲ့ update တွေ ကျရှုံးပါတယ်၊ env တစ်ခုထက်ပိုပြီး deploy လုပ်ရင်လည်း ပဋိပက္ခ ဖြစ်ပါတယ်။ CloudFormation ကို နာမည် generate လုပ်ခိုင်းပြီး Output နဲ့ ထုတ်ပေးတာ ပိုကောင်းပါတယ်။
  • Export ကို နေရာတိုင်းမှာ သုံးခြင်း — import လုပ်ထားသရွေ့ producer stack က အဲဒီ output ကို ပြောင်းလို့ မရလို့ stack တွေ တစ်ခုနဲ့တစ်ခု ချည်နှောင်ခံရပါတယ်။ ခဏခဏ ပြောင်းမယ့် တန်ဖိုးဆိုရင် SSM Parameter Store ကို သုံးတာ ပိုပြေပြစ်ပါတယ်။
  • ROLLBACK_COMPLETE stack ကို update လုပ်ဖို့ ကြိုးစားခြင်း — ပထမ create ကျရှုံးထားတဲ့ stack ကို update မလုပ်နိုင်ပါဘူး၊ ဖျက်ပြီး ပြန်ဆောက်ရပါတယ်။
  • Drift ကို လျစ်လျူရှုခြင်း — console hotfix က နောက် deploy မှာ ပြန်ပျောက်သွားတာ ဒါမှမဟုတ် update ကို ကျရှုံးစေတာ ဖြစ်ပါတယ်။

💰 Cost & Cleanup

CloudFormation ကိုယ်တိုင်က AWS:: namespace resource တွေအတွက် အခမဲ့ပါ (third-party resource type, hook တွေအတွက်ပဲ operation အလိုက် ကြေး ရှိပါတယ်)။ ငွေကျတာက stack ဆောက်ပေးတဲ့ resource တွေပါ။ ဒီ lab ရဲ့ on-demand DynamoDB table နဲ့ SQS queue တွေက အသုံးမပြုရင် ကုန်ကျစရိတ် အနည်းဆုံးပဲ ရှိပြီး free tier ထဲမှာ ပါတတ်ပါတယ်။ သတိထားရမှာက Retain ထားတဲ့ table ပါ — stack ကို ဖျက်ပြီးတာတောင် table က account ထဲမှာ ကျန်နေပြီး storage ကြေး ဆက်တက်နေပါမယ်၊ လက်နဲ့ ဖျက်ရပါမယ်။

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

bash
TABLE=$(aws cloudformation describe-stacks --stack-name orders-data-dev \
  --query "Stacks[0].Outputs[?OutputKey=='TableName'].OutputValue" --output text)
aws cloudformation delete-stack --stack-name orders-data-dev
aws cloudformation wait stack-delete-complete --stack-name orders-data-dev
# DeletionPolicy: Retain left the table behind on purpose - remove it by hand
aws dynamodb delete-table --table-name "$TABLE"

Mini challenge

Export ကို import လုပ်တဲ့ alarm stack

orders-data stack ရဲ့ QueueName export ကို import လုပ်ပြီး queue ထဲ message တွေ ကြာကြာ ပိတ်မိနေရင် CloudWatch alarm မြည်အောင် orders-alarms.yaml template တစ်ခု ရေးပါ။

  • DataStackName parameter (default orders-data-dev) နဲ့ export name ကို !Sub ဖြင့် တည်ဆောက်ပြီး !ImportValue သုံးရမယ်
  • ApproximateAgeOfOldestMessage metric ပေါ်မှာ alarm ထားရမယ်
  • Threshold ကို Mappings ကနေ env အလိုက် ယူရမယ် (dev 3600 စက္ကန့်၊ prod 300 စက္ကန့်)
  • cfn-lint ကို error မရှိဘဲ ဖြတ်ရမယ်

ရလဒ်: Alarm stack deploy ဖြစ်ပြီးနောက် orders-data-dev ကို ဖျက်ဖို့ ကြိုးစားရင် 'Export ... cannot be deleted as it is in use' error ရပါမယ် — export contract အလုပ်လုပ်နေတာကို ပြပါတယ်။

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

!ImportValue ထဲမှာ short-form !Sub ကို တိုက်ရိုက် မထည့်နိုင်ပါဘူး — Fn::Sub ကို long form နဲ့ ရေးပါ။ SQS metric ရဲ့ namespace က AWS/SQS ဖြစ်ပြီး dimension က QueueName ပါ။

အဖြေ ကြည့်ရန်
orders-alarms.yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: Alarm on stuck messages in the orders queue

Parameters:
  EnvName:
    Type: String
    AllowedValues: [dev, prod]
    Default: dev
  DataStackName:
    Type: String
    Default: orders-data-dev

Mappings:
  AgeThreshold:
    dev:
      Seconds: 3600
    prod:
      Seconds: 300

Resources:
  OldestMessageAlarm:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmDescription: Orders are waiting too long in the queue
      Namespace: AWS/SQS
      MetricName: ApproximateAgeOfOldestMessage
      Dimensions:
        - Name: QueueName
          Value: !ImportValue
            Fn::Sub: "${DataStackName}-QueueName"
      Statistic: Maximum
      Period: 60
      EvaluationPeriods: 5
      Threshold: !FindInMap [AgeThreshold, !Ref EnvName, Seconds]
      ComparisonOperator: GreaterThanThreshold
      TreatMissingData: notBreaching

Outputs:
  AlarmName:
    Value: !Ref OldestMessageAlarm

Quick Quiz 1 / 3

Change set မှာ OrdersTable အတွက် 'Action: Modify, Replacement: True' လို့ ပြနေပါတယ်။ Template မှာ DeletionPolicy: Retain ပဲ ပါပါတယ်။ Execute လုပ်ရင် ဘာဖြစ်မလဲ?

Quick Quiz 2 / 3

Template တစ်ခုတည်းကို Organization ထဲက account ၄၀ နဲ့ region ၂ ခုမှာ deploy ချင်ပြီး account အသစ် ဝင်လာရင်လည်း အလိုအလျောက် ရောက်စေချင်ပါတယ်။ ဘာကို သုံးမလဲ?

Quick Quiz 3 / 3

Stack က UPDATE_ROLLBACK_FAILED မှာ ပိတ်မိနေပါတယ်၊ အကြောင်းက တစ်ယောက်ယောက်က DLQ ကို console ကနေ ဖျက်ထားလို့ပါ။ ပထမဆုံး ဘာလုပ်သင့်လဲ?

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

  • Resources ပဲ မဖြစ်မနေ လိုပြီး Parameters, Mappings, Conditions, Outputs က template ကို env အများကြား ပြန်သုံးလို့ရအောင် လုပ်ပေးပါတယ်။
  • Production update တိုင်းကို change set နဲ့ ကြိုကြည့်ပြီး Replacement column ကို အထူးဂရုစိုက်ပါ။
  • Stateful resource တိုင်းမှာ DeletionPolicy နဲ့ UpdateReplacePolicy နှစ်ခုလုံး ထားပြီး stack policy နဲ့ termination protection နဲ့ ထပ်ကာပါ။
  • Drift detection က console ပြင်ဆင်မှုကို ရှာပေးပြီး rollback state တွေကနေ ဘယ်လို ထွက်ရမလဲ သိထားပါ။
  • Nested stack က template ခွဲဖို့၊ StackSets က account/region အများကြီးဆီ ဖြန့်ဖို့ပါ။

AWS CloudFormation User Guide — AWS Documentation

Update stacks using change sets — AWS Documentation

AWS CloudFormation StackSets — AWS Documentation

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

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

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