Study Notes - CloudFormation Stack


繼續整理 CloudFormation Stack 的筆記。主要包含 Stack 工作流程、Stack Status、Update Stack。


CloudFormation Stacks

Stack 是根據 Template 的邏輯資源 (Logical Resources) 描述,所建立出來的實體資源 (Physical Resources)。

用 OOP 來講,Template 就是一推 Class 的集合,然後 Stack 就是 new 出來的 Instances 集合,可以 new 各式各樣的需求,像是開發環境、測試環境、效能測試環境、正式環境等。

Stack 的增刪查改 (CRUD) 都可以在 AWS Console 建立,也可以透過 CLI 建立,以下是 CLI 的例子:

1
2
3
4
5
6
7
aws cloudformation create-stack \
--stack-name myteststack \
--template-body file:///mytemplate.json \
--parameters ParameterKey=Parm1,ParameterValue=test1 ParameterKey=Parm2,ParameterValue=test2
{
"StackId" : "arn:aws:cloudformation:us-west-2:123456789012:stack/myteststack/330b0120-1771-11e4-af37-50ba1b98bea6"
}

找出狀態是 CREATE_COMPLETE 的 Stacks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
aws cloudformation list-stacks \
--stack-status-filter CREATE_COMPLETE
[
{
"StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/myteststack/
644df8e0-0dff-11e3-8e2f-5088487c4896",
"TemplateDescription": "AWS CloudFormation Sample Template S3_Bucket: Sample template showing how to create a publicly accessible S3 bucket. **WARNING** This template creates an
S3 bucket. You will be billed for the AWS resources used if you create a stack from this template.",
"StackStatusReason": null,
"CreationTime": "2013-08-26T03:27:10.190Z",
"StackName": "myteststack",
"StackStatus": "CREATE_COMPLETE"
}
]

以下是其他描述 Stack 相關資訊的 CLI:

1
2
3
4
5
6
7
8
9
10
11
12
# Stack 的狀態
aws cloudformation describe-stacks --stack-name myteststack
# Stack 的 events (歷史):
aws cloudformation describe-stack-events --stack-name myteststack
# Stack 資源的列表
aws cloudformation list-stack-resources --stack-name myteststack
aws cloudformation get-template --stack-name myteststack
# 驗證 Template
aws cloudformation validate-template --template-url <s3_url>
aws cloudformation validate-template --template-body <local_path>
# 刪除 Stack
aws cloudformation delete-stack --stack-name myteststack

建立 Stack 的工作流程

建立 Stack 需要先準備好 Template,可以使用官方提供的 Sample Templates,或者自行建立,工作流程如下圖 (取自官方文件):

Stack Status Codes

CloudFormation Stack 的增刪查改會有以下 Status Codes:

  • CREATE_COMPLETE: 建立完成 Stack(s)
  • CREATE_IN_PROGRESS: 正在建立 Stack(s)
  • CREATE_FAILED: 建立 Stack(s) 失敗, 可能是權限、或者是建立資源時 timeout
  • DELETE_COMPLETE: 成功刪除 stack(s)
  • DELETE_FAILED: 刪除 stack(s) 失敗,可能還有一些資源還在運行。可以再刪除一次,或者從 stack event 查看哪一些資源刪除失敗。
  • DELETE_IN_PROGRESS: 正在刪除 Stack(s)
  • REVIEW_IN_PROGRESS: review 正在建立的 stack(s),但是只會產生 StackId, 並沒有真的建立相關資源。
  • ROLLBACK_COMPLETE: 建立 Stack 失敗後,或者建立過程中被取消的 Stack,成功還原 (Rollback)
  • ROLLBACK_FAILED: 建立 Stack 失敗後,或者建立過程中被取消的 Stack,還原失敗
  • ROLLBACK_IN_PROGRESS: 建立 Stack 失敗後,或者建立過程中被取消的 Stack,正在還原
  • UPDATE_COMPLETE: 成功更新 stacks
  • UPDATE_COMPLETE_CLEANUP_IN_PROGRESS: 成功更新 Stack 之後,正在清理舊的資源。
  • UPDATE_IN_PROGRESS: 正在更新 Stack
  • UPDATE_ROLLBACK_COMPLETE: 更新 stack 失敗之後,順利還原
  • UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS: 更新 stack 失敗之後,正在清理新建立的資源。
  • UPDATE_ROLLBACK_FAILED: 更新 stack 失敗之後,還原失敗。可以刪除整個 stack 或者 call support
  • UPDATE_ROLLBACK_IN_PROGRESS: 更新 stack 失敗之後,正在還原

因為 官方文件 並沒有狀態圖,下圖是我根據文件描述的狀態整理的 CloudFormation Status Flow:


Stack Update: StackSet

更新 Stack 的模式有兩種: direct update or creating and executing change sets. 流程如下 (圖取自官方文件):

  • direct update: 直接更新既有 stack 相關的資源
  • ChangeSets: 可以 preview CloudFormation 將會更新哪一些資源,再決定是否執行更新
    • 更新的差異會列出哪一些新增、哪一些刪除

結論

Stack 是 CloudFomration 執行 Template 的產出物,用物件導向來思考:Template = ClassStack = Instance。有了 Template、Stack,了解了生命週期,接下來整理的開發 Template 過程中,如何有效的 Debug Template 的經驗分享


延伸閱讀

系列文章

延伸閱讀 (站內)

參考資料

更新紀錄

  • 2017/03/31: 初版
  • 2018/12/22: 調整排版


Comments

  • 全站索引
  • 關於這裏
  • 關於作者
  • 學習法則
  • 思考本質
  • 一些領悟
  • 分類哲學
  • ▲ TOP ▲