forked from awsdocs/aws-doc-sdk-examples
/
CfnCrudOps.go
156 lines (129 loc) · 4.27 KB
/
CfnCrudOps.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"flag"
"fmt"
"io/ioutil"
"github.com/google/uuid"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
)
// CreateStack creates a CloudFormation stack
func CreateStack(sess *session.Session, stackName string, template string) error {
svc := cloudformation.New(sess)
// Open file template
// Get entire file as a string
content, err := ioutil.ReadFile(template)
if err != nil {
return err
}
// Convert []byte to string
templateBody := string(content)
input := &cloudformation.CreateStackInput{TemplateBody: aws.String(templateBody), StackName: aws.String(stackName)}
_, err = svc.CreateStack(input)
if err != nil {
return err
}
// Wait until stack is created
desInput := &cloudformation.DescribeStacksInput{StackName: aws.String(stackName)}
return svc.WaitUntilStackCreateComplete(desInput)
}
// GetStackSummaries gets a list of summary information about all stacks or those with status
func GetStackSummaries(sess *session.Session, status string) ([]*cloudformation.StackSummary, error) {
svc := cloudformation.New(sess)
var stackSummaries []*cloudformation.StackSummary
var filter []*string
if status != "all" {
filter = append(filter, aws.String(status))
}
input := &cloudformation.ListStacksInput{StackStatusFilter: filter}
resp, err := svc.ListStacks(input)
if err != nil {
return stackSummaries, err
}
return resp.StackSummaries, nil
}
// DeleteStack deletes the specified stack
func DeleteStack(sess *session.Session, name string) error {
svc := cloudformation.New(sess)
delInput := &cloudformation.DeleteStackInput{StackName: aws.String(name)}
_, err := svc.DeleteStack(delInput)
if err != nil {
return err
}
// Wait until stack is created
desInput := &cloudformation.DescribeStacksInput{StackName: aws.String(name)}
return svc.WaitUntilStackDeleteComplete(desInput)
}
func main() {
operationPtr := flag.String("o", "", "The operation to perform: create, list, delete, or all in that order")
stackNamePtr := flag.String("n", "", "The name of the stack to create or delete")
templateFilePtr := flag.String("t", "", "The name of the file containing the CloudFormation template")
flag.Parse()
operation := *operationPtr
stackName := *stackNamePtr
templateFile := *templateFilePtr
if (operation == "create" || operation == "delete" || operation == "all") && stackName == "" {
// Create dummy name using guid
// Create a unique GUID for stack name
id := uuid.New()
stackName = "stack-" + id.String()
}
if (operation == "create" || operation == "all") && templateFile == "" {
fmt.Println("You must supply the name of the template to use to create a stack")
return
}
// Initialize a session that the SDK uses to load
// credentials from the shared credentials file ~/.aws/credentials
// and configuration from the shared configuration file ~/.aws/config.
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
switch operation {
case "all":
// Create stack
err := CreateStack(sess, stackName, templateFile)
if err != nil {
fmt.Println("Could not create stack " + stackName)
}
// Get all stacks
summaries, err := GetStackSummaries(sess, "all")
if err != nil {
fmt.Println("Could not list stack summary info")
return
}
for _, s := range summaries {
fmt.Println(*s.StackName + ", Status: " + *s.StackStatus)
}
fmt.Println("")
// Delete stack
err = DeleteStack(sess, stackName)
if err != nil {
fmt.Println("Could not delete stack " + stackName)
}
case "create":
err := CreateStack(sess, stackName, templateFile)
if err != nil {
fmt.Println("Could not create stack " + stackName)
}
case "list":
summaries, err := GetStackSummaries(sess, "all")
if err != nil {
fmt.Println("Could not list stack summary info")
return
}
for _, s := range summaries {
fmt.Println(*s.StackName + ", Status: " + *s.StackStatus)
}
fmt.Println("")
case "delete":
err := DeleteStack(sess, stackName)
if err != nil {
fmt.Println("Could not delete stack " + stackName)
}
default:
fmt.Println("Unrecognized operation: " + operation)
}
}