#!/usr/bin/env ngs # Builds a section of /etc/hosts file listing EC2 instances. # Naming is based on "env" and "role" tags of the instances. ### sample output ### 172.XX.XX.XX staging-api-gateway # i-b2XXXXXX 5X.XXX.XXX.XXX api-gateway/66 ### 172.XX.XX.XX prod-compositor-1 # i-83XXXXXX 5X.XXX.XXX.XXX compositor/978-1-7 ### 172.XX.XX.XX prod-compositor-2 # i-84XXXXXX 1XX.XX.XX.XXX compositor/978-1-7 ### 172.XX.XX.XX prod-vdr-manager-1 # i-90XXXXXX 5X.XX.XX.XXX vdr-manager-vpc/683-4 ### 172.XX.XX.XX prod-vdr-manager-2 # i-97XXXXXX 5X.XX.XXX.XX vdr-manager-vpc/683-4 ### 172.XX.XX.XX staging-store # i-79XXXXXX 5X.XX.XXX.XXX store/257 ### 172.XX.XX.XX staging-dsp-manager # i-acXXXXXX 5X.X.XX.XXX dsp-manager/707-1-7 ### ... # For minimalistic output: NGS_table_ec2hosts='["PrivateIpAddress", "HostName"]' ./demo/ec2hostsfile.ngs { # TODO: sorting of the instances F HostnameBase(i:Hash) "${i.Tags.env}-${i.Tags.role}" instances = ``aws ec2 describe-instances --filters Name=instance-state-name,Values=running`` # Only look at instances with "env" and "role" tags instances = instances.filter(F(i) try HostnameBase(i)) # stats - know which instances to suffix with sequential number and which not # stats = instances.map(HostnameBase).Stats() stats = Stats(instances / HostnameBase) debug("Names stats: ${stats}") # sequential numbering for instances with same "env" and "role" tags numbering = Stats() # Preparing the table instances.each(F(i) { hostname_base = HostnameBase(i) i.HostName = if stats[hostname_base] == 1 hostname_base else { numbering.push(hostname_base) "${hostname_base}-${numbering[hostname_base]}" } i.Comment = '#' i.Name = i.Tags.get('Name', '-') }) config('table_ec2hosts', %[PrivateIpAddress HostName Comment InstanceId PublicIpAddress Name]) config('output_format', 'text') config('output_title', false) config('output_headers', false) t = Table('ec2hosts', instances) echo(t) }