Search code examples
bashamazon-web-servicescommand-line-interfacesh

Run aws cli looping over names from a text file - parameter store


I'm trying to run an awscli command for multiple resources as a loop in a bash script.

For example:

aws ssm get-parameters --name "NAME1", "NAME2", "NAME3"

I've added all the parameter names into a text file. How do I run the CLI command against each name in the file?

Here is my script:

AWS_PARAM="aws ssm get-parameters --name" $FILE
FILE="parameters.txt"

for list in $FILE; do
 $AWS_PARAM $list
done

The expected output should run the CLI on all the names in the file. I know the CLI is expecting the "name" of the parameter store. I'm hoping someone can help with looping the names from the list and running the CLI.

Thank you!


Solution

  • Here's an example of how to iterate over the parameter names and log the output to one file:

    #!/bin/bash
    
    AWS_PARAM="aws ssm get-parameters --name"
    input="input.txt"
    output="output.log"
    
    echo > "$output"
    
    while IFS= read -r line
    do
      echo "$line"
      $AWS_PARAM "$line" >> "$output"
    done < "$input"