七月有個客戶要求整套系統部署在他們自己的 AWS 帳號和 VPC 裡,而且他們的 IT 只接受 CloudFormation,不收 Terraform。

在我們自己的環境,資料庫和 MQ 都是在 Kubernetes 裡用 Helm 跑的。這次要全部換成 AWS 的託管服務。

拆成幾層

一個超大的 template 很難維護,我拆成幾層,各自部署,中間用 SSM Parameter Store 傳值:

foundation   ── VPC 參數、子網路、Security Group、KMS key
  └─ eks     ── EKS cluster、node group、IRSA
  └─ data    ── DocumentDB、Amazon MQ(RabbitMQ)、ElastiCache(Redis)
  └─ efs     ── 共用檔案系統
  └─ app     ── ACM 憑證、External Secrets 的 IAM role

沒用 CloudFormation 的 Export / ImportValue,是因為它會把 stack 綁死,被引用的值就改不了。SSM 參數鬆一點:

Parameters:
  VpcId:
    Type: AWS::SSM::Parameter::Value<String>
    Default: /platform/network/vpc-id

客戶 VPC 的限制

VPC 和子網路是客戶建的,我們不能動,只能當參數傳進來。有些客戶要求用他們自己的 KMS key 加密 EBS 和 Secrets Manager。EKS 的 node AMI、Service CIDR 也可能有規定。所以 template 裡幾乎沒有寫死的值,全部參數化。

託管資料服務

DocDBCluster:
  Type: AWS::DocDB::DBCluster
  Properties:
    StorageEncrypted: true
    KmsKeyId: !Ref DataKmsKeyArn
    EnableCloudwatchLogsExports: [audit]
    DBSubnetGroupName: !Ref DocDBSubnetGroup
    VpcSecurityGroupIds: [!Ref DataSecurityGroup]

endpoint、port 這些連線資訊寫進 Secrets Manager,再用 External Secrets Operator 同步成 Kubernetes Secret,應用程式從環境變數讀。Helm values 裡不會出現任何密碼。

apiVersion: external-secrets.io/v1beta1
kind: ExternalSecret
metadata:
  name: datastore
spec:
  secretStoreRef:
    name: aws-secrets-manager
    kind: ClusterSecretStore
  target:
    name: datastore
  dataFrom:
    - extract:
        key: platform/datastore

合規開關

稽核會問到的項目,我都做成參數,預設打開:EKS audit log、DocumentDB audit log、ALB access log、NLB deletion protection,還有所有儲存加密。

CI/CD 跑在客戶的 Jenkins

客戶不讓外部的 CI 直接碰他們的環境,所以部署流程放在客戶的 Jenkins 上跑。Jenkins 用自己的 Security Group / CIDR 存取 EKS API,不給它 admin 權限的 IAM user。

在自己家的環境,想開什麼資源就開。到了客戶環境,每個資源都得先問清楚是誰建的、誰能改、稽核怎麼查。這次的分層和參數化,主要就是為了回答這幾個問題。