Install awstools on a Mac

AWS awstools

4 min read | by Jordi Prats

awstools is a CLI tool that provides a small set of commands to manage the most commonly EC2 resources. It's installation it's not hard since it is a python script that has a list of dependencies on the requirements.txt file, but now it's even easier on Mac since there is a tap available

We can find the homebrew-awstools repository on GitHub, we can use it to install awstools using brew as follows:

$ brew install jordiprats/awstools/awstools
==> Downloading https://github.com/jordiprats/awstools/archive/refs/tags/1.0.tar.gz
==> Downloading from https://codeload.github.com/jordiprats/awstools/tar.gz/refs/tags/1.0
######################################################################## 100.0%
==> Installing awstools from jordiprats/awstools
==> python3 -m pip install -r requirements.txt
==> mkdir -p ~/awstools
==> sh ./helpers/macinstall.sh /opt/homebrew/Cellar/awstools/1.0
🍺  /opt/homebrew/Cellar/awstools/1.0: 4 files, 98.4KB, built in 8 seconds
==> Running `brew cleanup awstools`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

It will setup an alias (zsh/bash) to run awstools:

$ alias | grep aws
alias awstools='python3 /opt/homebrew/Cellar/awstools/1.0/awstools.py'

To configure it, we'll can create the ~/.awstools/config file, where we can define some defaults on it such as the default region or whether it should show the private or public IPs:

[aws]

region=us-west-2
useIP=PublicIpAddress

We can find all the different commands online or we can use the --help options:

$ awstools --help
Usage: awstools.py [OPTIONS] COMMAND [ARGS]...

Options:
  --profile TEXT  AWS profile
  --region TEXT   AWS region
  --debug         set debug mode
  --help          Show this message and exit.

Commands:
  acm          ACM related commands
  ce           Cost Explorer related commands
  ec2          EC2 related commands
  eks          EKS related commands
  elasticache  elasticache related commands
  iam          IAM related commands
  kms          KMS related commands
  rds          RDS related commands
  route53      Route53 related commands
  s3           S3 utilities
  sm           SM SecretManager related commands
  ssm          SSM Systems Manager related commands

We can use it on subcommands as well:

$ awstools ec2 --help
Usage: awstools.py ec2 [OPTIONS] COMMAND [ARGS]...

  EC2 related commands

Options:
  --help  Show this message and exit.

Commands:
  ami             EC2 AMI related commands
  asg             EC2 ASG related commands
  az              list available AZs
  cpucredits      retrieve InstanceCreditSpecifications
  cssh            multiple ssh to EC2 instances by name
  current-price
  ebs             EC2 EBS related commands
  import-keypair  import a keypair from a public key file
  instance-tags   show instance tags
  interfaces      list ENI per instance
  lb              EC2 Load Balancer related commands
  list            list EC2 running instances
  nat-gateways    list NAT Gateways
  scp             copy data from/to EC2 instance by name
  search          list EC2 running instances
  sg              EC2 SG related commands
  spot            EC2 spot instances
  ssh             ssh to a EC2 instance by name
  start           start EC2 instances by name
  stop            stop EC2 instances by name
  subnet          list subnets
  terminate       terminate EC2 instances by name

We'll find many subcomands, for example, listing EC2 instances:

$ awstools ec2 list
k3s-master                                                   32.84.207.220        i-038db699e0f9b6a22     t3a.micro     us-west-2d  2022-09-01 06:22:03+00:00    k3s-keypair
k3s-master                                                   51.11.46.225         i-059647d687ffb46ac     a1.medium     us-west-2c  2022-09-01 06:10:08+00:00    k3s-keypair
k3s-master                                                   33.163.30.226        i-02232350e1d148101     t3a.micro     us-west-2a  2022-09-01 06:24:07+00:00    k3s-keypair

Listing Auto Scaling Groups

$ awstools ec2 asg list
AutoScalingGroupName                                              DesiredCapacity              MinSize              MaxSize        InstanceCount
k3s_master_asg_arm64_bootstrap                                                  1                    1                    1                    1
k3s_master_asg_x86_64                                                           2                    2                    2                    2

Connecting to an EC2 instance using it's name (or ID):

$ awstools ec2 ssh k3s-master --any
Warning: Permanently added '31.11.227.220' (ECDSA) to the list of known hosts.

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
[ec2-user@ip-10-12-202-154 ~]$

Listing current pricing for the EC2 instance types:

$ awstools ec2 current-price t2.2xlarge
Instance Type                  OS                             Region                         OnDemand Price                 effective
t2.2xlarge                     Linux                          us-west-2                      0.3712000000                   2022-09-01T00:00:00Z
$ awstools ec2 spot current-price t2.2xlarge
Instance Type                  Product                        Availability Zone              Spot Price                     Last Update
t2.2xlarge                     Linux/UNIX (Amazon VPC)        us-west-2a                     0.130200                       2022-09-19 10:29:20+00:00
t2.2xlarge                     Linux/UNIX (Amazon VPC)        us-west-2b                     0.138000                       2022-09-19 12:03:22+00:00
t2.2xlarge                     Linux/UNIX (Amazon VPC)        us-west-2c                     0.133800                       2022-09-19 11:55:31+00:00
t2.2xlarge                     Linux/UNIX (Amazon VPC)        us-west-2d                     -                              -

Listing KMS keys:

$ awstools kms list
307ec264-68db-40dc-a6a9-88328d867223               Default key that protects my SSM parameters when no other key is defined
8e97cfb5-973b-42d2-a814-82738c803556               Default key that protects my EBS volumes when no other key is defined
df71d6f4-bffd-451d-4466-8cfc97a42413               Default key that protects my ACM private keys when no other key is defined

Creating parameters on the parameter store (SSM):

$ awstools ssm set demo value --description "test SSM"
2b7f94d4-d226-48d7-ec42-43051748c17e

Deleting a parameter is just as easy:

$ awstools ssm delete demo
HTTP 200 74579543-4dd3-4900-9b21-6b423f8375fe

And many more!


Posted on 20/09/2022

Categories