awsops
is a simplified CLI utility to help you interact with your AWS infrastructure.
$ npm install -g awsops
List all EC2 instances with a particular name:
$ awsops ls --name prod-serv
┌────────────┬───────────┬─────────────────┬──────────────────┬──────────────┬─────────┐
│ InstanceId │ Name │ PublicIpAddress │ PrivateIpAddress │ LaunchTime │ State │
├────────────┼───────────┼─────────────────┼──────────────────┼──────────────┼─────────┤
│ i-218369c8 │ prod-serv │ 54.185.61.256 │ 172.30.17.218 │ 20 hours ago │ running │
├────────────┼───────────┼─────────────────┼──────────────────┼──────────────┼─────────┤
│ i-768fa4e6 │ prod-serv │ 107.24.256.132 │ 172.21.8.65 │ 20 hours ago │ running │
└────────────┴───────────┴─────────────────┴──────────────────┴──────────────┴─────────┘
SSH into a particular EC2 instance:
$ awsops ssh --name prod-serv
┌────┬────────────┬───────────┬─────────────────┬──────────────┬─────────┐
│ ID │ InstanceId │ Name │ PublicIpAddress │ LaunchTime │ State │
├────┼────────────┼───────────┼─────────────────┼──────────────┼─────────┤
│ #1 │ i-218369c8 │ prod-serv │ 54.185.61.256 │ 20 hours ago │ running │
├────┼────────────┼───────────┼─────────────────┼──────────────┼─────────┤
│ #2 │ i-768fa4e6 │ prod-serv │ 107.24.256.132 │ 20 hours ago │ running │
└────┴────────────┴───────────┴─────────────────┴──────────────┴─────────┘
[awsops] Which server would you like to connect to? [1..2] 1
[awsops] Connecting to prod-serv (i-218369c8) (with prod-serv-key) ...
Warning: Permanently added '54.185.61.256' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-48-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Thu Aug 4 11:45:28 UTC 2016
System load: 0.13 Processes: 104
Usage of /: 24.2% of 7.74GB Users logged in: 0
Memory usage: 43% IP address for eth0: 172.30.17.218
Swap usage: 0%
Graph this data and manage this system at:
https://landscape.canonical.com/
Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud
121 packages can be updated.
82 updates are security updates.
New release '16.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Thu Aug 4 11:28:26 2016 from 172.21.8.65
ubuntu@ip-172-31-17-218:~$ # Welcome to your EC2 instance
$ awsops --help
Usage: awsops <command> [options]
Commands:
ls List EC2 instances
ssh SSH into an EC2 instance
s3cp Download/upload a file from/to an S3 bucket
Options:
--auth Optionally use a specified file for authentication
--id When using ls or ssh, optionally filter by an EC2 instance ID
--name When using ls or ssh, optionally filter by an EC2 instance's name
--only When using ls or ssh, optionally return a comma-separated list of fields instead of rendering a table
--security-group When using ls or ssh, optionally filter by a security group
--help Show help [boolean]
-q, --quiet When using s3cp, do not use the progress bar [boolean]
-v, --verbose Print info/debug statements [count]
Examples:
awsops ls --security-group example-group
awsops ssh --name instance-name
awsops s3cp archive.tar.gz s3://my-archive-bucket/archive-2016-08-12.tar.gz
Got questions? Check out https://github.com/car-throttle/awsops/
$ awsops ls [filters]
Lists your instances, after filtering using --id
, --name
or --security-group
.
You can also use --only
to print an individual property of the instances, which can be useful in scripts. Here's an
example of looping through a series of IPs during deployment:
$ awsops ls --name prod-serv --only PublicIpAddress
54.185.61.256 107.24.256.132
# In deploy.sh:
PROD_SERVS=$(awsops ls --name prod-serv --only PublicIpAddress)
if [ "$?" -ne "0" ]; then
echo "Failed to fetch the IPs for prod-serv"
exit 1
fi
COUNT=$(echo $PROD_SERVS | wc -w)
echo "Deploying to $COUNT prod-serv instances"
for IP in $PROD_SERVS; do
ssh ubuntu@$IP -i /path/to/instance_rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null <<'ENDSSH'
cd /var/app/my-application
git pull
pm2 restart all
ENDSSH
done
$ awsops ssh [filters] [-- [arguments]]
Connect to your instances, after filtering using --id
, --name
or --security-group
. You can either omit any
arguments, which starts an interactive SSH session as you'd expect, or you can provide arguments to run a set of
commands on each instance one after the other. For example, to check the status of a Node application on each instance
using pm2
:
$ awsops ssh --name prod-serv -- pm2 status
┌────────────┬───────────┬─────────────────┬──────────────────┬──────────────┬─────────┐
│ InstanceId │ Name │ PublicIpAddress │ PrivateIpAddress │ LaunchTime │ State │
├────────────┼───────────┼─────────────────┼──────────────────┼──────────────┼─────────┤
│ i-218369c8 │ prod-serv │ 54.185.61.256 │ 172.30.17.218 │ 20 hours ago │ running │
├────────────┼───────────┼─────────────────┼──────────────────┼──────────────┼─────────┤
│ i-768fa4e6 │ prod-serv │ 107.24.256.132 │ 172.21.8.65 │ 20 hours ago │ running │
└────────────┴───────────┴─────────────────┴──────────────────┴──────────────┴─────────┘
[awsops] Connecting to prod-serv (i-218369c8) (with prod-serv-key) ...
┌──────────────────────┬────┬─────────┬──────┬────────┬─────────┬────────┬──────────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ memory │ watching │
├──────────────────────┼────┼─────────┼──────┼────────┼─────────┼────────┼──────────────┼──────────┤
│ prod-serv-production │ 0 │ cluster │ 6874 │ online │ 1 │ 71m │ 242.832 MB │ disabled │
└──────────────────────┴────┴─────────┴──────┴────────┴─────────┴────────┴──────────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
[awsops] Connecting to prod-serv (i-768fa4e6) (with prod-serv-key) ...
┌──────────────────────┬────┬─────────┬──────┬────────┬─────────┬────────┬──────────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ memory │ watching │
├──────────────────────┼────┼─────────┼──────┼────────┼─────────┼────────┼──────────────┼──────────┤
│ prod-serv-production │ 0 │ cluster │ 6438 │ online │ 1 │ 70m │ 454.000 MB │ disabled │
└──────────────────────┴────┴─────────┴──────┴────────┴─────────┴────────┴──────────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
$ awsops s3cp [from] [to]
Download from, or upload to, an S3 bucket of your choice. There are a few shortcuts to make it easier to download/upload files in quick succession:
awsops s3cp s3://bucket/path/to/s3-item.txt
=> Download to CURRENT_WORKING_DIRECTORY/s3-item.txt
awsops s3cp s3://bucket/path/to/s3-item.txt folder/something.txt
=> Download to CURRENT_WORKING_DIRECTORY/folder/something.txt
awsops s3cp file.txt s3://bucket/
=> Upload file.txt to s3://bucket/file.txt
awsops s3cp file.txt s3://bucket/somefile.txt
=> Upload file.txt to s3://bucket/somefile.txt
Since this uses aws-sdk
behind the scenes, you can configure your own ~/.aws/credentials
file or load from environment variables as described in the Amazon AWS-SDK Node-JS docs. Or you
can load your own variables by passing a file to --auth
like so:
$ awsops --auth ~/path/to/auth/file.json <command> [options]
You can pass a .ini
, .json
, .js
, .yml
file. Whichever format you prefer.
[ec2]
accessKeyId = XXXXXXXXXXXXXXXXXXXX
secretAccessKey = YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
region = us-east-1
[s3]
accessKeyId = XXXXXXXXXXXXXXXXXXXX
secretAccessKey = YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
[ssh]
keys[prod-serv-key] = ~/src/aws/prod-serv-key.pem
{
"ec2": {
"accessKeyId": "XXXXXXXXXXXXXXXXXXXX",
"secretAccessKey": "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY",
"region": "us-east-1"
},
"s3": {
"accessKeyId": "XXXXXXXXXXXXXXXXXXXX",
"secretAccessKey": "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
},
"ssh": {
"keys": {
"prod-serv-key": "~/src/aws/prod-serv-key.pem"
}
}
}
aws:
profile: someimportantcompany
region: eu-west-2
ec2:
accessKeyId: XXXXXXXXXXXXXXXXXXXX
secretAccessKey: YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
region: us-east-1
s3:
accessKeyId: XXXXXXXXXXXXXXXXXXXX
secretAccessKey: YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
ssh:
keys:
prod-serv-key: ~/src/aws/prod-serv-key.pem
# proxycommand: 'ssh vpn.someimportantcompany.com nc %h %p 2> /dev/null' # Optionally, if you want to proxy the SSH commands
Key | Description |
---|---|
aws |
A config object for globally configuring AWS SDK instances |
ec2 |
A config object used when creating EC2 SDK instances |
s3 |
A config object used when creating S3 SDK instances |
ssh |
A config object used when invoking SSH connections |
For ec2
& s3
, these are the same config objects that you'd pass when using the EC2/S3 objects in your own code.
The aws
options available are:
Property | Description |
---|---|
profile |
Optionally set the AWS profile used from the shared credentials file |
region |
If you're not using either ec2 or s3 then you must specify a region here |
The ssh
options available are:
Property | Description |
---|---|
keys |
A key-value dictionary where you can define paths to your keys |
user |
Optionally you can override the username field used in SSH connections (defaults to ubuntu ) |
proxycommand |
If you want to proxy your SSH commands, enter your ProxyCommand here |
- When logging into an instance with SSH, you'll notice the line
Warning: Permanently added '54.185.61.256' (ECDSA) to the list of known hosts
. This is due to this script negating the call to a valid known_hosts file in order to stop your known_hosts becoming overrun with the hundreds of EC2 instances that you're likely to see thanks to autoscaling. You'll see this every time, so don't panic 😉 - Questions? Awesome! Open an issue to get started!