Trying to make a reusable function that can take up to 4 parameters (though the solution it should be arbitrary). I want to zip these with parameters for AWS CloudFormation such that if I don't pass a positional argument to update_stack it won't get included in CF_ARGS.
##
# Update the stack with the given template
# @param: stack_name
# @param: template_body
# @param: [tags]
# @param: [parameters]
##
update_stack() {
# Handle optional func parameters
CF_PARAMS=("--stack-name" "--template-body" "--tags" "--parameters")
# Only include a param if we have a value supplied as an argument. (+1 to ignore script name)
CF_ARGS=($(for i in "${!CF_PARAMS[@]}"; do j=$(($i+1)); if [ -n "${!j}" ]; then echo "${CF_PARAMS[$i]} ${!j}"; fi; done))
# Should make the call as "aws cloudformation create-stack --stack-name $1 --template-body $2"
# If $3 or $4 are not supplied then it should not add them to CFG_ARGS
aws cloudformation create-stack "${CFG_ARGS[@]}"
}
I think I've got the CF_ARGS construction working as expected I just don't know how to apply arguments in a bash function call and my google skills have failed on how to find a solution for this. Please help!
Thanks, Alex
PS. The error I've getting from awscli is (maybe the double quotes?):
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
aws: error: argument --stack-name is required
EDIT (1):
Thanks @Barmar for your first solution. That got me to this error:
Unknown options: Description:, Handles, Authentication, &, Access, permissions, Resources:, ##, #, Policies, ##, PolicyDevelopers:, Type:, AWS::IAM::Policy, Properties:, PolicyName:, Developers-cf, #, @todo:, Remove, suffix, PolicyDocument:, Version:, "2012-10-17", Statement:, -, Effect:, Allow, NotAction:, -, iam:*, -, sts:AssumeRole, ...
The "--template-body" argument is a multi-line string (YAML file to be exact), is there anyway to double-quote each argument? :/