Dynamically generated AWS CLI command doesn't run in bash script but it works if manually copy/paste run

0

I use bit bash in windows and uses vi code for all files.

So I have a .sh file takes a .conf text file as input. essentially .sh file copies 1 line at a time from .conf file to create a dynamically generated cli command to create security group and inbound/outbound etc. rule.

the .conf file looks like this and uses ":" as field seperator: ssgroup1:inbound:--protocol tcp --port 80 --cidr 0.0.0.0/0 ssgroup2:inbound:--protocol icmp --port -1 --cidr 0.0.0.0/0 .......

the dynamically created command looks like below: aws ec2 authorize-security-group-ingress --group-id sg-0c503ec0000fc0113 --protocol tcp --port 80 --cidr 0.0.0.0/0 --region us-east-2 aws ec2 authorize-security-group-ingress --group-id sg-0c503ec0000fc0113 --protocol icmp --port -1 --cidr 0.0.0.0/0 --region us-east-2

While security group created correctly, when the script ran about dynamically created command, it doesn't like. it spills out the following output:

usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters] To see help text, you can run: aws help aws <command> help aws <command> <subcommand> help

Unknown options: --protocol tcp --port 80 --cidr 0.0.0.0/0

So couple of things I tried

  1. added a sleep 5 for security group to be created for sure (just guessing)
  2. may .conf file in vi code added some line return etc. so I reopened it in nano and resave it.

I am totally new to aws, bash and shell whatever you name it. You probably can tell why i do it this way:) Can anyone shed light on this?

Thank You!

asked 3 months ago73 views
1 Answer
0

It seems like the issue you're experiencing is related to how the AWS CLI command is being constructed and executed within your bash script. Here are a few potential reasons and solutions for your problem:

  1. Command Construction: Ensure that your script is correctly constructing the AWS CLI command. The error message suggests that the options are not being recognized, which could mean they're not being passed correctly to the AWS CLI.

  2. Quoting: In bash scripts, it's important to properly quote your variables, especially when they contain spaces or special characters. Try enclosing your dynamically generated command in double quotes when executing it.

  3. IFS (Internal Field Separator): If you're using ':' as a field separator in your .conf file, make sure you're setting the IFS appropriately in your script when reading the file.

  4. Command Execution: Instead of trying to execute the entire command as a string, consider using an array to store the command and its arguments, then use the array to execute the command.

Here's an example of how you might modify your script:

while IFS=':' read -r group_name direction params; do
cmd=(aws ec2 authorize-security-group-ingress --group-id "$group_id" $params --region us-east-2)
"${cmd[@]}"
done < your_config.conf

This approach constructs the command as an array and then executes it, which can help avoid issues with spaces and special characters.

  1. Error Handling: Add error checking after each AWS CLI command to catch and display any errors that occur.

  2. AWS CLI Version: Ensure you're using a recent version of the AWS CLI that supports all the options you're trying to use.

  3. Permissions: Verify that you have the necessary permissions to execute these AWS CLI commands.

If the issue persists, you might want to add some debug output to your script to see exactly what command is being constructed before it's executed. This can help identify any malformed commands.

Remember to always be cautious when dynamically generating and executing commands, especially ones that modify your AWS resources. Ensure your input is properly sanitized and validated.
Sources
Use CreateSecurityGroup with an AWS SDK or CLI - AWS SDK Code Examples
create-security-group — AWS CLI 2.18.3 Command Reference

profile picture
answered 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions