/
DeleteStack.go
71 lines (60 loc) · 2.11 KB
/
DeleteStack.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
66
67
68
69
70
71
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[cfn.go.delete_stack]
package main
// snippet-start:[cfn.go.delete_stack.imports]
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface"
)
// snippet-end:[cfn.go.delete_stack.imports]
// RemoveStack deletes an AWS CloudFormation stack
// Inputs:
// svc is an AWS CloudFormation service client
// stackName is the name of the new stack
// Output:
// If success, nil
// Otherwise, an error from the call to DeleteStack
func RemoveStack(svc cloudformationiface.CloudFormationAPI, stackName *string) error {
// snippet-start:[cfn.go.delete_stack.call]
_, err := svc.DeleteStack(&cloudformation.DeleteStackInput{
StackName: stackName,
})
// snippet-end:[cfn.go.delete_stack.call]
return err
}
func main() {
// snippet-start:[cfn.go.delete_stack.args]
stackName := flag.String("n", "", "The name of the stack to create or delete")
flag.Parse()
if *stackName == "" {
fmt.Println("You must supply a stack name (-s STACK-NAME)")
return
}
// snippet-end:[cfn.go.delete_stack.args]
// snippet-start:[cfn.go.delete_stack.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := cloudformation.New(sess)
// snippet-end:[cfn.go.delete_stack.session]
err := RemoveStack(svc, stackName)
if err != nil {
fmt.Println("Got an error deleting stack " + *stackName)
return
}
// snippet-start:[cfn.go.delete_stack.wait]
err = svc.WaitUntilStackDeleteComplete(&cloudformation.DescribeStacksInput{
StackName: stackName,
})
if err != nil {
fmt.Println("Got an error waiting for stack to be deleted")
return
}
// snippet-end:[cfn.go.delete_stack.wait]
fmt.Println("Deleted stack " + *stackName)
}
// snippet-end:[cfn.go.delete_stack]