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

CloudFront, AWS WAF နဲ့ Shield — Edge မှာ ကာကွယ်ပြီး မြန်အောင်လုပ်ခြင်း

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

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

  • CloudFront ရဲ့ cache key, cache behavior, cache policy နဲ့ origin request policy ကွာခြားချက်ကို ရှင်းပြနိုင်မယ်
  • Origin Access Control (OAC) သုံးပြီး S3 bucket ကို လုံးဝ private ထားရင်း CloudFront ကနေ serve လုပ်နိုင်မယ်
  • TTL, invalidation, CloudFront Functions နဲ့ Lambda@Edge ကို ဘယ်အချိန် သုံးရမလဲ ဆုံးဖြတ်နိုင်မယ်
  • Managed rule group နဲ့ rate-based rule ပါတဲ့ WAF web ACL ကို count mode ကနေ block mode အထိ ဘေးကင်းစွာ rollout လုပ်နိုင်မယ်
  • Shield Standard နဲ့ Shield Advanced ဘာကွာလဲ၊ Advanced ဘယ်အချိန်မှ တန်လဲ ခွဲခြားနိုင်မယ်

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

Edge ဆိုတာ ဘာလဲ — ရိုးရိုးလေး

ကိုယ့်ဆိုင်ရဲ့ ဂိုဒေါင်တစ်ခုတည်းက Virginia မှာပဲ ရှိပြီး Yangon, Mandalay, Bangkok က customer တိုင်းက ပါဆယ်ထုပ်တစ်ခုချင်းစီ ကမ္ဘာကိုဖြတ်ပြီး ရောက်လာတာကို စောင့်နေရတယ်လို့ စိတ်ကူးကြည့်ရအောင်။ CloudFront ဆိုတာ မြို့ရာနဲ့ချီမှာ စတိုခန်းလေးတွေ ဖွင့်ထားသလိုပါပဲ။ မြို့တစ်မြို့ရဲ့ ပထမဆုံး customer ကတော့ ပစ္စည်းကို ဂိုဒေါင်ကြီးကနေ ရောက်လာအောင် စောင့်ရပေမယ့်၊ နောက်ကလူတွေအားလုံးကတော့ ဒေသခံ စတိုခန်းထဲက copy ကို millisecond ပိုင်းအတွင်း ရသွားကြပါတယ်။

AWS WAF ကတော့ စတိုခန်း တံခါးတိုင်းမှာ ရပ်နေတဲ့ အစောင့်ပါ။ ဝင်လာသူတိုင်းကို rule စာရင်းနဲ့ တိုက်စစ်ပါတယ် — သိပြီးသား attack pattern တွေ၊ address တစ်ခုတည်းကနေ အကြိမ်လွန်ဝင်လာတာတွေ၊ ကိုယ် service မပေးတဲ့ နိုင်ငံတွေက request တွေ — rule မအောင်တဲ့သူကို ပြန်လှည့်ခိုင်းပါတယ်။

AWS Shield ကတော့ အဆောက်အအုံတစ်ခုလုံးကို ကာထားတဲ့ ရေကာတံတိုင်း (flood wall) ပါ။ အမှိုက် traffic လှိုင်းကြီးတွေ (DDoS attack) ကို စုပ်ယူပေးလို့ အစောင့်ရော စင်ပေါ်က ပစ္စည်းတွေပါ ဘယ်တော့မှ လွှမ်းမိုးမခံရပါဘူး။ ဒီသင်ခန်းစာမှာ ဒီအလွှာသုံးခုကို တစ်ခုချင်း တည်ဆောက်သွားမှာပါ။

Problem → Why → Solution

Public S3 website ရဲ့ ပြဿနာ

AWS Fundamentals မှာ static site ကို S3 website endpoint ကနေ တိုက်ရိုက် host လုပ်ခဲ့ပါတယ်။ အလုပ်တော့ လုပ်ပါတယ်၊ ဒါပေမယ့် ဘာတွေ လုပ်ခဲ့ရလဲ ပြန်ကြည့်ပါ — Block Public Access ကို ပိတ်ခဲ့ရတယ်၊ internet တစ်ခုလုံးက object တိုင်းကို ဖတ်ခွင့်ရတဲ့ bucket policy တပ်ခဲ့ရတယ်၊ website endpoint ပေါ်က plain HTTP ကိုလည်း လက်ခံခဲ့ရတယ်။ Yangon ကလာလာ Frankfurt ကလာလာ request တိုင်းဟာ region တစ်ခုတည်းဆီ အဆုံးထိ သွားရပါတယ်။

အခု ဒီ site ကို marketing campaign တစ်ခုနောက်မှာ ထားလိုက်ပါ။ အဝေးက user တွေအတွက် request တစ်ခုချင်း latency က millisecond ရာနဲ့ချီ ရှိမယ်၊ page view တိုင်းဟာ ငွေကျတဲ့ S3 GET request နဲ့ data transfer ဖြစ်မယ်၊ bot တစ်ခုက /index.html ကို တစ်စက္ကန့် ထောင်နဲ့ချီ ထုနေရင်လည်း bucket ဆီ တန်းရောက်ပါတယ်။ 'ဒီ IP ကို block လုပ်' ဒါမှမဟုတ် 'client တစ်ခုကို ငါးမိနစ်မှာ request 2,000 ပဲ ခွင့်ပြု' လို့ ပြောလို့ရမယ့် နေရာ တစ်ခုမှ မရှိပါဘူး။

