

• 2026 年 4 月 30 日之後， AWS Systems Manager CloudWatch Dashboard 將不再可用。客戶可以繼續使用 Amazon CloudWatch 主控台來檢視、建立和管理其 Amazon CloudWatch 儀表板，就像現在一樣。如需詳細資訊，請參閱 [Amazon CloudWatch Dashboard 文件](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)。

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

# 在 Parameter Store 中使用參數階層
<a name="sysman-paramstore-hierarchies"></a>

以一般清單來管理數十或數百個參數不僅耗時且容易出錯。同時也很難識別任務的正確參數。這表示您可能會不小心使用錯誤的參數，或者可能會建立多個使用相同組態資料的參數。

您可以使用參數階層結構，來協助您整理和管理 參數。階層結構是一種參數名稱，其中包含您使用正斜線 (/) 定義的路徑。

**Topics**
+ [透過範例了解參數階層](#ps-hierarchy-examples)
+ [查詢階層中的參數](#ps-hierarchy-queries)
+ [使用 管理使用階層的參數 AWS CLI](#sysman-paramstore-walk-hierarchy)

## 透過範例了解參數階層
<a name="ps-hierarchy-examples"></a>

以下範例在名稱中使用三個階層來識別下列各項：

`/Environment/Type of computer/Application/Data`

`/Dev/DBServer/MySQL/db-string13`

您可以建立最多 15 個層級的階層。建議您建立階層以反映環境中現有的階層架構，如下範例所示：
+ 您的[持續整合](https://aws.amazon.com/devops/continuous-integration/)與[持續交付](https://aws.amazon.com/devops/continuous-delivery/)環境 (CI/CD 工作流程)

  `/Dev/DBServer/MySQL/db-string`

  `/Staging/DBServer/MySQL/db-string`

  `/Prod/DBServer/MySQL/db-string`
+ 使用容器的應用程式

  ```
  /MyApp/.NET/Libraries/my-password
  ```
+ 您的商業組織

  `/Finance/Accountants/UserList`

  `/Finance/Analysts/UserList`

  `/HR/Employees/EU/UserList`

參數階層將您建立參數的方式標準化，並讓您更輕鬆地隨時管理參數。參數階層也可以協助您識別設定任務的正確參數。這可協助您建立多個使用相同組態資料的參數。

您可以建立階層，讓您在不同的環境中共用參數，如以下在開發和臨時環境中使用密碼的範例所示。

`/DevTest/MyApp/database/my-password`

然後，您可以為生產環境建立唯一的密碼，如以下範例所示：

`/prod/MyApp/database/my-password`

您無需指定參數階層。您可以在第一層建立參數。這些稱為*根*參數。有關回溯相容性，在發佈階層之前於 Parameter Store 建立的所有參數皆為根參數。系統會將以下兩個參數視為根參數。

`/parameter-name`

`parameter-name`

## 查詢階層中的參數
<a name="ps-hierarchy-queries"></a>

使用階層的另一個好處是能夠使用 [GetParametersByPath](https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_GetParametersByPath.html) API 操作，來查詢在階層中某個層級*下*的所有參數。例如，如果您從 AWS Command Line Interface (AWS CLI) 執行下列命令，系統會傳回`Oncall`關卡下的所有參數：

```
aws ssm get-parameters-by-path --path /Dev/Web/Oncall
```

輸出範例：

```
{
    "Parameters": [
        {
            "Name": "/Dev/Web/Oncall/Week1",
            "Type": "String",
            "Value": "John Doe",
            "Version": 1,
            "LastModifiedDate": "2024-11-22T07:18:53.510000-08:00",
            "ARN": "arn:aws:ssm:us-east-2:123456789012:parameter/Dev/Web/Oncall/Week1",
            "DataType": "text"
        },
        {
            "Name": "/Dev/Web/Oncall/Week2",
            "Type": "String",
            "Value": "Mary Major",
            "Version": 1,
            "LastModifiedDate": "2024-11-22T07:21:25.325000-08:00",
            "ARN": "arn:aws:ssm:us-east-2:123456789012:parameter/Dev/Web/Oncall/Week2",
            "DataType": "text"
        }
    ]
}
```

若要檢視階層中已解密的 `SecureString` 參數，請指定路徑和 `--with-decryption` 參數，如以下範例所示。

```
aws ssm get-parameters-by-path --path /Prod/ERP/SAP --with-decryption
```

## 使用 管理使用階層的參數 AWS CLI
<a name="sysman-paramstore-walk-hierarchy"></a>

此程序示範如何利用 AWS CLI，來使用參數和參數階層。

**使用階層管理參數**

1. 如果您尚未安裝並設定 AWS Command Line Interface (AWS CLI)，請安裝並設定 。

   如需相關資訊，請參閱[安裝或更新最新版本的 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)。

1. 執行以下命令，以建立使用 `allowedPattern` 參數和 `String` 參數類型的參數。此範例中允許的模式表示參數值必須介於 1 位數到 4 位數。

------
#### [ Linux & macOS ]

   ```
   aws ssm put-parameter \
       --name "/MyService/Test/MaxConnections" \
       --value 100 --allowed-pattern "\d{1,4}" \
       --type String
   ```

------
#### [ Windows ]

   ```
   aws ssm put-parameter ^
       --name "/MyService/Test/MaxConnections" ^
       --value 100 --allowed-pattern "\d{1,4}" ^
       --type String
   ```

------

   該命令會傳回參數的版本號碼。

1. 執行以下命令，以 *嘗試* 以新的值覆寫您剛建立的參數。

------
#### [ Linux & macOS ]

   ```
   aws ssm put-parameter \
       --name "/MyService/Test/MaxConnections" \
       --value 10,000 \
       --type String \
       --overwrite
   ```

------
#### [ Windows ]

   ```
   aws ssm put-parameter ^
       --name "/MyService/Test/MaxConnections" ^
       --value 10,000 ^
       --type String ^
       --overwrite
   ```

------

   系統返回以下錯誤，因為新的值不符合您在上個步驟指定的允許模式的要求。

   ```
   An error occurred (ParameterPatternMismatchException) when calling the PutParameter operation: Parameter value, cannot be validated against allowedPattern: \d{1,4}
   ```

1. 執行以下其中`SecureString`一個命令，以建立使用 AWS 受管金鑰的 參數。此範例中允許的模式表示使用者可以指定任何字元，其值必須介於 8 到 20 個字元。

------
#### [ Linux & macOS ]

   ```
   aws ssm put-parameter \
       --name "/MyService/Test/my-password" \
       --value "p#sW*rd33" \
       --allowed-pattern ".{8,20}" \
       --type SecureString
   ```

------
#### [ Windows ]

   ```
   aws ssm put-parameter ^
       --name "/MyService/Test/my-password" ^
       --value "p#sW*rd33" ^
       --allowed-pattern ".{8,20}" ^
       --type SecureString
   ```

------

1. 執行以下命令，以建立更多使用先前步驟的階層結構的參數。

------
#### [ Linux & macOS ]

   ```
   aws ssm put-parameter \
       --name "/MyService/Test/DBname" \
       --value "SQLDevDb" \
       --type String
   ```

   ```
   aws ssm put-parameter \
       --name "/MyService/Test/user" \
       --value "SA" \
       --type String
   ```

   ```
   aws ssm put-parameter \
       --name "/MyService/Test/userType" \
       --value "SQLuser" \
       --type String
   ```

------
#### [ Windows ]

   ```
   aws ssm put-parameter ^
       --name "/MyService/Test/DBname" ^
       --value "SQLDevDb" ^
       --type String
   ```

   ```
   aws ssm put-parameter ^
       --name "/MyService/Test/user" ^
       --value "SA" ^
       --type String
   ```

   ```
   aws ssm put-parameter ^
       --name "/MyService/Test/userType" ^
       --value "SQLuser" ^
       --type String
   ```

------

1. 執行以下命令，以取得兩個參數的值。

------
#### [ Linux & macOS ]

   ```
   aws ssm get-parameters \
       --names "/MyService/Test/user" "/MyService/Test/userType"
   ```

------
#### [ Windows ]

   ```
   aws ssm get-parameters ^
       --names "/MyService/Test/user" "/MyService/Test/userType"
   ```

------

1. 執行以下命令，查詢單一層級下的所有參數。

------
#### [ Linux & macOS ]

   ```
   aws ssm get-parameters-by-path \
       --path "/MyService/Test"
   ```

------
#### [ Windows ]

   ```
   aws ssm get-parameters-by-path ^
       --path "/MyService/Test"
   ```

------

1. 執行下列命令，以刪除兩個參數

------
#### [ Linux & macOS ]

   ```
   aws ssm delete-parameters \
       --names "/IADRegion/Dev/user" "/IADRegion/Dev/userType"
   ```

------
#### [ Windows ]

   ```
   aws ssm delete-parameters ^
       --names "/IADRegion/Dev/user" "/IADRegion/Dev/userType"
   ```

------