Knowledge Center Monthly Newsletter - March 2025
Stay up to date with the latest from the Knowledge Center. See all new and updated Knowledge Center articles published in the last month and re:Post’s top contributors.
我有多個參數值儲存在 AWS Systems Manager Parameter Store 中。我想根據我的需求在 AWS CloudFormation 堆疊中動態使用這些參數。
下列解決方案使用 Windows 和 Linux 作為範例環境,示範如何在 CloudFormation 堆疊中動態使用不同參數。
開啟 AWS Systems Manager 主控台。
在導覽窗格中,選擇參數存放區。
建立 System Manager 參數作為 String 類型,以儲存 Linux 或 Windows 的 Amazon Machine Image (AMI) ID。
Linux:
LinuxAmiId - AMI-Id-for-Linux-resources
-or-
Windows:
WindowsAmiId - AMI-Id-for-Windows-resources
**注意:**用資源的 AMI ID 取代 AMI-Id-for-Linux-resources 和 AMI-Id-for-Windows-resources。
使用下列範例 CloudFormation 範本來建立參數,以選取要用於部署的 AMI:
{ "Parameters": { "AmiToUse": { "Type": "String", "AllowedValues": [ "windows", "linux" ] } }, "Conditions": { "CreateWindowsResources": { "Fn::Equals": [ "windows", { "Ref": "AmiToUse" } ] } }, "Resources": { "EC2Instance": { "Type": "AWS::EC2::Instance", "Properties": { "ImageId": { "Fn::If": [ "CreateWindowsResources", "{{resolve:ssm:WindowsAmiId:1}}", "{{resolve:ssm:LinuxAmiId:1}}" ] } } } } }
注意:在上一個範本中,如果將 AmiTouse 選取為視窗,則 CreateWindowsResources 條件評估為 true。CloudFormation 使用儲存在 WindowsAmild 中的值來佈建 AWS::EC2::執行個體 資源。對於 Linux,條件會評估為 false,而 CloudFormation 會使用儲存在 LinuxAmild 中的值來佈建 AWS::EC2::執行個體資源。
如需有關動態參考的詳細資訊,請參閱取得儲存在其他使用動態參考的服務的值。
動態參考