-
Notifications
You must be signed in to change notification settings - Fork 2
/
elb.go
88 lines (72 loc) · 2.2 KB
/
elb.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
package main
import (
"fmt"
"os"
"flag"
"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/elb"
)
var loadBalancerName = flag.String("name", "", "the access point name of the load balancer")
var registerOperation = flag.Bool("register", false, "register this instance in the load balancer")
var deregisterOperation = flag.Bool("deregister", false, "deregister this instance in the load balancer")
func register(instanceID string, svc *elb.ELB) {
params := &elb.RegisterInstancesWithLoadBalancerInput{
Instances: []*elb.Instance{ // Required
{ // Required
InstanceId: aws.String(instanceID),
},
// More values...
},
LoadBalancerName: aws.String(*loadBalancerName), // Required
}
_, err := svc.RegisterInstancesWithLoadBalancer(params)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
return
}
fmt.Println("Instance", instanceID, "registered.")
}
func deregister(instanceID string, svc *elb.ELB) {
params := &elb.DeregisterInstancesFromLoadBalancerInput{
Instances: []*elb.Instance{ // Required
{ // Required
InstanceId: aws.String(instanceID),
},
// More values...
},
LoadBalancerName: aws.String(*loadBalancerName), // Required
}
_, err := svc.DeregisterInstancesFromLoadBalancer(params)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
return
}
fmt.Println("Instance", instanceID, "deregistered.")
}
func main() {
flag.Parse()
session := session.New(&aws.Config{Region: aws.String("us-east-1")})
svc := elb.New(session)
metadata := ec2metadata.New(session)
instanceID, _ := metadata.GetMetadata("instance-id")
if instanceID == "unknown" {
fmt.Printf("Unable to determine AWS instance ID.\n")
os.Exit(1)
}
if *registerOperation == true && *deregisterOperation == false {
register(instanceID, svc)
os.Exit(0)
}
if *deregisterOperation == true && *registerOperation == false {
deregister(instanceID, svc)
os.Exit(0)
}
fmt.Printf("Unable to parse command flags.\n")
os.Exit(1)
}