2014-02-28 35 views
2

我試圖創建一個PowerShell腳本,其中包括創建AWS CloudFormation堆棧。我在使用aws cloudformation create-stack命令時遇到了麻煩,但它似乎沒有提取參數。下面是摘錄給我找麻煩:AWS:調用CreateStack操作時發生客戶端錯誤(ValidationError):需要ParameterKey ...的ParameterValue

$version = Read-Host 'What version is this?' 
aws cloudformation create-stack --stack-name Cloud-$version --template-body C:\awsdeploy\MyCloud.template --parameters ParameterKey=BuildNumber,ParameterValue=$version 

我收到的錯誤是:

aws : 
At C:\awsdeploy\Deploy.ps1:11 char:1 
+ aws cloudformation create-stack --stack-name Cloud-$version --template-bo ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo   : NotSpecified: (:String) [], RemoteException 
+ FullyQualifiedErrorId : NativeCommandError 

A client error (ValidationError) occurred when calling the CreateStack operation: ParameterValue for ParameterKey BuildNumber is required 

我知道CloudFormation腳本是可以的,因爲我可以不通過AWS資源管理問題執行它。參數部分看起來是這樣的:

"Parameters" : { 
    "BuildNumber" : { "Type" : "Number" } 
    }, 

我試過以下,其中沒有一個似乎幫助:

  • 用一個靜態值
  • 改變從數參數類型替換$版本爲String
  • 試圖通過JSON格式

上任何一個沒有骰子,同樣的參數列表錯誤。這就像它只是因爲某些原因不接受參數。有任何想法嗎?

回答

3

我敢打賭,Powershell在解析該逗號時遇到了困難,之後失去了ParameterValue。你可能想嘗試在一個字符串--parameter後換整段(雙引號的,所以$version仍然解決):

aws cloudformation create-stack --stack-name Cloud-$version --template-body C:\awsdeploy\MyCloud.template --parameters "ParameterKey=BuildNumber,ParameterValue=$version" 

或者,做不到這一點,嘗試running the line explicitly in the cmd environment


如果你有興趣的替代解決方案,AWS已經實施了他們的命令行工具稱爲AWS Tools for Powershell單獨的實用程序。 create-stack映射到New-CFNStack如本文檔中所示:New-CFNStack Docs

看起來這將是等同的調用:

$p1 = New-Object -Type Amazon.CloudFormation.Model.Parameter 
$p1.ParameterKey = "BuildNumber" 
$p1.ParameterValue = "$version" 

New-CFNStack -StackName "cloud-$version" ` 
-TemplateBody "C:\awsdeploy\MyCloud.template" ` 
-Parameters @($p1) 
+0

- 安東尼感謝輸入,這讓我在正確的方向。該命令工作,但要求我使用-TemplateURL設置爲上傳到S3位置的模板,如下所示:'New-CFNStack -StackName「cloud-$ version」 -TemplateURL「https://s3.amazonaws.com/。 ../my.template「 -Parameters @($ p1)' – Jayoaichen

相關問題