文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
搭配使用 EstimateTemplateCost 與 CLI
下列程式碼範例示範如何使用 EstimateTemplateCost。
- CLI
-
- AWS CLI
-
估算範本成本
下列
estimate-template-cost範例會為目前資料夾中名為template.yaml的範本產生估算成本。aws cloudformation estimate-template-cost \ --template-bodyfile://template.yaml輸出:
{ "Url": "http://calculator.s3.amazonaws.com/calc5.html?key=cloudformation/7870825a-xmpl-4def-92e7-c4f8dd360cca" }-
如需 API 詳細資訊,請參閱《AWS CLI 命令參考》中的 EstimateTemplateCost
。
-
- PowerShell
-
- Tools for PowerShell V4
-
範例 1:傳回 AWS 簡易每月計算器 URL,其中包含查詢字串,說明執行範本所需的資源。範本是從指定的 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 簡易每月計算器 URL,其中包含查詢字串,說明執行範本所需的資源。範本會從提供的內容剖析,並套用自訂參數 (此範例假設範本內容已宣告兩個參數:'KeyName' 和 'InstanceType')。自訂參數也可以使用 'Key' 和 'Value' (而不是 '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 參考 (V4)》中的 EstimateTemplateCost。
-
- Tools for PowerShell V5
-
範例 1:傳回 AWS 簡易每月計算器 URL,其中包含查詢字串,描述執行範本所需的資源。範本是從指定的 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 簡易每月計算器 URL,其中包含查詢字串,說明執行範本所需的資源。範本會從提供的內容剖析,並套用自訂參數 (此範例假設範本內容已宣告兩個參數:'KeyName' 和 'InstanceType')。自訂參數也可以使用 'Key' 和 'Value' (而不是 '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 參考 (V5)》中的 EstimateTemplateCost。
-