Skip to content

Commit

Permalink
Merge pull request #58 from GSA/solr-cloudwatch-sns-lambda-restarts
Browse files Browse the repository at this point in the history
Solr on ECS - Cloudwatch - SNS - Lambda Restarts
  • Loading branch information
nickumia-reisys authored Sep 2, 2022
2 parents a361094 + f76c8fb commit e5f050a
Show file tree
Hide file tree
Showing 15 changed files with 1,247 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ terraform/*/provision/.cache
.kube*
*.binding.json
.terraform.lock.hcl

# Solr restart temp files
terraform/ecs/provision/app.py
terraform/ecs/provision/package/
restart_app.zip
57 changes: 57 additions & 0 deletions bin/clean_ecs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

package main

import (
"fmt"
"sync"

nuke_aws "github.com/gruntwork-io/cloud-nuke/aws"
)

func main() {
fmt.Println("Are you ready to nuke some AWS Resources?!")
fmt.Println("---------------------")

queryResources()
}

func queryResources() {
var wg sync.WaitGroup

// NewQuery is a convenience method for configuring parameters you want to pass to your resource search
query, err := nuke_aws.NewQuery(
targetRegions,
excludeRegions,
resourceTypes,
excludeResourceTypes,
excludeAfter,
)
if err != nil {
fmt.Println(err)
}

// InspectResources still returns *AwsAccountResources, but this struct has been extended with several
// convenience methods for quickly determining if resources exist in a given region
accountResources, err := nuke_aws.InspectResources(query)
if err != nil {
fmt.Println(err)
}
for region, all_resources := range accountResources.Resources {
// fmt.Printf("Region: [%s]\n", region)
for class_name, list_resources := range all_resources.Resources {
// fmt.Printf("AWS Resource Class: [%s]\n", resourceTypes[class_name])
for _, resource := range list_resources.ResourceIdentifiers() {
fmt.Println("\n\n\n\n\n")
for group, command := range tag_commands {
checkTags(resource, class_name, group, command)
}
if interact(fmt.Sprintf("%s : %s : %s", region, resultTypes[class_name], resource)) {
wg.Add(1)
go deleteResource(&wg, list_resources, resource)
}
}
}
}
fmt.Println("Waiting for background processes to complete...")
wg.Wait()
}
78 changes: 78 additions & 0 deletions bin/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

package main

import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
nuke_aws "github.com/gruntwork-io/cloud-nuke/aws"
)

/** CLOUD-NUKE Configuration **/

// You can scan multiple regions at once, or just pass a single region for speed
var targetRegions = []string{"us-west-2"}
var excludeRegions = []string{}
// You can simultaneously target multiple resource types as well
// var resourceTypes = []string{"ec2", "vpc", "efs", "ecsserv", "ecscluster", "cloudwatch-loggroup", "eip", "nat-gateway", "elbv2"}
// var resultTypes = []string{"elbv2", "nat-gateway", "ec2", "eip", "ecsserv", "ecscluster", "cloudwatch-loggroup", "vpc", "efs"}
var resourceTypes = []string{"vpc"}
var resultTypes = []string{"vpc"}
var excludeResourceTypes = []string{}
// excludeAfter is parsed identically to the --older-than flag
var excludeAfter = time.Now()

var aws_session, session_err = session.NewSession(&aws.Config{Region: aws.String(targetRegions[0])})

/** Interactive Input Configuration **/

var reader = bufio.NewReader(os.Stdin)
func interact(resource string) bool{
/* Decide if to delete selected resource */
fmt.Printf("Delete [%s] -> ", resource)
text, _ := reader.ReadString('\n')
// convert CRLF to LF
text = strings.Replace(text, "\n", "", -1)

if strings.Compare("yes", text) == 0 {
fmt.Println("Got it! It's gone :)")
return true
} else {
fmt.Println("Well fine, I'll leave it :/")
return false
}
}

var tag_commands = map[string]string{
"nat-gateway": "aws ec2 describe-nat-gateways --nat-gateway-ids %s | jq -r .NatGateways[0].Tags",
"eip": "aws ec2 describe-addresses --filters Name=allocation-id,Values=%s | jq -r .Addresses[0].Tags",
"vpc": "aws ec2 describe-vpcs --filters Name=vpc-id,Values=%s | jq -r .Vpcs[0].Tags",
"efs": "aws efs describe-file-systems --file-system-id %s | jq -r .FileSystems[0].Name",
}

func checkTags(resource string, resource_class int, group string, command string) {
if resultTypes[resource_class] == group {
cmd := exec.Command("bash", "-c", fmt.Sprintf(command, resource))
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(stdout))
}
}
}

func deleteResource(wg *sync.WaitGroup, list_resources nuke_aws.AwsResources, resource string) {
defer wg.Done()
err := list_resources.Nuke(aws_session, []string{resource})
if err != nil {
fmt.Println(err)
}
}
38 changes: 38 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module bin

go 1.17

require (
github.com/aws/aws-sdk-go v1.44.85
github.com/gruntwork-io/cloud-nuke v0.19.0
)

require (
github.com/aws/aws-sdk-go-v2 v1.16.11 // indirect
github.com/aws/aws-sdk-go-v2/config v1.17.1 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.12.14 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.18 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.19 // indirect
github.com/aws/aws-sdk-go-v2/service/efs v1.17.10 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.12 // indirect
github.com/aws/aws-sdk-go-v2/service/sns v1.17.13 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.11.17 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.16.13 // indirect
github.com/aws/smithy-go v1.12.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/go-errors/errors v1.0.2-0.20180813162953-d98b870cc4e0 // indirect
github.com/gruntwork-io/go-commons v0.8.2 // indirect
github.com/gruntwork-io/gruntwork-cli v0.7.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/sirupsen/logrus v1.6.0 // indirect
github.com/urfave/cli v1.22.4 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
)
Loading

0 comments on commit e5f050a

Please sign in to comment.