System Manager Inventory 是 AWS re’invnet 2019 釋出的新功能,主要是用於收集所有 EC2 的系統資訊,這是企業管理中很常見的一環,以前要處理這些事情都必須透過 cron 把系統資訊丟出來非常麻煩,尤其是當 EC2 一多的時候幾乎是一個不受控的狀態。
這篇文章的需求除了收集 EC2 Inventory 以外,還希望可以 Cross-Account, Region 並且統一收集到一個 S3 bucket 做保存,除此之外也要能透過 QuickSight 隨時查看整體資訊,架構圖大概會像這樣:
![](https://i0.wp.com/i.imgur.com/zsEDBXG.png?w=1200&ssl=1)
Environments
- Account
- Central Account (Organizations):作為集中 Inventory 存放 S3 bucket 的帳號,在這個範例是以 Organizations Account 為 Central Account
- Linked Account:一或多個 Account 將 Inventory 丟到 Central Account 統一管理
建立 Central Account Inventory
第一次設定 Inventory 必須要 Setup Inventory
System Manager -> Inventory -> Setup Inventory
![](https://i0.wp.com/i.imgur.com/n4NCcSY.png?w=1200&ssl=1)
Setup Inventory 幾個參數解釋
- Targets:選擇要收 Inventory 的 instance,可選 Tags, All 或者手動
- Schedule:預設是每 30 分鐘收集一次,但個人覺得 24 hours 收集一次就好,畢竟執行次數多也是浪費 SSM Agent 的運算資源。
- Parameters:要收集的資訊類別,基本上採預設全收就好,都是用得到的資料,如果內建提供的 Inventory 不符需求也能 custom inventory
- Sync inventory execution logs S3 bucket:顧名思義保存執行的記錄到 S3
Setup Inventory 完成後就可以看到 EC2 開始將資訊拋到 Inventory
![](https://i0.wp.com/i.imgur.com/4kXpe0Y.png?w=1200&ssl=1)
建立 Central Account Inventory Detailed View
Inventory 預設只能在 Account/Region 內運作,若要做到 Cross-Account, Region 則要使用 Detailed View 的功能集成 S3, Glue, Athena 服務
Detailed View 的運作方法是:
- 建立 Resource Data Syncs 將 Inventory 丟到 S3 bucket (可選 KMS 加密)
- Glue Crawlers 每 12 小時將存在 S3 bucket 的 Inventory 同步 Data catalog (automatically create database and tables)
- Athena 查詢 Glue Data catalog
使用 Detailed View 會有 Glue, S3, Athena 的費用產生,但費用相對不高,如果不需要這麼即時查詢,建議可以將 Glue Crawlers 的時間拉長。
建立 Detailed View
先建立 S3 bucket (central-ssm-resources-data-1234567890) 作為 Inventory 儲存,而 Bucket Policy 參照以下設定:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "SSMBucketPermissionsCheck",
"Effect": "Allow",
"Principal": {
"Service": "ssm.amazonaws.com"
},
"Action": "s3:GetBucketAcl",
"Resource": "arn:aws:s3:::central-ssm-resources-data-1234567890"
},
{
"Sid": " SSMBucketOrgDelivery",
"Effect": "Allow",
"Principal": {
"Service": "ssm.amazonaws.com"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::central-ssm-resources-data-1234567890/*/accountid=*/*",
"Condition": {
"StringEquals": {
"s3:x-amz-acl": "bucket-owner-full-control"
}
}
},
{
"Sid": " SSMBucketDelivery",
"Effect": "Allow",
"Principal": {
"Service": "ssm.amazonaws.com"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::central-ssm-resources-data-1234567890/*/accountid=*/*",
"Condition": {
"StringEquals": {
"s3:RequestObjectTag/OrgId": "o-sh3qftx8ug",
"s3:x-amz-acl": "bucket-owner-full-control"
}
}
},
{
"Sid": " SSMBucketDeliveryTagging",
"Effect": "Allow",
"Principal": {
"Service": "ssm.amazonaws.com"
},
"Action": "s3:PutObjectTagging",
"Resource": "arn:aws:s3:::central-ssm-resources-data-1234567890/*/accountid=*/*"
}
]
}
- 修改上述 Resource 定義的 S3 bucket ARN
SSMBucketDelivery
中的s3:RequestObjectTag/OrgId
代表在這個 Organizations 底下都有s3:PutObject
權限SSMBucketOrgDelivery
這條是給 Organizations Account 的 Inventory 用的,如果 Organizations 沒有 instance 則可以不用
建立 Resource Data Syncs 將上述 S3 bucket 資訊填入
![](https://i0.wp.com/i.imgur.com/IqBvHXi.png?w=1200&ssl=1)
建立大約要等幾分鐘 Glue Crawlers 完成後 Detailed View 才會有資料,或是到 Glue Crawlers 確認 Parsing 狀態。
完成後的 Detailed View 可以查到在 S3 的 Inventory 資料
![](https://i0.wp.com/i.imgur.com/ndI6RO7.png?w=1200&ssl=1)
建立 Linked Account Inventory 與 Resource Data Syncs
由於所有分析的來源都是從 Central Account 的 S3 bucket,同理也能將 Linked Account 的 Inventory 透過 Resource Data Syncs 將資料丟到 Central Account 的 S3 bucket (central-ssm-resources-data-1234567890)
Linked Account 的數量也許很多,建議使用 CloudFormation StackSet 同步設定 Resource Data Syncs 可以參考 shazi7804/aws-cloudformation-ssm-Inventory-resource-data-syncs。
![](https://i0.wp.com/i.imgur.com/y8WDqzA.png?w=1200&ssl=1)
部署 CloudFormation StackSet 後可以看到多個帳號已經建立好 Resource Data Syncs,這時 Central Account 的 S3 bucket 就會陸續收到 Linked Account 的 Inventory 資訊
同理 Detailed View 也應該要能查到 Linked Account 的 EC2 Inventory
![](https://i0.wp.com/i.imgur.com/n3O6xUk.png?w=1200&ssl=1)
Inventory with QuickSight
除了即時查詢以外,圖像化也很重要,透過 QuickSight 從 Data catalog 將資訊顯示:
設定 QuickSight Datasets
將 Inventory Table 串起來,Inventory schema 可以拿 region
, accountid
, resourceid
這幾個欄位作為 mapping
![](https://i0.wp.com/i.imgur.com/PAdyWIx.png?w=1200&ssl=1)
再到 Analyses 將 Visuals 拉出來呈現到 Dashboard 上
![](https://i0.wp.com/i.imgur.com/mPk6ZCk.png?w=1200&ssl=1)
接下來只要有任何的 EC2 Launch 都會記錄在 S3 / QuickSight 上,即便被 Terminal 掉也會留下 Inventory 紀錄。