与 AWS SDK或EstimateTemplateCost一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

与 AWS SDK或EstimateTemplateCost一起使用 CLI

以下代码示例演示如何使用 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" }
PowerShell
用于 PowerShell

示例 1:返回一个URL带有查询字符串的 AWS 简单月度计算器,该字符串描述了运行模板所需的资源。模板是从指定的 Amazon S3 中获取的,URL并应用了单个自定义参数。也可以使用 “键” 和 “值” 来指定参数,而不是 “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:返回一个URL带有查询字符串的 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 简单月URL度计算器,其中包含描述运行模板所需资源的查询字符串。模板是从提供的内容中解析出来的,并带有自定义参数(此示例假设模板内容会声明两个参数,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 参考EstimateTemplateCost中的。