-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_ec2_ip.go
47 lines (43 loc) · 1.05 KB
/
get_ec2_ip.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
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"log"
"os"
)
func main() {
if len(os.Args) != 3 {
log.Fatal("Bad arguments")
}
regionName := os.Args[1]
instanceName := os.Args[2]
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String(regionName),
}))
svc := ec2.New(sess)
tagValues := []*string{aws.String(instanceName)}
filters := []*ec2.Filter{&ec2.Filter{
Name: aws.String("tag-value"),
Values: tagValues,
}}
params := &ec2.DescribeInstancesInput{
Filters: filters,
}
describeInstancesOutput, err := svc.DescribeInstances(params)
if err != nil {
log.Fatalf("DescribeInstances Error: %+v\n", err)
}
for _, reservation := range describeInstancesOutput.Reservations {
for _, instance := range reservation.Instances {
for _, tag := range instance.Tags {
if *tag.Key == "Name" && *tag.Value == instanceName {
fmt.Println(*instance.PrivateIpAddress)
os.Exit(0)
}
}
}
}
log.Fatal("Instance not found")
}