I'm trying to write a bash function that does the following:
- If there is no 3rd argument, run a command.
- If there is a third argument, take every argument from the third one and run a command.
The problem I have is the last bit of the command --capabilities CAPABILITY_IAM
in the else
statement that I don't want to pass in all the time if I have multiple parameters.
An error occurred (InsufficientCapabilitiesException) when calling the CreateStack operation: Requires capabilities : [CAPABILITY_NAMED_IAM]
// that means I need to pass in --capabilities CAPABILITY_IAM
Is there a way to tell bash that: hey, take all the args from the 3rd one, then add the --capabilities CAPABILITY_IAM
after? Like in JavaScript I can do this:
function allTogetherNow(a, b, ...c) {
console.log(`${a}, ${b}, ${c}. Can I have a little more?`);
}
allTogetherNow('one', 'two', 'three', 'four')
Here's my function:
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
}
And the 3rd and so on parameters look like this if I don't use a bash function:
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
Update 22 May 2019:
Following Dennis Williamson's answer below. I've tried:
- Passing the parameters in the AWS way:
cloudformation_create STACK_NAME FILE_NAME ParameterKey=KeyPairName,ParameterValue=TestKey ParameterKey=SubnetIDs,ParameterValue=SubnetID1
Got error:
An error occurred (ValidationError) when calling the CreateStack operation: Parameters: [...] must have values
- Pass in as a string:
cloudformation_create STACK_NAME FILE_NAME "ParameterKey=KeyPairName,ParameterValue=TestKey ParameterKey=SubnetIDs,ParameterValue=SubnetID1"
Got error:
An error occurred (ValidationError) when calling the CreateStack operation: ParameterValue for ... is required
- Pass in without
ParameterKey
andParameterValue
:
cloudformation_create STACK_NAME FILE_NAME KeyPairName=TestKey SubnetIDs=SubnetID1
Got error:
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
- Pass in without
ParameterKey
andParameterValue
and as a string. Got error:
arameter validation failed:
Unknown parameter in Parameters[0]: "PARAM_NAME", must be one of: ParameterKey, ParameterValue, UsePreviousValue, ResolvedValue
I tried Alex Harvey's answer and got this:
An error occurred (ValidationError) when calling the CreateStack operation: Template format error: unsupported structure.