Install and configure AWS cli

Install and enable completion

# Ubuntu
sudo apt install awscli

# .bash_profile should source .bashrc
echo complete -C '/usr/local/bin/aws_completer' aws > ~/.bashrc

You need keys to connect and configure will store them in ~/.aws/credentials

aws configure
AWS Access Key ID [None]: 123....4P5Q
AWS Secret Access Key [None]: 123...TCzr
Default region name [None]: us-east-1
Default output format [None]:

cat ~/.aws/credentials
[default]
aws_access_key_id = 123...4P5Q
aws_secret_access_key = 123...TCzr

cat ~/.aws/config
[default]
region = us-east-1

aws configure
AWS Access Key ID [**************4P5Q]:
AWS Secret Access Key [****************TCzr]:

You can have several profiles in additional to [default]. To add another profile

aws configure --profile duleorlovic
# this will add [profile duleorlovic] to ~/.aws/config
# and [duleorlovic] to ~/.aws/credentials
# you can use in terraform provider "aws" { profile = "duleorlovic" }
aws ec2 describe-vpcs --profile duleorlovic

You can check which profile you are using

# show current profile settings
aws configure list

# list all profile names
aws configure list-profile

To change profile in shell you can export

export AWS_PROFILE=duleorlovic

Another way to change profile is to use different credentials file

AWS_SHARED_CREDENTIALS_FILE=~/.aws/credentials_duleorlovic aws s3 ls s3://my-trk-bucket

You can extract data with query to parse json for example Find other examples on aws ec2 describe-instances help TODO: add examples

  • [*] array
  • .attribute you can have --output text, --output json or --output table, or you can use jq
# list all public instances, using * returns array of arrays
aws ec2 describe-instances --query "Reservations[*].Instances[*].PublicIpAddress" --output=text
# this returns inline results since it is single array
aws ec2 describe-instances --query "Reservations[].Instances[].PublicIpAddress" --output=text

You can combine columns, for example to show ARN

aws ec2 describe-instances --region us-east-1 | jq -r '.Reservations[] | .OwnerId as $OwnerId | ( .Instances[] | { "ARN": "arn:aws:ec2:\(.Placement.AvailabilityZone[:-1]):\($OwnerId):instance/\(.InstanceId)", "AvailabilityZone": "\(.Placement.AvailabilityZone)", InstanceId, PublicDnsName, PrivateDnsName, Tags} )' | jq -s .
[
  {
    "ARN": "arn:aws:ec2:us-east-1:606470370249:instance/i-0b7175eed059b8f41",
    "AvailabilityZone": "us-east-1a",
    "InstanceId": "i-0b7175eed059b8f41",
    "PublicDnsName": "ec2-34-230-46-141.compute-1.amazonaws.com",
    "PrivateDnsName": "ip-172-31-90-19.ec2.internal",
    "Tags": [
      {
        "Key": "Name",
        "Value": "test"
      }
    ]
  }
]