透過 IaC 產生器掃描的資源建立 CloudFormation 範本 - AWS CloudFormation

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

透過 IaC 產生器掃描的資源建立 CloudFormation 範本

本主題說明如何從透過 IaC 產生器功能掃描的資源建立範本。

透過掃描的資源建立範本 (主控台)

透過掃描的資源建立堆疊範本
  1. 開啟 CloudFormation 主控台的 IaC 產生器頁面

  2. 在畫面頂端的導覽列上,選擇包含掃描資源的 AWS 區域。

  3. 範本區段,選擇建立範本

  4. 選擇從新範本開始

    1. 對於範本名稱,提供範本的名稱。

    2. (選用) 設定您的刪除政策和更新取代政策

    3. 選擇下一步,將掃描的資源新增至範本。

  5. 對於新增掃描的資源,瀏覽掃描的資源清單,並選取您要新增至範本的資源。您可以依據資源識別碼、資源類型或標籤篩選資源。這些篩選條件是相互包含的。

  6. 將所有需要的資源新增至範本後,選擇下一步以離開新增掃描的資源頁面,然後繼續前往新增相關資源頁面。

  7. 檢閱建議的相關資源清單。Amazon EC2 執行個體與安全群組等相關資源是相互依存的,通常歸屬於相同的工作負載。選取您要納入產生範本的相關資源。

    注意

    建議您將所有相關資源新增至此範本。

  8. 檢閱範本詳細資訊、掃描的資源及相關資源。

  9. 選擇建立範本,以離開檢閱和建立頁面,並建立範本。

透過掃描的資源建立範本 (AWS CLI)

透過掃描的資源建立堆疊範本
  1. 使用 list-resource-scan-resources 命令列出掃描期間找到的資源,並可選擇性指定限制輸出的 --resource-identifier 選項。對於 --resource-scan-id 選項,請將範例 ARN 取代為實際 ARN。

    aws cloudformation list-resource-scan-resources \ --resource-scan-id arn:aws:cloudformation:us-east-1:123456789012:resourceScan/0a699f15-489c-43ca-a3ef-3e6ecfa5da60 \ --resource-identifier MyApp

    下列是回應範例,其中 ManagedByStack 表示 CloudFormation 是否已管理該資源。複製輸出內容。下一個步驟需要此值。

    { "Resources": [ { "ResourceType": "AWS::EKS::Cluster", "ResourceIdentifier": { "ClusterName": "MyAppClusterName" }, "ManagedByStack": false }, { "ResourceType": "AWS::AutoScaling::AutoScalingGroup", "ResourceIdentifier": { "AutoScalingGroupName": "MyAppASGName" }, "ManagedByStack": false } ] }

    如需輸出中欄位的說明,請參閱《AWS CloudFormation API 參考》中的 ScannedResource

  2. 使用 cat 命令,將資源類型和識別碼儲存至主目錄中名為 resources.json 的 JSON 檔案。以下是基於上述步驟中範例命令的範例 JSON。

    $ cat > resources.json [ { "ResourceType": "AWS::EKS::Cluster", "ResourceIdentifier": { "ClusterName": "MyAppClusterName" } }, { "ResourceType": "AWS::AutoScaling::AutoScalingGroup", "ResourceIdentifier": { "AutoScalingGroupName": "MyAppASGName" } } ]
  3. 使用 list-resource-scan-related-resources 命令,搭配您建立的 resources.json 檔案,列出與您掃描資源相關的資源。

    aws cloudformation list-resource-scan-related-resources \ --resource-scan-id arn:aws:cloudformation:us-east-1:123456789012:resourceScan/0a699f15-489c-43ca-a3ef-3e6ecfa5da60 \ --resources file://resources.json

    下列是回應範例,其中 ManagedByStack 表示 CloudFormation 是否已管理該資源。將這些資源新增至您在上一步驟建立的 JSON 檔案。您建立範本時會需要用到它。

    { "RelatedResources": [ { "ResourceType": "AWS::EKS::Nodegroup", "ResourceIdentifier": { "NodegroupName": "MyAppNodegroupName" }, "ManagedByStack": false }, { "ResourceType": "AWS::IAM::Role", "ResourceIdentifier": { "RoleId": "arn:aws::iam::account-id:role/MyAppIAMRole" }, "ManagedByStack": false } ] }

    如需輸出中欄位的說明,請參閱《AWS CloudFormation API 參考》中的 ScannedResource

    注意

    輸入的資源清單長度不得超過 100。若要列出超過 100 個資源的相關資源,請以每批次 100 個的方式執行 list-resource-scan-related-resources 命令,並合併結果。

    請注意,輸出清單中可能包含重複的資源。

  4. 若要建立新的啟動範本,請按如下所示使用 create-generated-template 命令,且進行這些修改:

    • us-east-1 取代為包含掃描資源的 AWS 區域。

    • MyTemplate 取代為要建立的範本名稱。

    aws cloudformation create-generated-template --region us-east-1 \ --generated-template-name MyTemplate \ --resources file://resources.json

    以下是範例 resources.json 檔案。

    [ { "ResourceType": "AWS::EKS::Cluster", "LogicalResourceId":"MyCluster", "ResourceIdentifier": { "ClusterName": "MyAppClusterName" } }, { "ResourceType": "AWS::AutoScaling::AutoScalingGroup", "LogicalResourceId":"MyASG", "ResourceIdentifier": { "AutoScalingGroupName": "MyAppASGName" } }, { "ResourceType": "AWS::EKS::Nodegroup", "LogicalResourceId":"MyNodegroup", "ResourceIdentifier": { "NodegroupName": "MyAppNodegroupName" } }, { "ResourceType": "AWS::IAM::Role", "LogicalResourceId":"MyRole", "ResourceIdentifier": { "RoleId": "arn:aws::iam::account-id:role/MyAppIAMRole" } } ]

    如果成功,此命令會傳回下列內容:

    { "Arn": "arn:aws:cloudformation:region:account-id:generatedtemplate/7fc8512c-d8cb-4e02-b266-d39c48344e48", "Name": "MyTemplate" }