-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_running_instances.go
52 lines (45 loc) · 1.23 KB
/
get_running_instances.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
/**
* Retrieve list of running instances.
* Don't hard-code your credentials!
* Export the following environment variables instead:
*
* export AWS_ACCESS_KEY_ID='AKID'
* export AWS_SECRET_ACCESS_KEY='SECRET'
*
* This example loads credentials from ~/.aws/credentials:
* [default]
* aws_access_key_id = ...
* aws_secret_access_key = ...
*/
package main
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/service/ec2"
)
func main() {
defaults.DefaultConfig.Region = aws.String("us-west-2")
svc := ec2.New(nil)
var filters = []*ec2.Filter{
&ec2.Filter{
Name: aws.String("instance-state-name"),
Values: []*string{aws.String("running")},
},
}
request := ec2.DescribeInstancesInput{Filters: filters}
result, err := svc.DescribeInstances(&request)
if err != nil {
log.Fatalf("Error getting description: %s", err)
}
for _, reservation := range result.Reservations {
for _, instance := range reservation.Instances {
var id = *instance.InstanceId
var publicIp = *instance.PublicIpAddress
var privateIp = *instance.PrivateIpAddress
var keyName = *instance.KeyName
fmt.Printf("%s\t%s\t%s\t%s\n", id, publicIp, privateIp, keyName)
}
}
}