Home > Back-end >  Passing variables to aws cli command in bash script
Passing variables to aws cli command in bash script

Time:04-09

I'm new to writing bash scripts and am having trouble with passing in variable to an AWS CLI command. I'm sure there's an easy fix for this, and I'm just having trouble understanding this syntax. Below is the script I have so far:

#!/bin/bash

CONFIG_RECORDER=`aws configservice describe-configuration-recorders
export CONFIG_RECORDER_NAME=$(jq -r '.ConfigurationRecorders[].name' <<<"$CONFIG_RECORDER")

sudo yum -y install jq

aws configservice delete-configuration-recorder --configuration-recorder-name $CONFIG_RECORDER_NAME

The purpose of this script is to delete the configuration recorder using the aws cli command. I'm getting an error that aws: error: argument --configuration-recorder-name: expected one argument

I have a variable, $CONFIG_RECORDER_NAME, being specified as the argument but for some reason it is not reading. Any advice on how to pass in this variable to this command would be helpful.

CodePudding user response:

The following seems to work correctly if the aws configservice describe-configuration-recorders invocation returns a single configuration recorder name:

#!/bin/bash

sudo yum -y install jq

CONFIG_RECORDER=`aws configservice describe-configuration-recorders`
# echo "CONFIG_RECORDER="$CONFIG_RECORDER

CONFIG_RECORDER_NAME=$(jq -r '.ConfigurationRecorders[].name' <<<"$CONFIG_RECORDER")
# echo "CONFIG_RECORDER_NAME="$CONFIG_RECORDER_NAME

aws configservice \
    delete-configuration-recorder \
    --configuration-recorder-name $CONFIG_RECORDER_NAME

There's actually no need to use jq. You could simply use:

#!/bin/bash

CONFIG_RECORDER_NAME=`aws configservice describe-configuration-recorders --query 'ConfigurationRecorders[0].name' --output text`

aws configservice \
    delete-configuration-recorder \
    --configuration-recorder-name $CONFIG_RECORDER_NAME
  • Related