forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeleteSecurityGroup.go
65 lines (55 loc) · 1.9 KB
/
DeleteSecurityGroup.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[ec2.go.delete_security_group]
package main
// snippet-start:[ec2.go.delete_security_group.imports]
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)
// snippet-end:[ec2.go.delete_security_group.imports]
// RemoveSecurityGroup deletes a security group.
// Inputs:
// svc is an Amazon EC2 service client
// name is the name of the security group
// Output:
// If success, nil
// Otherwise, an error from the call to DeleteSecurityGroup
func RemoveSecurityGroup(svc ec2iface.EC2API, name *string) error {
// snippet-start:[ec2.go.delete_security_group.call]
_, err := svc.DeleteSecurityGroup(&ec2.DeleteSecurityGroupInput{
GroupName: name,
})
// snippet-end:[ec2.go.delete_security_group.call]
if err != nil {
return err
}
return nil
}
func main() {
// snippet-start:[ec2.go.delete_security_group.args]
name := flag.String("n", "", "The name of the security group to delete")
flag.Parse()
if *name == "" {
fmt.Println("You must supply the name of the security group to delete")
return
}
// snippet-end:[ec2.go.delete_security_group.args]
// snippet-start:[ec2.go.delete_security_group.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := ec2.New(sess)
// snippet-end:[ec2.go.delete_security_group.session]
err := RemoveSecurityGroup(svc, name)
if err != nil {
fmt.Println("Got an error deleting security group:")
fmt.Println(err)
return
}
fmt.Println("Deleted security group with ID " + *name)
}
// snippet-end:[ec2.go.delete_security_group]