Last active
April 7, 2017 19:07
Star
You must be signed in to star a gist
Get the list of IP addresses of instances in the same ASG, excluding this instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package config | |
import ( | |
"errors" | |
"io/ioutil" | |
"net/http" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/autoscaling" | |
"github.com/aws/aws-sdk-go/service/ec2" | |
) | |
func getASGPeers() ([]string, error) { | |
myInstanceID, err := getOwnInstanceID() | |
if err != nil { | |
return nil, err | |
} | |
return getPeersOfInstance(myInstanceID, session.Options{}) | |
} | |
func getPeersOfInstance(instanceID string, options session.Options) ([]string, error) { | |
sess, err := session.NewSessionWithOptions(options) | |
if err != nil { | |
return nil, err | |
} | |
myASG, err := getASGForInstance(sess, instanceID) | |
if err != nil { | |
return nil, err | |
} | |
instances, err := getInstancesForASG(sess, myASG) | |
if err != nil { | |
return nil, err | |
} | |
instanceIDs := make([]*string, 0, len(instances)) | |
for _, instance := range instances { | |
if *instance.InstanceId != instanceID { | |
instanceIDs = append(instanceIDs, instance.InstanceId) | |
} | |
} | |
return getInstanceIPs(sess, instanceIDs) | |
} | |
func getOwnInstanceID() (string, error) { | |
// Get my instance ID | |
res, err := http.Get("http://169.254.169.254/latest/meta-data/instance-id") | |
if err != nil { | |
return "", err | |
} | |
defer res.Body.Close() | |
myInstanceID, err := ioutil.ReadAll(res.Body) | |
if err != nil { | |
return "", err | |
} | |
return string(myInstanceID), nil | |
} | |
func getASGForInstance(sess *session.Session, instanceID string) (string, error) { | |
svc := autoscaling.New(sess) | |
asgParams := &autoscaling.DescribeAutoScalingInstancesInput{ | |
InstanceIds: []*string{ | |
aws.String(instanceID), | |
}, | |
MaxRecords: aws.Int64(1), | |
} | |
resp, err := svc.DescribeAutoScalingInstances(asgParams) | |
if err != nil { | |
return "", err | |
} | |
if len(resp.AutoScalingInstances) < 1 { | |
return "", err | |
} | |
asgName := resp.AutoScalingInstances[0].AutoScalingGroupName | |
return *asgName, nil | |
} | |
func getInstancesForASG(sess *session.Session, asgName string) ([]*autoscaling.Instance, error) { | |
asgSvc := autoscaling.New(sess) | |
instancesParams := &autoscaling.DescribeAutoScalingGroupsInput{ | |
AutoScalingGroupNames: []*string{ | |
aws.String(asgName), | |
}, | |
MaxRecords: aws.Int64(1), | |
} | |
asgResp, err := asgSvc.DescribeAutoScalingGroups(instancesParams) | |
if err != nil { | |
return nil, err | |
} | |
if len(asgResp.AutoScalingGroups) < 1 { | |
return nil, errors.New("ASG not found: " + asgName) | |
} | |
return asgResp.AutoScalingGroups[0].Instances, nil | |
} | |
func getInstanceIPs(sess *session.Session, instanceIDs []*string) ([]string, error) { | |
ec2Svc := ec2.New(sess) | |
params := &ec2.DescribeInstancesInput{ | |
InstanceIds: instanceIDs, | |
} | |
resp, err := ec2Svc.DescribeInstances(params) | |
if err != nil { | |
return nil, err | |
} | |
ips := make([]string, 0, len(instanceIDs)) | |
for _, resv := range resp.Reservations { | |
for _, resvInstance := range resv.Instances { | |
ips = append(ips, *resvInstance.PrivateIpAddress) | |
} | |
} | |
return ips, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment