forked from gruntwork-io/terratest
/
asg.go
59 lines (49 loc) · 1.52 KB
/
asg.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
package aws
import (
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
)
// GetInstanceIdsForAsg gets the IDs of EC2 Instances in the given ASG.
func GetInstanceIdsForAsg(t *testing.T, asgName string, awsRegion string) []string {
ids, err := GetInstanceIdsForAsgE(t, asgName, awsRegion)
if err != nil {
t.Fatal(err)
}
return ids
}
// GetInstanceIdsForAsgE gets the IDs of EC2 Instances in the given ASG.
func GetInstanceIdsForAsgE(t *testing.T, asgName string, awsRegion string) ([]string, error) {
asgClient, err := NewAsgClientE(t, awsRegion)
if err != nil {
return nil, err
}
input := autoscaling.DescribeAutoScalingGroupsInput{AutoScalingGroupNames: []*string{aws.String(asgName)}}
output, err := asgClient.DescribeAutoScalingGroups(&input)
if err != nil {
return nil, err
}
instanceIDs := []string{}
for _, asg := range output.AutoScalingGroups {
for _, instance := range asg.Instances {
instanceIDs = append(instanceIDs, aws.StringValue(instance.InstanceId))
}
}
return instanceIDs, nil
}
// NewAsgClient creates an Auto Scaling Group client.
func NewAsgClient(t *testing.T, region string) *autoscaling.AutoScaling {
client, err := NewAsgClientE(t, region)
if err != nil {
t.Fatal(err)
}
return client
}
// NewAsgClientE creates an Auto Scaling Group client.
func NewAsgClientE(t *testing.T, region string) (*autoscaling.AutoScaling, error) {
sess, err := NewAuthenticatedSession(region)
if err != nil {
return nil, err
}
return autoscaling.New(sess), nil
}