如何利用 AWS CloudFormation 堆疊動態使用存放在 Systems Manager 中的參數?

1 分的閱讀內容
0

我有多個參數值存放在 AWS Systems Manager Parameter Store 中。我想根據我的需求在 AWS CloudFormation 堆疊中動態使用這些參數。

簡短描述

以下解決方案以 Microsoft Windows 和 Linux 為示例環境,示範如何在 CloudFormation 堆疊中動態使用不同的參數。

重要 :使用以下命令和範本時,請確認將 WindowsLinux 替換成您的需求。

解決方案

1.    開啟 AWS Systems Manager 主控台

2.    在導覽窗格中,選擇Parameter Store (參數存放)。

3.    建立類型為 String (字串) 的 Systems Manager 參數,以存放用於 Linux 或 Windows 的 Amazon Machine Image (AMI) ID。

Linux:

LinuxAmiId - AMI-Id-for-Linux-resources

-或-

Windows:

WindowsAmiId - AMI-Id-for-Windows-resources

**注意:**將 AMI-Id-for-Linux-resourcesAMI-Id-for-Windows-resources 替換成您資源的 AMI ID。

4.    使用以下示例 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 選為 Windows,則 CreateWindowsResources 條件評估結果為 True。CloudFormation 使用存放在 WindowsAmiId 中的值佈建 AWS::EC2::Instance 資源。對於 Linux,條件評估結果為 False,CloudFormation 使用存放在 LinuxAmiId 的值佈建 AWS::EC2::Instance 資源。

有關動態參照的詳細資訊,請參閲使用動態參照指定範本值

相關資訊

CloudFormation 研討會:動態參照實驗室


AWS 官方
AWS 官方已更新 2 年前