public-website.sh
# The AWS Fundamentals way: a public bucket website (not for production)
BUCKET=shop-site-111122223333
aws s3api delete-public-access-block --bucket "$BUCKET"
aws s3api put-bucket-policy --bucket "$BUCKET" --policy file://public-read.json
aws s3 website "s3://$BUCKET" --index-document index.html --error-document error.html
# Result: http://shop-site-111122223333.s3-website-us-east-1.amazonaws.com
#   HTTP only, one region, every object world-readable, no rate limiting

ဘာကြောင့် scale မှာ ပျက်သလဲ

Origin ဟာ internet ကို တိုက်ရိုက် ပေါ်နေပါတယ် — load ကို စုပ်ယူပေးမယ့် cache မရှိ၊ user နားမှာ HTTPS ကို ဖြေရှင်းပေးမယ့် edge မရှိ၊ internet နဲ့ ကိုယ့် data ကြားမှာ စစ်ဆေးပေးမယ့် အလွှာ မရှိပါဘူး။ Bucket ကို public လုပ်ထားတော့ နောက်မှ ရှေ့က ဘာပဲ ထားထား လူတိုင်း ကျော်ပြီး bucket ကို တိုက်ရိုက် ခေါ်လို့ ရနေပါသေးတယ်။ ဖြေရှင်းနည်းကတော့ bucket ကို private ပြန်လုပ်၊ ရှေ့မှာ CloudFront ထား၊ အဲဒီ distribution တစ်ခုတည်းကိုပဲ bucket ဖတ်ခွင့်ပေး၊ edge မှာ WAF rule တွေ တပ်ဖို့ပါပဲ။

CloudFront, OAC, WAF, Shield ဘယ်လို ပေါင်းအလုပ်လုပ်လဲ

CloudFront ဟာ edge location ရာနဲ့ချီ နဲ့ အဲဒီ edge တွေနဲ့ origin ကြားမှာ ရှိတဲ့ regional edge cache တွေနဲ့ ဖွဲ့ထားတဲ့ content delivery network (CDN) ပါ။ Viewer တစ်ယောက်ဟာ DNS ကနေတဆင့် အနီးဆုံး edge ဆီ ရောက်ပါတယ်။ Edge မှာ copy ရှိနေရင် (cache hit) ချက်ချင်း ပြန်ပေးပါတယ်၊ မရှိရင် (cache miss) regional edge cache ကို မေးပြီး အဲဒီမှာလည်း မရှိမှ origin ဆီ သွားပါတယ်။

Object တစ်ခုကို ခွဲခြားသိမှတ်တဲ့ နည်းကို cache key လို့ ခေါ်ပါတယ်။

Cache key
Cached object တစ်ခုကို ခွဲခြားဖို့ သုံးတဲ့ value အစုပါ — default အားဖြင့် domain နဲ့ URL path ပါပြီး၊ cache policy ကနေ ထည့်ခိုင်းထားတဲ့ header, cookie, query string တွေလည်း ပါနိုင်ပါတယ်။ Cache key ထဲ value တစ်ခု ထပ်ထည့်တိုင်း cache ကို အပိုင်းပိုင်း ပိုခွဲသလို ဖြစ်ပြီး hit ratio ကျသွားပါတယ်။

