-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_metadata.go
36 lines (28 loc) · 1013 Bytes
/
aws_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
package main
import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
)
type EC2Metadata struct {
Region,
SubnetID,
SubnetCIDR,
VpcID,
VpcCIDR string
}
func NewEc2Metadata() (*EC2Metadata, error) {
service := ec2metadata.New(session.New())
if !service.Available() {
return nil, errors.New("EC2 Metadata service is unavailable. Make sure the application is running within an EC2 Instance.")
}
mac, _ := service.GetMetadata("network/interfaces/macs/")
meta := &EC2Metadata{}
meta.Region, _ = service.Region()
meta.SubnetID, _ = service.GetMetadata(fmt.Sprintf("network/interfaces/macs/%s/subnet-id", mac))
meta.SubnetCIDR, _ = service.GetMetadata(fmt.Sprintf("network/interfaces/macs/%s/subnet-ipv4-cidr-block", mac))
meta.VpcID, _ = service.GetMetadata(fmt.Sprintf("network/interfaces/macs/%s/vpc-id", mac))
meta.VpcCIDR, _ = service.GetMetadata(fmt.Sprintf("network/interfaces/macs/%s/vpc-ipv4-cidr-block", mac))
return meta, nil
}