

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

# 使用 Systems Manager 公有參數來參考最新的 AMI
<a name="finding-an-ami-parameter-store"></a>

AWS Systems Manager 為 維護的公有 AMIs 提供公有參數 AWS。您可在啟動執行個體時使用公有參數來確保您使用的是最新 AMI。例如，公有參數 `/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-arm64` 可在所有區域中使用，並且一律指向指定區域中適用於 arm64 架構的 Amazon Linux 2023 AMI 最新版本。

公有參數可從以下路徑取得：
+ **Linux** – `/aws/service/ami-amazon-linux-latest`
+ **Windows** – `/aws/service/ami-windows-latest`

如需詳細資訊，請參閱*「AWS Systems Manager 使用者指南」*中的[使用公有參數](https://docs.aws.amazon.com/systems-manager/latest/userguide/parameter-store-public-parameters.html)。

## 列示 Amazon Linux AMI
<a name="list-ami-amazon-linux-latest"></a>

------
#### [ AWS CLI ]

**列出目前區域中AMIs AWS**  
請使用下面的 [get-parameters-by-path](https://docs.aws.amazon.com/cli/latest/reference/ssm/get-parameters-by-path.html) 命令。`--path` 參數的值為 Linux AMI 特定。

```
aws ssm get-parameters-by-path \
    --path /aws/service/ami-amazon-linux-latest \
    --query "Parameters[].Name"
```

------
#### [ PowerShell ]

**列出目前區域中AMIs AWS**  
使用 [Get-SSMParametersByPath](https://docs.aws.amazon.com/powershell/latest/reference/items/Get-SSMParametersByPath.html) cmdlet。

```
Get-SSMParametersByPath `
    -Path "/aws/service/ami-amazon-linux-latest" | `
    Sort-Object Name | Format-Table Name
```

------

## 列示 Windows AMI
<a name="list-ami-windows-latest"></a>

------
#### [ AWS CLI ]

**列出目前 AWS 區域中AMIs**  
請使用下面的 [get-parameters-by-path](https://docs.aws.amazon.com/cli/latest/reference/ssm/get-parameters-by-path.html) 命令。`--path` 參數的值為 Windows AMI 特定。

```
aws ssm get-parameters-by-path \
    --path /aws/service/ami-windows-latest \
    --query "Parameters[].Name"
```

------
#### [ PowerShell ]

**列出目前 AWS 區域中AMIs**  
使用 [Get-SSMParametersByPath](https://docs.aws.amazon.com/powershell/latest/reference/items/Get-SSMParametersByPath.html) cmdlet。

```
Get-SSMParametersByPath `
    -Path "/aws/service/ami-windows-latest" | `
    Sort-Object Name | Format-Table Name
```

------

## 使用公有參數啟動執行個體
<a name="launch-instance-public-parameter"></a>

如需在啟動執行個體時指定公有參數，請使用下列語法：`resolve:ssm:public-parameter`，其中 `resolve:ssm` 是標準字首，而 `public-parameter` 是公有參數的路徑和名稱。

------
#### [ AWS CLI ]

**使用公有參數啟動執行個體**  
請使用 `--image-id` 選項並搭配 [run-instances](https://docs.aws.amazon.com/cli/latest/reference/ec2/run-instances.html) 命令。此範例指定映像 ID 的 Systems Manager 公有參數，以使用最新 Amazon Linux 2023 AMI 啟動執行個體

```
--image-id resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64
```

------
#### [ PowerShell ]

**使用公有參數啟動執行個體**  
使用 [New-EC2Instance](https://docs.aws.amazon.com/powershell/latest/reference/items/New-EC2Instance.html) cmdlet 搭配 `-ImageId` 參數。此範例指定映像 ID 的 Systems Manager 公有參數，以使用最新 AMI for Windows Server 2022 啟動執行個體

```
-ImageId "resolve:ssm:/aws/service/ami-windows-latest/Windows_Server-2022-English-Full-Base"
```

------

如需使用 Systems Manager 參數的更多範例，請參閱[使用 AWS Systems Manager 參數存放區查詢最新的 Amazon Linux AMI IDs ](https://aws.amazon.com/blogs/compute/query-for-the-latest-amazon-linux-ami-ids-using-aws-systems-manager-parameter-store/) 和[使用 AWS Systems Manager 參數存放區查詢最新的 Windows AMI](https://aws.amazon.com/blogs/mt/query-for-the-latest-windows-ami-using-systems-manager-parameter-store/)。