

文档 AWS SDK 示例 GitHub 存储库中还有更多 [S AWS DK 示例](https://github.com/awsdocs/aws-doc-sdk-examples)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 将 `EstimateTemplateCost` 与 CLI 配合使用
<a name="cloudformation_example_cloudformation_EstimateTemplateCost_section"></a>

以下代码示例演示如何使用 `EstimateTemplateCost`。

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

**AWS CLI**  
**估算模板成本**  
以下 `estimate-template-cost` 示例为当前文件夹中名为 `template.yaml` 的模板生成成本估算。  

```
aws cloudformation estimate-template-cost \
    --template-body file://template.yaml
```
输出：  

```
{
    "Url": "http://calculator.s3.amazonaws.com/calc5.html?key=cloudformation/7870825a-xmpl-4def-92e7-c4f8dd360cca"
}
```
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[EstimateTemplateCost](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/estimate-template-cost.html)*中的。

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

**适用于 PowerShell V4 的工具**  
**示例 1：返回一个带有查询字符串的 AWS 简单月度计算器网址，该字符串描述了运行模板所需的资源。模板是从指定的 Amazon S3 URL 获取的，并且应用了单个自定义参数。也可以使用 “Key” 和 “Value” 来指定参数，而不是 ParameterKey “” 和 “ParameterValue”。**  

```
Measure-CFNTemplateCost -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
                        -Region us-west-1 `
                        -Parameter @{ ParameterKey="KeyName"; ParameterValue="myKeyPairName" }
```
**示例 2：返回一个带有查询字符串的 AWS 简单月度结算器网址，该字符串描述了运行模板所需的资源。模板是根据提供的内容和应用的自定义参数进行解析的（此示例假设模板内容会声明两个参数，'和KeyName' InstanceType '）。也可以使用 “密钥” 和 “值” 来指定自定义参数，而不是 “ParameterKey” 和 “ParameterValue”。**  

```
Measure-CFNTemplateCost -TemplateBody "{TEMPLATE CONTENT HERE}" `
                        -Parameter @( @{ ParameterKey="KeyName"; ParameterValue="myKeyPairName" },`
                                      @{ ParameterKey="InstanceType"; ParameterValue="m1.large" })
```
**示例 3：使用 New-Object 生成模板参数集，并返回一个 “ AWS 简单月度计算器” 网址，其中包含描述运行模板所需资源的查询字符串。模板是从提供的内容中解析出来的，并带有自定义参数（此示例假设模板内容会声明两个参数，KeyName'和' InstanceType '）。**  

```
$p1 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p1.ParameterKey = "KeyName"
$p1.ParameterValue = "myKeyPairName"

$p2 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p2.ParameterKey = "InstanceType"
$p2.ParameterValue = "m1.large"

Measure-CFNTemplateCost -TemplateBody "{TEMPLATE CONTENT HERE}" -Parameter @( $p1, $p2 )
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 4) [EstimateTemplateCost](https://docs.aws.amazon.com/powershell/v4/reference)中的。

**适用于 PowerShell V5 的工具**  
**示例 1：返回一个带有查询字符串的 AWS 简单月度计算器网址，该字符串描述了运行模板所需的资源。模板是从指定的 Amazon S3 URL 获取的，并且应用了单个自定义参数。也可以使用 “Key” 和 “Value” 来指定参数，而不是 ParameterKey “” 和 “ParameterValue”。**  

```
Measure-CFNTemplateCost -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
                        -Region us-west-1 `
                        -Parameter @{ ParameterKey="KeyName"; ParameterValue="myKeyPairName" }
```
**示例 2：返回一个带有查询字符串的 AWS 简单月度结算器网址，该字符串描述了运行模板所需的资源。模板是根据提供的内容和应用的自定义参数进行解析的（此示例假设模板内容会声明两个参数，'和KeyName' InstanceType '）。也可以使用 “密钥” 和 “值” 来指定自定义参数，而不是 “ParameterKey” 和 “ParameterValue”。**  

```
Measure-CFNTemplateCost -TemplateBody "{TEMPLATE CONTENT HERE}" `
                        -Parameter @( @{ ParameterKey="KeyName"; ParameterValue="myKeyPairName" },`
                                      @{ ParameterKey="InstanceType"; ParameterValue="m1.large" })
```
**示例 3：使用 New-Object 生成模板参数集，并返回一个 “ AWS 简单月度计算器” 网址，其中包含描述运行模板所需资源的查询字符串。模板是从提供的内容中解析出来的，并带有自定义参数（此示例假设模板内容会声明两个参数，KeyName'和' InstanceType '）。**  

```
$p1 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p1.ParameterKey = "KeyName"
$p1.ParameterValue = "myKeyPairName"

$p2 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p2.ParameterKey = "InstanceType"
$p2.ParameterValue = "m1.large"

Measure-CFNTemplateCost -TemplateBody "{TEMPLATE CONTENT HERE}" -Parameter @( $p1, $p2 )
```
+  有关 API 的详细信息，请参阅 *AWS Tools for PowerShell Cmdlet 参考 (V* 5) [EstimateTemplateCost](https://docs.aws.amazon.com/powershell/v5/reference)中的。

------