forked from aws/aws-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_ec2_by_tag.go
38 lines (36 loc) · 912 Bytes
/
filter_ec2_by_tag.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
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"
"strings"
)
// This example will list instances with a filter
//
// Usage:
// go run filter_ec2_by_tag.go <name_filter>
func main() {
nameFilter := os.Args[1]
awsRegion := "us-east-1"
svc := ec2.New(session.New(&aws.Config{Region: aws.String(awsRegion)}))
fmt.Printf("listing instances with tag %v in: %v\n", nameFilter, awsRegion)
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag:Name"),
Values: []*string{
aws.String(strings.Join([]string{"*", nameFilter, "*"}, "")),
},
},
},
}
resp, err := svc.DescribeInstances(params)
if err != nil {
fmt.Println("there was an error listing instances in", awsRegion, err.Error())
log.Fatal(err.Error())
}
fmt.Printf("%+v\n", *resp)
}