diff --git a/resource/stack.go b/resource/stack.go index 64d71db..b77f5bd 100644 --- a/resource/stack.go +++ b/resource/stack.go @@ -23,6 +23,8 @@ type Stack struct { CloudformationParameters []string `yaml:"CloudformationParameters,omitempty"` CloudformationCapabilities []string `yaml:"CloudformationCapabilities,omitempty"` + + Destroy bool `yaml:"Destroy,omitempty"` } func (s Stack) NewForRegion(region string) Stack { diff --git a/resourceoperation/cdk.go b/resourceoperation/cdk.go index f152ef6..b6d1d58 100644 --- a/resourceoperation/cdk.go +++ b/resourceoperation/cdk.go @@ -73,6 +73,10 @@ func (co *cdkOperation) Call(ctx context.Context) error { } } else if co.Operation == Deploy { cdkArgs = []string{"deploy", "--require-approval", "never"} + + if co.Stack.Destroy { + cdkArgs = []string{"destroy", "--require-approval", "never"} + } } cdkArgs = append(cdkArgs, cdkDefaultArgs(*co.Account, co.Stack)...) diff --git a/resourceoperation/cloudformation.go b/resourceoperation/cloudformation.go index b5f0221..9e6b6cb 100644 --- a/resourceoperation/cloudformation.go +++ b/resourceoperation/cloudformation.go @@ -83,6 +83,14 @@ func (co *cloudformationOp) Call(ctx context.Context) error { return nil } + if co.Stack.Destroy { + err := co.DeleteStack(ctx) + if err != nil { + return oops.Wrapf(err, "DeleteStack") + } + return nil + } + _, err = co.executeChangeSet(ctx, cs.ChangeSetId) if err != nil { return oops.Wrapf(err, "executing change set") @@ -202,6 +210,16 @@ func (co *cloudformationOp) executeChangeSet(ctx context.Context, changeSetID *s } } +func (co *cloudformationOp) DeleteStack(ctx context.Context) error { + _, err := co.CloudformationClient.DeleteStackWithContext(ctx, &cloudformation.DeleteStackInput{ + StackName: co.Stack.CloudformationStackName(), + }) + if err != nil { + return oops.Wrapf(err, "DeleteStackWithContext stack: %s", *co.Stack.CloudformationStackName()) + } + return nil +} + func (co *cloudformationOp) ToString() string { return "" } diff --git a/resourceoperation/scp.go b/resourceoperation/scp.go index ecf0791..da8b32d 100644 --- a/resourceoperation/scp.go +++ b/resourceoperation/scp.go @@ -140,6 +140,10 @@ func (so *scpOperation) Call(ctx context.Context) error { args = []string{ "apply", "-auto-approve", } + + if so.Stack.Destroy { + args = append(args, "-destroy") + } } workingPath := so.tmpPath() diff --git a/resourceoperation/terraform.go b/resourceoperation/terraform.go index c9ac820..f72339a 100644 --- a/resourceoperation/terraform.go +++ b/resourceoperation/terraform.go @@ -88,6 +88,10 @@ func (to *tfOperation) Call(ctx context.Context) error { } } + if to.Stack.Destroy { + args = append(args, "-destroy") + } + workingPath := terraform.TmpPath(*to.Account, to.Stack.Path) cmd := exec.Command(localstack.TfCmd(), args...) cmd.Dir = workingPath