我在 AWS CloudFormation 主控台中收到下列錯誤: 「此範本不含任何要匯入的資源。進一步了解。」
簡短描述
在使用 AWS CloudFormation 主控台將資源匯入到在 CloudFormation 外部建立的現有堆疊時,您會收到此錯誤。
在下列任何情況下使用 AWS CloudFormation 主控台時,可能會發生此錯誤:
- **條件式資源。**您匯入的資源具有與評估為 False 的條件相關聯的條件索引鍵。
- **AWS Serverless Application Model (SAM) 範本。**匯入資源時,AWS CloudFormation 主控台不支援轉換區段。您無法透過使用 AWS::Serverless transform 的範本匯入資源。
- **Fn::Transform。**AWS CloudFormation 主控台不支援在匯入資源時使用內在函數 Fn::Transform。
您可以使用 AWS Command Line Interface (AWS CLI) 取代 AWS CloudFormation 主控台,使用以下命令解決範本的此錯誤:
若要解決條件式資源的此錯誤,請確定在條件索引鍵下指定的條件對要匯入的資源評估為 True。
AWS CLI 要求您使用 CloudFormation 命令 create-change-set 明確提供匯入的資源。
**注意:**如果您在執行 AWS CLI 命令時收到錯誤訊息,請確定您使用的是最新版本的 AWS CLI。
解決方法
在下列範例中,AWS CLI 用於將現有 AWS::ECS::Cluster 資源匯入至 CloudFormation 堆疊:
Resources:
...
ECSCluster2:
Condition: MyCondition
Type: AWS::ECS::Cluster
DeletionPolicy: Retain
Properties:
ClusterName: Cluster2
**注意:**在繼續執行下一個步驟之前,請確定條件 MyCondition 評估為 True。
若要使用 AWS CLI 匯入資源,請完成以下步驟:
**注意:**如果您的堆疊不在預設 AWS 區域中,請在命令中新增 --region,或透過設定和匯出 AWS_DEFAULT_REGION 環境變數以變更預設區域。
1. 建立名為 import.txt 的資源匯入檔案。例如:
[
{
"ResourceType": "AWS::ECS::Cluster",
"LogicalResourceId":
"ECSCluster2"
,
"ResourceIdentifier": {
"ClusterName":"Cluster2"
}
}
]
2. 若要針對堆疊建立變更集,請執行下列 create-change-set 命令:
ID=$(aws cloudformation create-change-set --stack-name testStack --change-set-name testSet --resources-to-import file://import.txt --change-set-type IMPORT --template-body file://template.yaml --capabilities CAPABILITY_AUTO_EXPAND --query 'Id' --output text)
**注意:**將 testStack 替換為您的堆疊名稱,將 template.yaml 替換為您的 CloudFormation 範本檔案名稱。上述命令會傳回變更集的 Amazon Resource Name (ARN),並將 ARN 儲存在環境變數 ID 中。
**注意:**只有當您的範本使用轉換時,才能使用 CAPABILITY_AUTO_EXPAND。
3. (選用) 若要等待變更集成功建立,請執行下列命令:
aws cloudformation wait change-set-create-complete --change-set-name ${ID}
4. 使用 AWS CloudFormation 主控台檢視變更集。或者,使用下列 describe-change-set 命令:
aws cloudformation describe-change-set --change-set-name ${ID}
5. 若要套用變更集並將資源匯入堆疊,請執行下列命令:
aws cloudformation execute-change-set --change-set-name ${ID}
6. (選用) 若要驗證範本中的所有屬性是否與您的資源相符,請在資源上使用漂移偵測。
相關資訊
使用 AWS CLI 將現有資源匯入堆疊
支援匯入和漂移偵測操作的資源