Skip to main content

aws-gate - AWS SSM Session Manager client CLI

Project description

aws-gate

Build StatusCode style: blackcodecovCodacy BadgePyPI versionPyPI - Downloads

AWS SSM Session manager client

Documentation

Motivation

I am using AWS a lot and I am tired of dealing with everything that comes with the bastion host (additional instance one has to maintain, distribute SSH keys (shared SSH keys are not an option for me), exposing SSH to the network). A while ago, Amazon released a service to fix this - AWS Systems Manager Session Manager. However, CLI user experience of Session Manager is limited and lacks some features:

  • ability to connect to instances by other means (e.g. DNS, IP, tag, instance name, autoscaling group) as aws cli supports only connecting by instance IDs
  • configuration file support for storing connection information via Session Manager

aws-gate tries to address these issues.

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

  • Python 3.5+ (earlier Python 3 versions should work too)
  • session-plugin-manager from AWS
  • SSM Agent version 2.3.68.0 or later must be installed on EC2 instances we want to connect to
  • Proper IAM permissions for instance profile

Installing

pip install aws-gate

or via Homebrew

brew tap xen0l/homebrew-taps
brew install aws-gate

or via Docker

docker login docker.pkg.github.com -u $YOUR_GH_USERNAME -p $GH_TOKEN
docker pull docker.pkg.github.com/xen0l/aws-gate/aws-gate:latest

Features

config and config.d support

You can store information about to connect to your instance (name, region and profile) and aws-gate will do everything for you. The config file is stored in ~/.aws-gate/config and has the following YAML syntax:

hosts:
  - alias: backend-pre
    name: backend
    profile: preproduction
    region: eu-west-1
  - alias: backend-pro
    name: backend
    profile: production
    region: eu-west-1

defaults:
  profile: development
  region: eu-west-1

where hosts stores connection information and defaults default configuration settings to use. To connect to instance backend-pre, execute:

aws-gate session backend-pre

You can place additional configuration files in ~/.aws-gate/config.d. This is ideal when you are working on different projects or when you need to share configuration inside your team.

Querying instances by different instance identifiers

aws-gate supports querying for instances with following identifiers:

  • instance id
aws-gate session i-0772e4c1dcdd763b6
  • DNS name
aws-gate session ec2-34-245-174-132.eu-west-1.compute.amazonaws.com
  • private DNS name
aws-gate session ip-172-31-35-113.eu-west-1.compute.internal
  • IP address
aws-gate session 34.245.174.13
  • private IP address
aws-gate session 172.31.35.113
  • tags
aws-gate session Name:SSM-test
  • name (uses tag identifier under the hood)
aws-gate session SSM-test
  • autoscaling group name (uses tag identifier under the hood)
aws-gate session asg:dummy-v001

SSH ProxyCommand support

AWS SSM Session Manager supports tunneling SSH sessions over it. Moreover, aws-gate supports generating ephemeral SSH keys and uploading them via EC2 Instance Connect API. However, to use this functionality, EC2 Instance Connect setup is needed.

To use this functionality, simply run aws-gate ssh-config, which will generate the required ~/.ssh/config snippet for you:

% aws-gate ssh-config
Host *.eu-west-1.default
IdentityFile /Users/xenol/.aws-gate/key
IdentitiesOnly yes
User ec2-user
Port 22
ProxyCommand sh -c "aws-gate ssh-proxy -p `echo %h | sed -Ee 's/^(.*)\.(.*)\.(.*)$/\\3/g'` -r `echo %h | sed -Ee 's/^(.*)\.(.*)\.(.*)$/\\2/g'` `echo %h | sed -Ee 's/^(.*)\.(.*)\.(.*)$/\\1/g'`"

Store the snippet inside _~/.ssh/config:

% aws-gate ssh-config >> ~/.ssh/config

Then connect via ssh:

% ssh ssm-test.eu-west-1.default
Last login: Fri Oct  4 17:17:02 2019 from localhost

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

https://aws.amazon.com/amazon-linux-2/
1 package(s) needed for security, out of 20 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-172-31-35-173 ~]$

SSH session to instance ssm-test in eu-west-1 AWS region via default AWS profile is opened.

SSH support

