-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.go
73 lines (61 loc) · 2.35 KB
/
aws.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
)
func getPublicIPsFromTargetGroup(targetGroupARN string, awsProfile string, awsAccessKeyID string, awsSecretAccessKey string) ([]string, error) {
var cfg aws.Config
var err error
if awsAccessKeyID != "" && awsSecretAccessKey != "" {
// Use static credentials if both access key and secret key are provided
cfg, err = config.LoadDefaultConfig(context.TODO(), config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(awsAccessKeyID, awsSecretAccessKey, "")))
} else if awsProfile != "" {
// Use the specified AWS profile if provided
cfg, err = config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigProfile(awsProfile))
} else {
// Load the default configuration
cfg, err = config.LoadDefaultConfig(context.TODO())
}
if err != nil {
return nil, fmt.Errorf("error loading AWS configuration: %w", err)
}
// Create an ELB client
elbv2Client := elasticloadbalancingv2.NewFromConfig(cfg)
// Describe target health to get instance IDs
targetHealthOutput, err := elbv2Client.DescribeTargetHealth(context.TODO(), &elasticloadbalancingv2.DescribeTargetHealthInput{
TargetGroupArn: aws.String(targetGroupARN),
})
if err != nil {
return nil, fmt.Errorf("error describing target health: %w", err)
}
// Extract instance IDs from the targets
instanceIDs := make([]string, len(targetHealthOutput.TargetHealthDescriptions))
for i, thd := range targetHealthOutput.TargetHealthDescriptions {
instanceIDs[i] = *thd.Target.Id
}
// Create an EC2 client
ec2Client := ec2.NewFromConfig(cfg)
// Describe instances to get public IPs
publicIPs := []string{}
if len(instanceIDs) > 0 {
describeInstancesOutput, err := ec2Client.DescribeInstances(context.TODO(), &ec2.DescribeInstancesInput{
InstanceIds: instanceIDs,
})
if err != nil {
return nil, fmt.Errorf("error describing EC2 instances: %w", err)
}
for _, reservation := range describeInstancesOutput.Reservations {
for _, instance := range reservation.Instances {
if instance.PublicIpAddress != nil {
publicIPs = append(publicIPs, *instance.PublicIpAddress)
}
}
}
}
return publicIPs, nil
}