-
Notifications
You must be signed in to change notification settings - Fork 21
/
ec2.go
43 lines (36 loc) · 905 Bytes
/
ec2.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
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
type ec2Client struct {
ec2 *ec2.EC2
}
func newEC2(region string, sess *session.Session) *ec2Client {
return &ec2Client{
ec2: ec2.New(sess, aws.NewConfig().WithRegion(region)),
}
}
func (e *ec2Client) describeInstance(id *string) (*ec2.Instance, error) {
params := &ec2.DescribeInstancesInput{
InstanceIds: []*string{
id,
},
}
resp, err := e.ec2.DescribeInstances(params)
if err != nil {
return nil, err
}
return resp.Reservations[0].Instances[0], nil
}
type ec2Meta struct {
metadata *ec2metadata.EC2Metadata
}
func NewEC2Metadata(sess *session.Session) *ec2Meta {
return &ec2Meta{metadata: ec2metadata.New(sess)}
}
func (m *ec2Meta) region() (string, error) {
return m.metadata.Region()
}