Skip to content

Update your hosts file with ec2 instance metadata

Notifications You must be signed in to change notification settings

atongen/ec2hosts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ec2hosts

Queries AWS EC2 api and updates your local host file using instance name tag and public/private IP address.

Intended for use with ssh ProxyCommand or sshuttle.

Requires a valid AWS IAM access key and secret with permission to read EC2 metadata configured in ~/.aws/credentials.

Note: This is written in go. There is a similar ruby gem called ec2_hosts.

install

Download the latest release, extract it, and put it somewhere on your PATH.

or

λ go get github.com/atongen/ec2hosts

or

λ mkdir -p $GOPATH/src/github.com/atongen
λ cd $GOPATH/src/github.com/atongen
λ git clone https://github.com/atongen/ec2hosts.git
λ cd ec2hosts
λ go install
λ rehash

cli options

λ ec2hosts -h
Usage of ec2hosts:
  -action string
    	Action to perform: 'update', 'delete', or 'delete-all' (default "update")
  -backup int
    	Number of backup files to keep, 0 is no backup (default 3)
  -dry-run
    	Print updated file content to stdout only
  -exclude string
    	Pattern of hostname to exclude
  -file string
    	Path to file to update (default "/etc/hosts")
  -name string
    	Name of block of hosts in file
  -public string
    	Pattern to use to match public hosts
  -region string
    	AWS Region
  -tag value
    	Add instance tag filters, should be of the form -tag 'key:value'
  -tag-out value
    	Include value for tag in host file output, should be of the form -tag 'key'
  -v	Print version information and exit
  -vpc-id string
    	Filter EC2 instances by vpc-id

example

λ ec2hosts -name my-app -dry-run -public bastion -region us-west-2
127.0.0.1 localhost

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

# START EC2HOSTS - my-app #
55.55.55.55 my-app-bastion # i-xxxxxxxx t2.micro
172.60.0.125 my-app-production-web # i-xxxxxxxx c4.xlarge
172.60.0.126 my-app-production-job # i-xxxxxxxx c4.xlarge
172.60.0.127 my-app-production-cron # i-xxxxxxxx c4.xlarge
172.60.0.128 my-app-production-db # i-xxxxxxxx c4.xlarge
# END EC2HOSTS - my-app #

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/atongen/ec2hosts.