|
|
|
@ -46,17 +46,26 @@ func (p *Provider) Addrs(args map[string]string, l *log.Logger) ([]string, error
|
|
|
|
|
accessKey := args["access_key_id"]
|
|
|
|
|
secretKey := args["secret_access_key"]
|
|
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] discover-aws: Using region=%s tag_key=%s tag_value=%s", region, tagKey, tagValue)
|
|
|
|
|
if accessKey == "" && secretKey == "" {
|
|
|
|
|
log.Printf("[DEBUG] discover-aws: No static credentials")
|
|
|
|
|
log.Printf("[DEBUG] discover-aws: Using environment variables, shared credentials or instance role")
|
|
|
|
|
} else {
|
|
|
|
|
log.Printf("[DEBUG] discover-aws: Static credentials provided")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if region == "" {
|
|
|
|
|
l.Printf("[INFO] discover-aws: Looking up region")
|
|
|
|
|
l.Printf("[INFO] discover-aws: Region not provided. Looking up region in metadata...")
|
|
|
|
|
ec2meta := ec2metadata.New(session.New())
|
|
|
|
|
identity, err := ec2meta.GetInstanceIdentityDocument()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("discover-aws: %s", err)
|
|
|
|
|
return nil, fmt.Errorf("discover-aws: GetInstanceIdentityDocument failed: %s", err)
|
|
|
|
|
}
|
|
|
|
|
region = identity.Region
|
|
|
|
|
}
|
|
|
|
|
l.Printf("[INFO] discover-aws: Region is %s", region)
|
|
|
|
|
|
|
|
|
|
l.Printf("[DEBUG] discover-aws: Creating session...")
|
|
|
|
|
svc := ec2.New(session.New(), &aws.Config{
|
|
|
|
|
Region: ®ion,
|
|
|
|
|
Credentials: credentials.NewChainCredentials(
|
|
|
|
@ -73,29 +82,38 @@ func (p *Provider) Addrs(args map[string]string, l *log.Logger) ([]string, error
|
|
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
l.Printf("[INFO] discover-aws: Filter instances with %s=%s", tagKey, tagValue)
|
|
|
|
|
resp, err := svc.DescribeInstances(&ec2.DescribeInstancesInput{
|
|
|
|
|
Filters: []*ec2.Filter{
|
|
|
|
|
{
|
|
|
|
|
&ec2.Filter{
|
|
|
|
|
Name: aws.String("tag:" + tagKey),
|
|
|
|
|
Values: []*string{aws.String(tagValue)},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("discover-aws: %s", err)
|
|
|
|
|
return nil, fmt.Errorf("discover-aws: DescribeInstancesInput failed: %s", err)
|
|
|
|
|
}
|
|
|
|
|
l.Printf("[INFO] discover-aws: Filter instances by %s=%s", tagKey, tagValue)
|
|
|
|
|
|
|
|
|
|
l.Printf("[DEBUG] discover-aws: Found %d reservations", len(resp.Reservations))
|
|
|
|
|
var addrs []string
|
|
|
|
|
for i := range resp.Reservations {
|
|
|
|
|
for _, inst := range resp.Reservations[i].Instances {
|
|
|
|
|
for _, r := range resp.Reservations {
|
|
|
|
|
l.Printf("[DEBUG] discover-aws: Reservation %s has %d instances", *r.ReservationId, len(r.Instances))
|
|
|
|
|
for _, inst := range r.Instances {
|
|
|
|
|
id := *inst.InstanceId
|
|
|
|
|
l.Printf("[DEBUG] discover-aws: Found instance %s", id)
|
|
|
|
|
|
|
|
|
|
// Terminated instances don't have the PrivateIpAddress field
|
|
|
|
|
if inst.PrivateIpAddress == nil {
|
|
|
|
|
l.Printf("[DEBUG] discover-aws: Instance %s has no private ip", id)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
l.Printf("[INFO] discover-aws: Found %x", *inst.PrivateIpAddress)
|
|
|
|
|
|
|
|
|
|
l.Printf("[INFO] discover-aws: Instance %s has private ip %s", id, *inst.PrivateIpAddress)
|
|
|
|
|
addrs = append(addrs, *inst.PrivateIpAddress)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
l.Printf("[DEBUG] discover-aws: Found ip addresses: %v", addrs)
|
|
|
|
|
return addrs, nil
|
|
|
|
|
}
|
|
|
|
|