-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
DescribeSecurityGroups.go
59 lines (50 loc) · 2.04 KB
/
DescribeSecurityGroups.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[ec2.go.describe_security_groups]
package main
// snippet-start:[ec2.go.describe_security_groups.imports]
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
// snippet-end:[ec2.go.describe_security_groups.imports]
// GetSecurityGroupInfo retrieves information about your Amazon Elastic Compute Cloud (Amazon EC2) security groups.
// Inputs:
// sess is the current session, which provides configuration for the SDK's service clients
// Output:
// If success, a list of the Amazon EC2 security groups and nil
// Otherwise, nil and an error from the call to DescribeSecurityGroups
func GetSecurityGroupInfo(sess *session.Session) (*ec2.DescribeSecurityGroupsOutput, error) {
// snippet-start:[ec2.go.describe_security_groups.call]
svc := ec2.New(sess)
result, err := svc.DescribeSecurityGroups(nil)
// snippet-end:[ec2.go.describe_security_groups.call]
if err != nil {
return nil, err
}
return result, nil
}
func main() {
// snippet-start:[ec2.go.describe_security_groups.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// snippet-end:[ec2.go.describe_security_groups.session]
result, err := GetSecurityGroupInfo(sess)
if err != nil {
fmt.Println("Got an error retrieving information about your security groups:")
fmt.Println(err)
return
}
// snippet-start:[ec2.go.describe_security_groups.display]
for _, group := range result.SecurityGroups {
fmt.Println(*group.GroupName)
fmt.Println(" Description: " + *group.Description)
fmt.Println(" Group ID: " + *group.GroupId)
fmt.Println("")
}
fmt.Println("Found", len(result.SecurityGroups), "security groups")
// snippet-end:[ec2.go.describe_security_groups.display]
}
// snippet-end:[ec2.go.describe_security_groups]