aws-gate provides a way to open SSH session on the instance directly. This is achieved by wrapping around ssh under the hood. Simply run aws-gate ssh <instance_identifier>:

% aws-gate ssh ssm-test
Last login: Sat Nov  9 10:23:11 2019 from localhost

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

https://aws.amazon.com/amazon-linux-2/
28 package(s) needed for security, out of 56 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-172-31-35-173 ~]$

If you wish to execute a specific command (or plug it into your shell pipelines):

% aws-gate ssh ssm-test uname -a
Linux ip-172-31-35-173.eu-west-1.compute.internal 4.14.123-111.109.amzn2.x86_64 #1 SMP Mon Jun 10 19:37:57 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Debugging mode

If you run into issues, you can get detailed debug log by setting GATE_DEBUG environment variable:

export GATE_DEBUG=1

After setting the environment variable, the debug mode will be automatically enabled:

% aws-gate session test
2019-05-26 01:18:23,535 - aws_gate.config  - DEBUG - Located config file: /Users/xenol/.aws-gate/config
2019-05-26 01:18:23,538 - aws_gate.utils   - DEBUG - Obtaining boto3 session object
2019-05-26 01:18:23,549 - aws_gate.utils   - DEBUG - Obtained configured AWS profiles: default development preproduction production
2019-05-26 01:18:23,550 - aws_gate.utils   - DEBUG - Obtaining boto3 session object
2019-05-26 01:18:23,560 - aws_gate.utils   - DEBUG - Obtained configured AWS profiles: default development preproduction production
2019-05-26 01:18:23,560 - aws_gate.utils   - DEBUG - Obtaining boto3 session object
2019-05-26 01:18:23,574 - aws_gate.utils   - DEBUG - Obtaining ssm client
2019-05-26 01:18:23,608 - aws_gate.utils   - DEBUG - Obtaining boto3 session object
2019-05-26 01:18:23,636 - aws_gate.utils   - DEBUG - Obtaining ec2 boto3 resource
2019-05-26 01:18:23,694 - aws_gate.query   - DEBUG - Querying EC2 API for instance identifier: SSM-test
2019-05-26 01:18:24,029 - aws_gate.query   - DEBUG - Found 1 maching instances
2019-05-26 01:18:24,030 - aws_gate.query   - DEBUG - Matching instance: i-0772e4c1dcdd763b6
2019-05-26 01:18:24,030 - aws_gate.session - INFO  - Opening session on instance i-0772e4c1dcdd763b6 (eu-west-1) via profile default
2019-05-26 01:18:24,030 - aws_gate.session - DEBUG - Creating a new session on instance: i-0772e4c1dcdd763b6 (eu-west-1)
...

Debug mode also enables printing of Python stack traces if there is a crash or some other problem.

License

This project is licensed under the BSD License - see the LICENSE.md file for details

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws-gate-0.8.6.tar.gz (46.5 kB view details)

Uploaded Source

Built Distribution

aws_gate-0.8.6-py3-none-any.whl (38.2 kB view details)

Uploaded Python 3

File details

Details for the file aws-gate-0.8.6.tar.gz.

File metadata

  • Download URL: aws-gate-0.8.6.tar.gz
  • Upload date:
  • Size: 46.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for aws-gate-0.8.6.tar.gz
Algorithm Hash digest
SHA256 0a9738200851b9d78de94544792009fff954c47f7d567d73da9efe8dd226606d
MD5 f32e5ecf6347a74cd95e9788fde5730d
BLAKE2b-256 53f31702aafc92761f872b2a47854fc2f4402e0ebde6ad82862cc40bfae1bdc9

See more details on using hashes here.

File details

Details for the file aws_gate-0.8.6-py3-none-any.whl.

File metadata

  • Download URL: aws_gate-0.8.6-py3-none-any.whl
  • Upload date:
  • Size: 38.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for aws_gate-0.8.6-py3-none-any.whl
Algorithm Hash digest
SHA256 3653d4287f9dd9e15f5c214a488fd95d2d2047ab497a372b0e55d934d175da9a
MD5 e3c719972bfcca20eb637476d817131d
BLAKE2b-256 bc7d898a1959058cc8959c5ba5b1d2c6e84eb750c7aceb30dde5f63f17579239

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page