1
votes

i am trying to fetch VPC details for all region.i tried to run my script without default profile which results in error "You must specify a region. You can also configure your region by running "aws configure" ,evnthough i have my own profile configured with all required details for it. same script works fine after configuring default profile. Question is does AWS CLI requires default profile as mandatory ? My script

for region in `aws ec2 describe-regions  --output text| cut -f4`
do
aws ec2 --profile sam  --region $region --output text --query 'Vpcs[*].{VpcId:VpcId,CidrBlock:CidrBlock}' 
describe-vpcs
done
cat .aws/config
[profile sam]
output = json
region = us-east-1
3
What does aws ec2 describe-regions --output text give you, without the cut or anything else? Anything there that doesn’t look like an AWS region name?user3456014
here is sample output for "aws ec2 describe-regions --output text" REGIONS ec2.eu-north-1.amazonaws.com opt-in-not-required eu-north-1 REGIONS ec2.ap-south-1.amazonaws.com opt-in-not-required ap-south-1 REGIONS ec2.eu-west-3.amazonaws.com opt-in-not-required eu-west-3sam

3 Answers

2
votes

If you don’t have a default profile configured, you can define the target profile with the --profile option.

aws ec2 describe-regions --profile profile-name

Another way is to set the AWS_PROFILE environment variable. This way you don’t have to explicitly add the option for every AWS CLI command.

export AWS_PROFILE=profile-name
1
votes

Seems a bug in your script. I tried the below and it worked for me.

for region in `aws ec2 describe-regions  --output text| cut -f4`
do
aws ec2 describe-vpcs --profile <myProfile>  --region $region --output text --query 'Vpcs[*].{VpcId:VpcId,CidrBlock:CidrBlock}'
done
0
votes

found the issue , need to add --profile in my first line of code as well.It works fine now.

for region in `aws ec2 describe-regions  --profile sam --output text| cut -f4