Issue
I am trying to loop through an array elements called pfl
which contains --profile
and another rgn
which contains --region
values respectively.
I am not sure how i can call them correctly in a for loop.
#!/bin/bash
#
# below the --profiles defined as "pfl"
pfl="""
dev
qa
seoul
emea
apac
amec
mde
deploy
getprodkey
phx
"""
# below is `--region` defined as "rgn"
rgn="""
eu-north-1
ap-south-1
eu-west-3
eu-west-2
eu-west-1
ap-northeast-3
ap-northeast-2
ap-northeast-1
sa-east-1
ca-central-1
ap-southeast-1
ap-southeast-2
eu-central-1
us-east-1
us-east-2
us-west-1
us-west-2
"""
for loc in "${pfl[@]}"; do aws ec2 describe-vpcs --region "${rgn[$@]}" --profile $loc --query "Vpcs[*].{ CIDR: CidrBlock, STATE: State}" --output text;done
When i am invoking the above script it gives issues like Unknown options: seoul, emea, apac, amec, mde, deploy, getprodkey, phx, qa
.
However if i shall try that as a single command it will run without issues.
$ aws ec2 describe-vpcs --region us-west-2 --query "Vpcs[*].{CIDR: CidrBlock, STATE: State, VPCID: VpcId, OWNER: OwnerId, ASSOCID: CidrBlockAssociationSet.AssociationId}" --profile phx --output text
None 192.168.0.0/17 947877767552 available vpc-0c13t7787897ea4b9
Solution
If you put your data into proper bash arrays instead of strings then you can loop through them easily:
#!/bin/bash
pfl=( dev qa seoul emea apac amec mde deploy getprodkey phx )
rgn=(
eu-north-1 ap-south-1 eu-west-3 eu-west-2 eu-west-1
ap-northeast-3 ap-northeast-2 ap-northeast-1 sa-east-1 ca-central-1
ap-southeast-1 ap-southeast-2 eu-central-1
us-east-1 us-east-2 us-west-1 us-west-2
)
for p in "${pfl[@]}"
do
for r in "${rgn[@]}"
do
aws ec2 describe-vpcs \
--region "$r" \
--profile "$p" \
--query 'Vpcs[*].{ CIDR: CidrBlock, STATE: State}' \
--output text
done
do
remark: I don't think that you can query multiple regions at once so I loop through them one by one
Answered By - Fravadona Answer Checked By - David Goodson (WPSolving Volunteer)