Distribution တစ်ခုမှာ cache behavior တွေ ရှိပါတယ်။ Behavior တစ်ခုစီဟာ path pattern (/api/*, /assets/*, default *) တစ်ခုနဲ့ ကိုက်ပြီး ကိုယ်ပိုင် origin, viewer protocol policy, function association တွေနဲ့ policy နှစ်ခု ပါပါတယ်။ Cache policy က cache key ထဲ ဘာပါမလဲ၊ TTL (minimum, default, maximum) ဘယ်လောက်လဲ ဆုံးဖြတ်ပါတယ်။ Origin request policy ကတော့ cache key ကို မခွဲစေဘဲ origin ဆီ ဘယ် header, cookie, query string တွေ ထပ်ပို့မလဲ ဆုံးဖြတ်ပါတယ်။ ဒီလို ခွဲထားလို့ API တစ်ခုက Authorization header လိုအပ်တာကို cache ဖျက်စီးမခံဘဲ ပို့ပေးလို့ ရပါတယ်။

Origin Access Control (OAC) က CloudFront ကို origin ဆီ ပို့တဲ့ request တိုင်းကို SigV4 နဲ့ sign လုပ်ခိုင်းပါတယ်။ Bucket policy ကတော့ cloudfront.amazonaws.com service principal ကိုပဲ ခွင့်ပြုပြီး AWS:SourceArn condition နဲ့ ကိုယ့် distribution ARN တစ်ခုတည်းကို ချည်ထားပါတယ်။ ဒါကြောင့် bucket ဟာ Block Public Access ဖွင့်ထားရင်း လုံးဝ private ဖြစ်နေပါတယ်။ OAC ဟာ အဟောင်း Origin Access Identity (OAI) ကို အစားထိုးတာဖြစ်ပြီး SSE-KMS encrypt ထားတဲ့ object တွေကိုပါ support လုပ်ပါတယ်။

AWS WAF web ACL ကို distribution နဲ့ ချိတ်ထားရင် request တိုင်းကို cache မကြည့်ခင် edge မှာတင် စစ်ပါတယ်။ Rule တွေကို priority အစဉ်လိုက် စစ်ပြီး ပထမဆုံး terminating action (Allow, Block, CAPTCHA, Challenge) နဲ့ ရပ်ပါတယ်၊ Count ကတော့ မှတ်ရုံသာ မှတ်ပြီး ဆက်သွားပါတယ်။ AWS က ထိန်းသိမ်းပေးတဲ့ managed rule group တွေက OWASP-style attack, bad input, IP reputation တွေကို ဖုံးလွှမ်းပြီး rate-based rule က client တစ်ခုချင်းရဲ့ request နှုန်းကို ကန့်သတ်ပါတယ်။

Shield Standard ကတော့ customer အားလုံးအတွက် အပိုကြေး မပေးရဘဲ အမြဲဖွင့်ထားပြီး SYN flood, UDP reflection လို layer 3/4 attack တွေကို ကာကွယ်ပါတယ်။ Shield Advanced ကတော့ ငွေပေးရတဲ့ အဆင့်ပါ — နောက်ပိုင်း section မှာ ကြည့်ပါမယ်။

text
REQUEST PATH THROUGH THE EDGE
-----------------------------
 Viewer (Yangon)              Edge location (Singapore)
     |                 +-------------------------------------+
     |  HTTPS request  | 1. AWS WAF web ACL (priority order) |
     +---------------> | 2. CloudFront Function (viewer req) |
                       | 3. Cache lookup by cache key        |
                       +------------------+------------------+
                                          | miss
                                          v
                       +-------------------------------------+
                       |  Regional edge cache                |
                       +------------------+------------------+
                                          | miss, signed with SigV4 (OAC)
                                          v
                       +-------------------------------------+
                       |  Private S3 bucket (us-east-1)      |
                       |  Block Public Access = ON           |
                       +-------------------------------------+
 Shield Standard: always-on L3/L4 DDoS protection at every edge
Request တစ်ခုရဲ့ ခရီး
  1. Viewer -> nearest edge
  2. WAF web ACL checks rules
  3. Cache key lookup: hit or miss
  4. Miss: fetch origin, signed by OAC
  5. Store for TTL, return response

Cache key သေးလေ hit ratio မြင့်လေ၊ origin ပိုသက်သာလေ ဖြစ်ပါတယ်။ WAF ဟာ cache မတိုင်ခင် run လို့ cache hit ဖြစ်မယ့် request တောင် block ခံရနိုင်ပါတယ်။

OAC bucket policy ပုံစံ

oac-bucket-policy.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowCloudFrontServicePrincipalReadOnly",
      "Effect": "Allow",
      "Principal": { "Service": "cloudfront.amazonaws.com" },
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::shop-site-111122223333/*",
      "Condition": {
        "StringEquals": {
          "AWS:SourceArn": "arn:aws:cloudfront::111122223333:distribution/E2QWRUHAPOMQZL"
        }
      }
    }
  ]
}

Principal က CloudFront service ဖြစ်ပြီး AWS:SourceArn condition က 'ဒီ distribution တစ်ခုတည်းကပဲ' လို့ ကန့်သတ်ပါတယ်။ ဒီ condition မပါရင် account ဘယ်ကပဲ ဖြစ်ဖြစ် CloudFront distribution တိုင်းက ကိုယ့် bucket ကို ဖတ်လို့ ရသွားနိုင်ပါတယ်။ Action ကိုလည်း s3:GetObject ပဲ ပေးထားတာ သတိပြုပါ။

Private origin + OAC + WAF ပါတဲ့ distribution တစ်ခု အပြည့်အစုံ

အောက်က CloudFormation template က private bucket, OAC, viewer-request CloudFront Function, managed rule group (count mode) နဲ့ rate-based rule (block mode) ပါတဲ့ WAF web ACL နဲ့ distribution ကို stack တစ်ခုတည်းမှာ တည်ဆောက်ပါတယ်။ CloudFormation အသေးစိတ်ကို နောက် chapter မှာ နက်နက်ရှိုင်းရှိုင်း လေ့လာမှာဖြစ်လို့ အခုတော့ resource တွေ ဘယ်လို ချိတ်ဆက်ထားလဲကိုပဲ အာရုံစိုက်ပါ။

us-east-1 မှာပဲ deploy လုပ်ပါ

CloudFront နဲ့ ချိတ်မယ့် web ACL (Scope: CLOUDFRONT) ကို us-east-1 (N. Virginia) မှာပဲ ဖန်တီးလို့ ရပါတယ်။ ဒါကြောင့် ဒီ stack တစ်ခုလုံးကို us-east-1 မှာ deploy လုပ်ပါ။ Bucket ကိုယ်တိုင်ကတော့ ဘယ် region မှာ ဖြစ်ဖြစ် ရပါတယ်။

edge-site.yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: Private S3 origin behind CloudFront with OAC and an AWS WAF web ACL

Parameters:
  RateLimit:
    Type: Number
    Default: 1000
    Description: Max requests per client IP in each 5-minute window

Resources:
  SiteBucket:
    Type: AWS::S3::Bucket
    Properties:
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true
      BucketEncryption:
        ServerSideEncryptionConfiguration:
          - ServerSideEncryptionByDefault:
              SSEAlgorithm: AES256

  SiteOAC:
    Type: AWS::CloudFront::OriginAccessControl
    Properties:
      OriginAccessControlConfig:
        Name: !Sub "${AWS::StackName}-oac"
        OriginAccessControlOriginType: s3
        SigningBehavior: always
        SigningProtocol: sigv4

  IndexRewriteFunction:
    Type: AWS::CloudFront::Function
    Properties:
      Name: !Sub "${AWS::StackName}-index-rewrite"
      AutoPublish: true
      FunctionConfig:
        Comment: Append index.html to directory-style URLs
        Runtime: cloudfront-js-2.0
      FunctionCode: |
        function handler(event) {
          var request = event.request;
          if (request.uri.endsWith('/')) {
            request.uri += 'index.html';
          }
          return request;
        }

  EdgeWebAcl:
    Type: AWS::WAFv2::WebACL
    Properties:
      Name: !Sub "${AWS::StackName}-edge-acl"
      Scope: CLOUDFRONT
      DefaultAction:
        Allow: {}
      VisibilityConfig:
        SampledRequestsEnabled: true
        CloudWatchMetricsEnabled: true
        MetricName: edge-acl
      Rules:
        - Name: aws-common-rules
          Priority: 0
          OverrideAction:
            Count: {}
          Statement:
            ManagedRuleGroupStatement:
              VendorName: AWS
              Name: AWSManagedRulesCommonRuleSet
          VisibilityConfig:
            SampledRequestsEnabled: true
            CloudWatchMetricsEnabled: true
            MetricName: aws-common-rules
        - Name: rate-limit-per-ip
          Priority: 1
          Action:
            Block: {}
          Statement:
            RateBasedStatement:
              Limit: !Ref RateLimit
              AggregateKeyType: IP
          VisibilityConfig:
            SampledRequestsEnabled: true
            CloudWatchMetricsEnabled: true
            MetricName: rate-limit-per-ip

  Distribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        Enabled: true
        Comment: shop static site
        DefaultRootObject: index.html
        HttpVersion: http2and3
        PriceClass: PriceClass_100
        WebACLId: !GetAtt EdgeWebAcl.Arn
        Origins:
          - Id: s3-site
            DomainName: !GetAtt SiteBucket.RegionalDomainName
            OriginAccessControlId: !GetAtt SiteOAC.Id
            S3OriginConfig:
              OriginAccessIdentity: ""
        DefaultCacheBehavior:
          TargetOriginId: s3-site
          ViewerProtocolPolicy: redirect-to-https
          Compress: true
          # AWS managed policy "CachingOptimized"
          CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6
          FunctionAssociations:
            - EventType: viewer-request
              FunctionARN: !GetAtt IndexRewriteFunction.FunctionARN

  SiteBucketPolicy:
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: !Ref SiteBucket
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Sid: AllowCloudFrontOACReadOnly
            Effect: Allow
            Principal:
              Service: cloudfront.amazonaws.com
            Action: s3:GetObject
            Resource: !Sub "${SiteBucket.Arn}/*"
            Condition:
              StringEquals:
                AWS:SourceArn: !Sub >-
                  arn:aws:cloudfront::${AWS::AccountId}:distribution/${Distribution}

Outputs:
  BucketName:
    Value: !Ref SiteBucket
  DistributionId:
    Value: !Ref Distribution
  DistributionDomain:
    Value: !GetAtt Distribution.DomainName

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

  1. SiteBucketPublicAccessBlockConfiguration: ... true

    Block Public Access setting လေးခုလုံး ဖွင့်ထားပါတယ်။ Bucket ဟာ internet ကနေ ဘယ်တော့မှ တိုက်ရိုက် မရောက်နိုင်တော့ပါဘူး — CloudFront ကနေပဲ ရောက်ပါမယ်။

  2. SiteOACSigningBehavior: always / SigningProtocol: sigv4

    CloudFront ကို S3 ဆီ ပို့တဲ့ request တိုင်းကို SigV4 နဲ့ sign ခိုင်းပါတယ်။ Origin ရဲ့ OriginAccessControlId နဲ့ ချိတ်ထားပြီး OriginAccessIdentity ကို empty string ထားရပါတယ် (OAI အဟောင်း မသုံးဘူးလို့ ဆိုလိုတာပါ)။

  3. IndexRewriteFunctionRuntime: cloudfront-js-2.0

    /docs/ လို URL ကို /docs/index.html ပြောင်းပေးတဲ့ CloudFront Function ပါ။ Viewer request မှာ run လို့ cache lookup မတိုင်ခင် URI ပြောင်းပြီးသား ဖြစ်နေပါတယ်။ S3 website endpoint က အလိုအလျောက် လုပ်ပေးခဲ့တဲ့ အလုပ်ကို REST endpoint + OAC နဲ့ ဒီလို ပြန်လုပ်ပေးရပါတယ်။

  4. aws-common-rulesOverrideAction: Count: {}

    AWSManagedRulesCommonRuleSet ကို count mode နဲ့ စထားပါတယ်။ Rule group က ဘာကို block မယ်ဆိုတာ မှတ်တမ်းတင်ရုံပဲ လုပ်ပြီး ဘာမှ မတားသေးပါဘူး။ False positive မရှိမှန်း သေချာမှ None ပြောင်းပြီး block စလုပ်ပါ။

  5. rate-limit-per-ipRateBasedStatement: Limit / AggregateKeyType: IP

    IP address တစ်ခုချင်းစီရဲ့ request ကို (default) ငါးမိနစ် window အတွင်း ရေတွက်ပြီး RateLimit ကျော်ရင် block လုပ်ပါတယ်။ Request နှုန်း ပြန်ကျသွားရင် အလိုအလျောက် ပြန်ခွင့်ပြုပါတယ်။

  6. DistributionWebACLId: !GetAtt EdgeWebAcl.Arn

    WAFv2 web ACL ကို ARN နဲ့ ချိတ်ပါတယ်။ CachePolicyId က AWS managed 'CachingOptimized' policy ဖြစ်ပြီး header, cookie, query string တွေကို cache key ထဲ မထည့်ဘဲ gzip/brotli ကိုတော့ support လုပ်ပါတယ်။

  7. SiteBucketPolicyAWS:SourceArn: arn:aws:cloudfront::...:distribution/...

    Syntax section က bucket policy ပုံစံအတိုင်းပါပဲ — ဒီတစ်ခါတော့ account ID နဲ့ distribution ID ကို !Sub နဲ့ ဖြည့်ထားပါတယ်။

You should see
Sample output (ID နဲ့ domain တွေ ကွဲပြားပါမယ်):

$ aws cloudformation deploy --template-file edge-site.yaml \
    --stack-name edge-site --region us-east-1
Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - edge-site

$ curl -sI https://d1a2b3c4d5e6f7.cloudfront.net/ | grep -i x-cache
x-cache: Miss from cloudfront
$ curl -sI https://d1a2b3c4d5e6f7.cloudfront.net/ | grep -i x-cache
x-cache: Hit from cloudfront
Edge မှာ request တစ်ခု ဘယ်လို ဆုံးဖြတ်ခံရလဲ
  1. Viewer sends HTTPS request
  2. WAF rules run in priority order
  3. Viewer-request function runs
  4. Cache key lookup at the edge
  5. Miss: regional cache, then origin

WAF rule က Block

403 Forbidden from the edge

Cache hit

Served from edge, origin untouched

Cache miss

Fetched via OAC, cached for TTL

TTL, invalidation နဲ့ version ပါတဲ့ file name

Object တစ်ခု edge မှာ ဘယ်လောက်ကြာ နေမလဲဆိုတာ origin ပို့တဲ့ Cache-Control header နဲ့ cache policy ရဲ့ minimum/maximum TTL ပေါင်းပြီး ဆုံးဖြတ်ပါတယ်။ Origin က header မပို့ရင် default TTL ကို သုံးပါတယ်။ Header ပို့ရင်တော့ min နဲ့ max ကြားမှာ ညှပ်ပြီး သုံးပါတယ်။

ဖိုင်အသစ် deploy လုပ်တိုင်း cache ကို ဖျက်ဖို့ invalidation သုံးလို့ ရပေမယ့် တစ်လကို ပထမ path 1,000 ပဲ အခမဲ့ဖြစ်ပြီး ကျော်ရင် path တစ်ခုချင်း ငွေကျပါတယ် (/images/* လို wildcard က path တစ်ခုလို့ ရေတွက်ပါတယ်)။ ပိုကောင်းတဲ့ နည်းကတော့ build tool က app.3f9c2b.js လို content hash ပါတဲ့ file name ထုတ်ပေးတာပါ။ အဲဒီ file တွေကို တစ်နှစ်လောက် cache လုပ်ပြီး index.html တစ်ခုတည်းကိုပဲ TTL တိုတို ထားလိုက်ရင် invalidation မလိုတော့ပါဘူး။

invalidate.sh
DIST_ID=E2QWRUHAPOMQZL
# Purge only the entry point; hashed assets never change in place
aws cloudfront create-invalidation --distribution-id "$DIST_ID" \
  --paths "/index.html" "/"

# Wait until the edges have dropped the old copies
INV_ID=$(aws cloudfront list-invalidations --distribution-id "$DIST_ID" \
  --query "InvalidationList.Items[0].Id" --output text)
aws cloudfront wait invalidation-completed --distribution-id "$DIST_ID" --id "$INV_ID"
File အမျိုးအစားCache နည်းဗျူဟာ
app.3f9c2b.js, style.a81e.cssTTL ရှည် (တစ်နှစ်)၊ invalidation မလို
index.htmlTTL တို (ဥပမာ ၆၀ စက္ကန့်) သို့မဟုတ် deploy တိုင်း invalidate
/api/* responseManaged CachingDisabled policy

CloudFront Functions နဲ့ Lambda@Edge

Edge မှာ code run ဖို့ နည်းနှစ်ခု ရှိပါတယ်။ CloudFront Functions က edge location တိုင်းမှာ run ပြီး JavaScript အသေးလေးတွေအတွက် ရည်ရွယ်ပါတယ် — header ထည့်တာ၊ URL rewrite/redirect၊ cache key normalize လုပ်တာ၊ token စစ်တာမျိုး။ Network call မလုပ်နိုင်ဘဲ execution time အရမ်းတိုအောင် ကန့်သတ်ထားပါတယ်။ Lambda@Edge ကတော့ regional edge cache တွေမှာ run တဲ့ Node.js/Python Lambda function ဖြစ်ပြီး network ခေါ်လို့ရ၊ body ဖတ်လို့ရ၊ ပိုကြာကြာ run လို့ရပေမယ့် invocation တစ်ခုချင်း ဈေးပိုကြီးပြီး latency ပိုများပါတယ်။

စည်းမျဉ်း ရိုးရိုးလေးကတော့ — CloudFront Functions နဲ့ စပါ။ Network call, request body, ဒါမှမဟုတ် origin request/response event လိုမှ Lambda@Edge ကို ရွေးပါ။

CloudFront FunctionsLambda@Edge
Run တဲ့နေရာEdge location တိုင်းRegional edge cache
Event များViewer request/response ပဲViewer + origin request/response
RuntimeJavaScript (cloudfront-js)Node.js, Python
Network / bodyမရရ
သုံးသင့်တဲ့နေရာHeader, redirect, rewriteAuth call, image resize, A/B logic

WAF ကို ဘေးကင်းစွာ rollout လုပ်ခြင်း — count, ကြည့်, ပြီးမှ block

Count mode နဲ့ deploy

Managed rule group တွေကို OverrideAction: Count နဲ့ ထည့်ပါ။ Traffic အစစ်ပေါ်မှာ ဘာကို block မလဲ မှတ်တမ်းတင်ပေမယ့် ဘယ်သူ့ကိုမှ မတားသေးပါဘူး။

အနည်းဆုံး ရက်အနည်းငယ် ကြည့်ပါ

CloudWatch metric နဲ့ sampled request (ဒါမှမဟုတ် WAF log) ကို ကြည့်ပြီး ဘယ် rule က match ဖြစ်လဲ စစ်ပါ။ ဥပမာ upload endpoint ရဲ့ request body ကြီးကြီးတွေက SizeRestrictions_BODY rule နဲ့ match တတ်ပါတယ်။

False positive ကို scope-down လုပ်ပါ

Rule group ထဲက rule တစ်ခုချင်းကို RuleActionOverrides နဲ့ Count မှာပဲ ထားခဲ့ပါ၊ ဒါမှမဟုတ် ScopeDownStatement နဲ့ path တချို့ကို ဖယ်ပါ။

Block ပြောင်းပြီး ဆက်စောင့်ကြည့်ပါ

OverrideAction ကို None ပြောင်းပြီး BlockedRequests metric ပေါ်မှာ CloudWatch alarm တပ်ပါ။ ရုတ်တရက် တက်လာရင် attack ဖြစ်နိုင်သလို deploy အသစ်က false positive လည်း ဖြစ်နိုင်ပါတယ်။

waf-observe.sh
# CLOUDFRONT-scope web ACLs live in us-east-1
aws wafv2 list-web-acls --scope CLOUDFRONT --region us-east-1 \
  --query "WebACLs[].{Name:Name,Arn:ARN}" --output table

# Which requests matched the common rule group in the last hour?
ACL_ARN=$(aws wafv2 list-web-acls --scope CLOUDFRONT --region us-east-1 \
  --query "WebACLs[?Name=='edge-site-edge-acl'].ARN" --output text)
aws wafv2 get-sampled-requests --region us-east-1 --scope CLOUDFRONT \
  --web-acl-arn "$ACL_ARN" --rule-metric-name aws-common-rules \
  --time-window StartTime=$(date -u -d '-1 hour' +%s),EndTime=$(date -u +%s) \
  --max-items 50

Shield Standard နဲ့ Shield Advanced

Shield Standard ကို ဘာမှ ဖွင့်စရာ မလိုပါဘူး — AWS customer တိုင်းမှာ အပိုကြေးမရှိဘဲ အလိုအလျောက် ပါပြီးသားပါ။ CloudFront နဲ့ Route 53 နောက်မှာ ထားလိုက်တာနဲ့ edge network ကြီးတစ်ခုလုံးက layer 3/4 flood တွေကို စုပ်ယူပေးပါတယ်။

Shield Advanced ကတော့ organization အလိုက် လစဉ် subscription ကြေး (တစ်နှစ် commitment ပါ၊ ပမာဏအတိအကျကို pricing page မှာ စစ်ပါ) ပေးရတဲ့ service ပါ။ ဒါပေမယ့် တစ်ချို့ application တွေအတွက်တော့ တန်ပါတယ် — attack ကာလအတွင်း Shield Response Team (SRT) ရဲ့ အကူအညီ၊ DDoS ကြောင့် scale-out ဖြစ်လို့ တက်လာတဲ့ ကုန်ကျစရိတ်အတွက် cost protection၊ health-based detection၊ protect လုပ်ထားတဲ့ resource တွေအတွက် WAF ကုန်ကျစရိတ် တချို့ ပါဝင်တာ စတာတွေ ရပါတယ်။

Shield StandardShield Advanced
ကုန်ကျစရိတ်အခမဲ့၊ အမြဲဖွင့်လစဉ် subscription + commitment
ကာကွယ်မှုLayer 3/4 ပဲLayer 3/4/7 + health-based detection
လူ့အကူအညီမရှိShield Response Team (SRT)
Bill ကာကွယ်မှုမရှိDDoS scaling charge အတွက် cost protection

Production မှာ

Production မှာ team တွေ တကယ်လုပ်တာ

ပုံမှန် e-commerce site တစ်ခုမှာ distribution တစ်ခုထဲ behavior သုံးခုလောက် ရှိပါတယ် — /assets/* က S3 + OAC ကို TTL ရှည်နဲ့၊ /api/* က ALB ကို managed CachingDisabled + AllViewerExceptHostHeader policy နဲ့၊ default behavior က index.html ကို TTL တိုတိုနဲ့ပါ။ ALB ကို CloudFront ကနေပဲ ရောက်အောင် security group ကို CloudFront ရဲ့ origin-facing managed prefix list နဲ့ ကန့်သတ်ပြီး CloudFront က ထည့်ပေးတဲ့ secret custom header ကို ALB rule မှာ စစ်လေ့ ရှိပါတယ်။

WAF မှာ AWSManagedRulesCommonRuleSet, KnownBadInputs, AmazonIpReputationList နဲ့ login endpoint အတွက် သီးသန့် rate-based rule ကို အများဆုံး တွေ့ရပါတယ်။ WAF log ကို S3 ဒါမှမဟုတ် CloudWatch Logs ဆီ ပို့ပြီး Athena နဲ့ query လုပ်ပါတယ်။ Web ACL ကိုယ်တိုင်ကိုလည်း template ထဲမှာ ထားပြီး code review နဲ့ ပြောင်းလဲပါတယ် — console ကနေ ညသန်းခေါင် 'ခဏ' block rule ထည့်တာက နောက်ပိုင်း outage ရဲ့ အကြောင်းရင်း ဖြစ်တတ်လို့ပါ။

Cache hit ratio ကို metric အဖြစ် စောင့်ကြည့်ပါ

Distribution ရဲ့ additional metrics ဖွင့်ထားရင် CacheHitRate ကို CloudWatch မှာ မြင်ရပါတယ်။ Static asset တွေအတွက် ရာခိုင်နှုန်း ၉၀ ကျော် ရှိသင့်ပြီး ရုတ်တရက် ကျသွားရင် cache key ထဲ မလိုအပ်တဲ့ value ဝင်လာတာ ဖြစ်တတ်ပါတယ်။

အမှားများ

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

  • Header, cookie အကုန်လုံးကို cache key ထဲ ထည့်ခြင်း — User-Agent ဒါမှမဟုတ် session cookie ကို cache key ထဲ ထည့်လိုက်ရင် user တိုင်းက ကိုယ်ပိုင် cache entry ရသွားပြီး hit ratio က သုညနားကပ်သွားပါတယ်။ Origin လိုအပ်ရုံ header ဆိုရင် origin request policy နဲ့ ပို့ပါ၊ cache key ထဲ မထည့်ပါနဲ့။
  • Managed rule group ကို တန်းပြီး Block mode နဲ့ ဖွင့်ခြင်း — CommonRuleSet ထဲမှာ request body အရွယ်အစား ကန့်သတ်တဲ့ rule ပါလို့ file upload တွေ၊ JSON body ကြီးတဲ့ API တွေ ချက်ချင်း 403 ပြန်ပါတယ်။ Count mode နဲ့ အရင် ကြည့်ပါ။
  • Web ACL ကို region မှားပြီး ဖန်တီးခြင်း — CloudFront အတွက် web ACL ဟာ Scope CLOUDFRONT နဲ့ us-east-1 မှာ ဖြစ်ရပါတယ်။ ap-southeast-1 မှာ REGIONAL scope နဲ့ ဖန်တီးထားရင် distribution နဲ့ ချိတ်လို့ မရပါဘူး။
  • S3 website endpoint ကို origin အဖြစ် ထားပြီး OAC သုံးဖို့ ကြိုးစားခြင်း — OAC က S3 REST endpoint (bucket.s3.region.amazonaws.com) နဲ့ပဲ အလုပ်လုပ်ပါတယ်။ Website endpoint က public bucket ကို လိုအပ်နေဆဲပါ။
  • Deploy တိုင်း /* ကို invalidate လုပ်ခြင်း — cache တစ်ခုလုံး ဗလာဖြစ်ပြီး origin ဆီ traffic အလုံးအရင်း ဝင်လာပါတယ်။ Content hash ပါတဲ့ file name သုံးပါ။

Best Practices

Best practices

  • Origin တိုင်းကို private ထားပါ — S3 အတွက် OAC၊ ALB အတွက် prefix list + secret header။
  • Cache key ကို အသေးဆုံး ထားပြီး origin လိုတာကို origin request policy နဲ့ ပို့ပါ။
  • WAF ပြောင်းလဲမှုတိုင်းကို Count mode ကနေ စပြီး template ထဲမှာ version control လုပ်ပါ။
  • Login, OTP, search လို ဈေးကြီးတဲ့ endpoint တွေအတွက် သီးသန့် rate-based rule ထားပါ။

💰 Cost & Cleanup

CloudFront က internet ဆီ ထွက်တဲ့ data transfer (GB အလိုက်) နဲ့ request အရေအတွက် (HTTPS က နည်းနည်း ပိုဈေးကြီး) နဲ့ ငွေတောင်းပါတယ်။ CloudFront မှာ always-free tier ရှိလို့ (လစဉ် data transfer နဲ့ request ပမာဏ တစ်ခုအထိ) ဒီ lab လောက်ကတော့ ပုံမှန်အားဖြင့် အခမဲ့ အတွင်းမှာပါ။ AWS WAF ကတော့ free tier မရှိပါဘူး — web ACL တစ်ခုချင်း လစဉ်ကြေး၊ rule တစ်ခုချင်း လစဉ်ကြေး၊ request သန်းချီ အလိုက် ကြေး ပေးရပါတယ်။ Lab ပြီးတာနဲ့ stack ကို ဖျက်ပါ။ Shield Advanced ကိုတော့ lab အတွက် ဘယ်တော့မှ subscribe မလုပ်ပါနဲ့ — တစ်နှစ် commitment ပါပါတယ်။

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

bash
# The bucket must be empty before CloudFormation can delete it
BUCKET=$(aws cloudformation describe-stacks --stack-name edge-site --region us-east-1 \
  --query "Stacks[0].Outputs[?OutputKey=='BucketName'].OutputValue" --output text)
aws s3 rm "s3://$BUCKET" --recursive
aws cloudformation delete-stack --stack-name edge-site --region us-east-1
# Disabling and deleting a distribution can take several minutes
aws cloudformation wait stack-delete-complete --stack-name edge-site --region us-east-1

Mini challenge

Hash ပါတဲ့ asset တွေအတွက် custom cache policy

Content hash ပါတဲ့ /assets/* file တွေအတွက် သီးသန့် cache policy တစ်ခုကို CloudFormation template အသေးလေး (assets-cache-policy.yaml) နဲ့ ဖန်တီးပြီး၊ နောက်ပိုင်း distribution ရဲ့ CacheBehaviors ထဲမှာ ပြန်သုံးနိုင်အောင် ID ကို export လုပ်ပါ။

  • MinTTL 0၊ DefaultTTL တစ်ရက် (86400)၊ MaxTTL တစ်နှစ် (31536000)
  • Cookie, header, query string တစ်ခုမှ cache key ထဲ မပါရ
  • Gzip နဲ့ Brotli compression ကို ဖွင့်ထားရမယ်
  • Policy ID ကို stack name ပါတဲ့ export name နဲ့ Output အဖြစ် ထုတ်ပေးရမယ်

ရလဒ်: cfn-lint ကို error မရှိဘဲ ဖြတ်ပြီး deploy လုပ်ပြီးနောက် aws cloudformation list-exports မှာ policy ID ပေါ်လာပါမယ်။ /assets/* behavior ထဲမှာ အဲဒီ ID ကို !ImportValue နဲ့ ပြန်ခေါ်လို့ ရပါတယ်။

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

AWS::CloudFront::CachePolicy resource ကို သုံးပါ။ ParametersInCacheKeyAndForwardedToOrigin ထဲမှာ behavior သုံးခုလုံးကို none ထားပါ။ !Ref က cache policy ID ကို ပြန်ပေးပါတယ်။

အဖြေ ကြည့်ရန်
assets-cache-policy.yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: Long-lived cache policy for content-hashed static assets

Resources:
  AssetsCachePolicy:
    Type: AWS::CloudFront::CachePolicy
    Properties:
      CachePolicyConfig:
        Name: !Sub "${AWS::StackName}-hashed-assets"
        Comment: Long TTL for file names that change when their content changes
        MinTTL: 0
        DefaultTTL: 86400
        MaxTTL: 31536000
        ParametersInCacheKeyAndForwardedToOrigin:
          EnableAcceptEncodingGzip: true
          EnableAcceptEncodingBrotli: true
          CookiesConfig:
            CookieBehavior: none
          HeadersConfig:
            HeaderBehavior: none
          QueryStringsConfig:
            QueryStringBehavior: none

Outputs:
  AssetsCachePolicyId:
    Value: !Ref AssetsCachePolicy
    Export:
      Name: !Sub "${AWS::StackName}-AssetsCachePolicyId"

Quick Quiz 1 / 3

Response တိုင်းမှာ security header (Strict-Transport-Security စသဖြင့်) ထည့်ချင်ပါတယ်။ Network call မလိုပါဘူး။ ဘာက အသင့်တော်ဆုံးလဲ?

Quick Quiz 2 / 3

AWSManagedRulesCommonRuleSet ကို production ထဲ ပထမဆုံး ထည့်တဲ့အခါ ဘာကြောင့် Count mode နဲ့ စသင့်တာလဲ?

Quick Quiz 3 / 3

OAC bucket policy မှာ AWS:SourceArn condition ကို ဖယ်လိုက်ရင် ဘာဖြစ်မလဲ?

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

  • Cache policy က cache key နဲ့ TTL ကို ဆုံးဖြတ်ပြီး origin request policy က cache မခွဲဘဲ origin ဆီ ပို့မယ့်ဟာကို ဆုံးဖြတ်ပါတယ်။
  • OAC + AWS:SourceArn condition နဲ့ S3 bucket ကို လုံးဝ private ထားရင်း CloudFront ကနေ serve လုပ်ပါ။
  • Content hash ပါတဲ့ file name + TTL ရှည်က deploy တိုင်း invalidation လုပ်တာထက် ပိုကောင်းပါတယ်။
  • CloudFront Functions နဲ့ စပါ၊ network/body/origin event လိုမှ Lambda@Edge ကို သုံးပါ။
  • WAF ကို us-east-1 မှာ CLOUDFRONT scope နဲ့ ဖန်တီး၊ Count နဲ့ စ၊ ပြီးမှ Block ပြောင်းပါ။ Shield Standard က အခမဲ့ အမြဲပါပြီး Advanced က လိုအပ်မှ ယူပါ။

Amazon CloudFront Developer Guide — AWS Documentation

Restricting access to an Amazon S3 origin (OAC) — AWS Documentation

AWS WAF, AWS Firewall Manager, and AWS Shield Advanced Developer Guide — AWS Documentation

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

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

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

CloudFront, AWS WAF နဲ့ Shield — Edge မှာ ကာကွယ်ပြီး မြန်အောင်လုပ်ခြင်း | Thuta Learning