1

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? :/

Alex Latchford
  • 655
  • 1
  • 9
  • 17

1 Answers1

1

You're setting CF_ARGS to a single string, not an array. You need to wrap another set of parentheses around it to get an array.

CF_ARGS=($(for i in "${!CF_PARAMS[@]}"; do j=$(($i+1)); if [ -n "${!j}" ]; then echo "${CF_PARAMS[$i]} ${!j}"; fi; done))

And to make it treat each line of output from the for loop as a single argument, you can set IFS to only contain newline:

IFS=$'\n'
CF_ARGS=($(for i in "${!CF_PARAMS[@]}"; do j=$(($i+1)); if [ -n "${!j}" ]; then echo "${CF_PARAMS[$i]} ${!j}"; fi; done))
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Fantastic! Thanks for that, I've hit another problem though. I've updated the original question with a follow up, any suggestions? – Alex Latchford Mar 16 '17 at 23:34
  • I had to use `IFS=$";"` as the multiline string was splitting too but great idea! Still not fully working but will open a new question for the next bit :) – Alex Latchford Mar 17 '17 at 17:42
  • Hey @Barmar I've added another question for the next part of this problem, thanks for your help with this bit! http://stackoverflow.com/questions/42864168/call-bash-function-with-array-of-arguments-including-multiline-string – Alex Latchford Mar 17 '17 at 17:53
  • As an additional point. I ran into further problems downstream because I concatenated arguments together here: `"${CF_PARAMS[$i]} ${!j}"`. It should have actually been: `"${CF_PARAMS[$i]};${!j};"`. This results in 2 array items, not just 1 which when used as function arguments downstream make it possible to pass the array as function inputs. – Alex Latchford Mar 21 '17 at 00:42