如何與 AWS CloudFormation 堆疊動態使用儲存在 System Manager 中的參數?

1 分的閱讀內容
0

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

解決方法

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

  1. 開啟 AWS Systems Manager 主控台

  2. 在導覽窗格中,選擇參數存放區

  3. 建立 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-resourcesAMI-Id-for-Windows-resources

  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 選取為視窗,則 CreateWindowsResources 條件評估為 true。CloudFormation 使用儲存在 WindowsAmild 中的值來佈建 AWS::EC2::執行個體 資源。對於 Linux,條件會評估為 false,而 CloudFormation 會使用儲存在 LinuxAmild 中的值來佈建 AWS::EC2::執行個體資源。

如需有關動態參考的詳細資訊,請參閱取得儲存在其他使用動態參考的服務的值

相關資訊

動態參考

AWS 官方
AWS 官方已更新 4 個月前