-
Notifications
You must be signed in to change notification settings - Fork 6
/
metadata.go
44 lines (37 loc) · 1.14 KB
/
metadata.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
package ec2
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/gogo/status"
"google.golang.org/grpc/codes"
)
var (
instanceFilters = []types.Filter{
{
Name: aws.String("instance-state-name"),
Values: []string{
"pending",
"running",
},
},
}
)
func QueryInstanceMetadata(ctx context.Context, c *ec2.Client, instanceIds []string) (*types.Instance, error) {
instancesDesc, err := c.DescribeInstances(context.Background(), &ec2.DescribeInstancesInput{
InstanceIds: instanceIds,
Filters: instanceFilters,
})
if err != nil {
return nil, fmt.Errorf("ec2 describe instances failed, %s", err)
}
if len(instancesDesc.Reservations) < 1 {
return &types.Instance{}, status.Error(codes.Internal, "failed to query AWS via describe-instances: returned no reservations")
}
if len(instancesDesc.Reservations[0].Instances) < 1 {
return &types.Instance{}, status.Error(codes.Internal, "failed to query AWS via describe-instances: returned no instances")
}
return &instancesDesc.Reservations[0].Instances[0], nil
}