forked from discobean/aws-ec2-assign-elastic-ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (93 loc) · 2.77 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"flag"
"fmt"
"log"
"os"
"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/jtheo/aws-ec2-assign-elastic-ip/assigner"
)
var Version = "v0.0"
func main() {
var tagKey string
var tagValue string
var instanceID string
var region string
var ver bool
flag.StringVar(&tagKey, "tag-name", "", "EIP Pool Tag Key (required)")
flag.StringVar(&tagValue, "tag-value", "", "EIP Pool Tag Value (required)")
flag.StringVar(&instanceID, "instanceid", "", "Instance ID to set (optional, if empty use metadata service)")
flag.StringVar(®ion, "region", "", "AWS Region (optional, if empty use metadata service)")
flag.BoolVar(&ver, "version", false, "show the version")
flag.Parse()
if ver {
fmt.Printf("aws-ec2-assign-elastic-ip version: %s\n", Version)
return
}
if tagKey == "" {
log.Println("tag-name required")
flag.Usage()
os.Exit(5)
}
if tagValue == "" {
log.Println("tag-name required")
flag.Usage()
os.Exit(6)
}
awsSession, err := session.NewSession(&aws.Config{})
if err != nil {
log.Printf("Failed to create AWS session: %v\n", err)
os.Exit(1)
}
// get the instance ID information if not specified, using the metadata service
if instanceID == "" {
metadataSvc := ec2metadata.New(awsSession)
if !metadataSvc.Available() {
log.Println("No instance metadata available")
os.Exit(2)
}
instanceIdentity, err := metadataSvc.GetInstanceIdentityDocument()
if err != nil {
log.Printf("Failed to get instance identity document: %v\n", err)
os.Exit(3)
}
log.Printf("Got instance ID: %v\n", instanceIdentity.InstanceID)
instanceID = instanceIdentity.InstanceID
}
// get the region information if not specified, using the metadata service
if region == "" {
metadataSvc := ec2metadata.New(awsSession)
if !metadataSvc.Available() {
log.Println("No instance metadata available")
os.Exit(2)
}
instanceIdentity, err := metadataSvc.GetInstanceIdentityDocument()
if err != nil {
log.Printf("Failed to get instance identity document: %v\n", err)
os.Exit(3)
}
log.Printf("Got region: %v\n", instanceIdentity.Region)
region = instanceIdentity.Region
}
// create a new session but specify the correct region information now
awsSession, err = session.NewSession(&aws.Config{
Region: aws.String(region),
})
if err != nil {
log.Printf("Failed to create AWS session with region: %v\n", err)
os.Exit(7)
}
assignerSvc, err := assigner.New(awsSession)
if err != nil {
log.Printf("Failed to create new EIP assigner: %v\n", err)
os.Exit(8)
}
result, err := assignerSvc.AssignEIPFromPoolUsingTags(instanceID, tagKey, tagValue)
if err != nil {
log.Printf("Association failed: %v\n", err)
os.Exit(9)
}
fmt.Println(result)
}