【发布时间】:2019-10-08 08:02:01
【问题描述】:
我正在尝试编写一个执行以下操作的 bash 函数:
- 如果没有第三个参数,请运行命令。
- 如果有第三个参数,则从第三个参数中取出每个参数并运行命令。
我遇到的问题是else 语句中命令--capabilities CAPABILITY_IAM 的最后一位,如果我有多个参数,我不想一直传入。
An error occurred (InsufficientCapabilitiesException) when calling the CreateStack operation: Requires capabilities : [CAPABILITY_NAMED_IAM]
// that means I need to pass in --capabilities CAPABILITY_IAM
有没有办法告诉 bash:嘿,从第 3 个参数中取出所有参数,然后在后面添加 --capabilities CAPABILITY_IAM?就像在 JavaScript 中一样,我可以这样做:
function allTogetherNow(a, b, ...c) {
console.log(`${a}, ${b}, ${c}. Can I have a little more?`);
}
allTogetherNow('one', 'two', 'three', 'four')
这是我的功能:
cloudformation_create() {
if [ -z "$3" ]; then
aws cloudformation create-stack --stack-name "$1" --template-body file://"$2" --capabilities CAPABILITY_IAM
else
aws cloudformation create-stack --stack-name "$1" --template-body file://"$2" --parameters "${@:3}" --capabilities CAPABILITY_IAM
fi
}
如果我不使用 bash 函数,第三个等参数看起来像这样:
aws cloudformation create-stack --stack-name MY_STACK_NAME --template-body file://MY_FILE_NAME --parameters ParameterKey=KeyPairName,ParameterValue=TestKey ParameterKey=SubnetIDs,ParameterValue=SubnetID1 --capabilities CAPABILITY_IAM
2019 年 5 月 22 日更新:
以下丹尼斯威廉姆森的回答。我试过了:
- 以AWS方式传递参数:
cloudformation_create STACK_NAME FILE_NAME ParameterKey=KeyPairName,ParameterValue=TestKey ParameterKey=SubnetIDs,ParameterValue=SubnetID1
出现错误:
An error occurred (ValidationError) when calling the CreateStack operation: Parameters: [...] must have values
- 作为字符串传入:
cloudformation_create STACK_NAME FILE_NAME "ParameterKey=KeyPairName,ParameterValue=TestKey ParameterKey=SubnetIDs,ParameterValue=SubnetID1"
出现错误:
An error occurred (ValidationError) when calling the CreateStack operation: ParameterValue for ... is required
- 不带
ParameterKey和ParameterValue:
cloudformation_create STACK_NAME FILE_NAME KeyPairName=TestKey SubnetIDs=SubnetID1
出现错误:
Parameter validation failed:
Unknown parameter in Parameters[0]: "PARAM_NAME", must be one of: ParameterKey, ParameterValue, UsePreviousValue, ResolvedValue
// list of all the params with the above error
- 不带
ParameterKey和ParameterValue并作为字符串传入。出现错误:
arameter validation failed:
Unknown parameter in Parameters[0]: "PARAM_NAME", must be one of: ParameterKey, ParameterValue, UsePreviousValue, ResolvedValue
我尝试了 Alex Harvey 的回答,得到了这个:
An error occurred (ValidationError) when calling the CreateStack operation: Template format error: unsupported structure.
【问题讨论】:
-
我觉得这个功能不错。怎么不工作了?
-
@DennisWilliamson 最后需要
--capabilities CAPABILITY_IAM。出于某种原因,我的 bash 函数接受了所有参数,但没有在最后添加功能。 -
当您使用使用“${@:3}”(而不是星号)的 Bash 函数(就在上面的“这是我的函数:”下方)时会发生什么?
-
我们不会在 Stack Overflow 上的问题标题中添加“已解决”。请发布您的解决方案作为答案。您甚至可以将自己的答案标记为已接受。
-
@Viet,仅供参考,“不受支持的结构”是因为我的拼写错误;我错过了“file://”位。我已经更新了。
标签: bash amazon-web-services amazon-cloudformation