Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit 7144849

Browse files
authored
Merge pull request rebuy-de#2 from aws-samples/feature-publish-apg
Update stack to add SNS resources, and modify Nuke with latest version
2 parents 7e8f4c0 + 8f3c2d3 commit 7144849

File tree

4 files changed

+159
-76
lines changed

4 files changed

+159
-76
lines changed

README.md

+19-12
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,25 @@ $ aws-nuke -c $line.yaml --force --no-dry-run --access-key-id $ACCESS_KEY_ID --s
7070

7171
* Clone the repo
7272
* Determine the ID of the account to be deployed for clean up ( This is only to be deployed to Dev/Test/Sandbox environments )
73-
* Verify and Update your nuke config file as needed with specific filters for the resources/accounts
74-
* Deploy the stack using the below command. You can run it in any desired region. Replace the required parameter with the SNS Topic Arn for notification email
73+
* Verify and Update your nuke configuration file as needed with specific filters for the resources/accounts
74+
* Deploy the stack using the below command. You can run it in any desired region.
7575
```sh
76-
aws cloudformation create-stack --stack-name NukeCleanser --template-body file://nuke-cfn-stack.yaml --region us-east-2 --capabilities CAPABILITY_NAMED_IAM --parameters ParameterKey=NukeTopicArn,ParameterValue='arn:aws:sns:us-east-2:{ACCT_ID}:TestSNSTopic'
76+
aws cloudformation create-stack --stack-name NukeCleanser --template-body file://nuke-cfn-stack.yaml --region us-east-2 --capabilities CAPABILITY_NAMED_IAM
7777
```
78-
* Once the S3 bucket is created using the cfn template, upload the Nuke generic config file and the config update python script
78+
* Once the stack is created, upload the nuke generic config file and the python script to the S3 bucket using the commands below. You can find the name of the S3 bucket generated from the CloudFormation console `Outputs` tab.
7979
```sh
80-
aws s3 cp config/nuke_generic_config.yaml --region us-east-2 s3://nuke-account-cleanser-config
81-
aws s3 cp config/nuke_config_update.py --region us-east-2 s3://nuke-account-cleanser-config
80+
aws s3 cp config/nuke_generic_config.yaml --region us-east-2 s3://{your-bucket-name}
81+
aws s3 cp config/nuke_config_update.py --region us-east-2 s3://{your-bucket-name}
8282
```
8383
* Run the stack manually by triggering the StepFunctions with the below sample input payload. (which is pre-configured in the EventBridge Target as a Constant JSON input). You can configure this to run in parallel on the required number of regions by updating the region_list parameter.
8484

8585
```sh
8686
{
8787
"InputPayLoad": {
8888
"nuke_dry_run": "true",
89-
"nuke_version": "2.5",
90-
"nuke_config_bucket": "nuke-account-cleanser-config",
91-
"sns_notification_arn": "sns_topic_arn",
89+
"nuke_version": "2.21.2",
9290
"region_list": [
91+
"global",
9392
"us-west-1",
9493
"us-east-1"
9594
]
@@ -117,11 +116,19 @@ Account Cleansing Process Completed;
117116
Build State : JOB SUCCEEDED
118117
Build ID : AccountNuker-NukeCleanser:4509a9b5
119118
CodeBuild Project Name : AccountNuker-NukeCleanser
120-
Process Start Time : Thu Dec 2 02:04:40 UTC 2021
121-
Process End Time : Thu Dec 2 02:06:45 UTC 2021
119+
Process Start Time : Thu Feb 23 04:05:21 UTC 2023
120+
Process End Time : Thu Feb 23 04:05:54 UTC 2023
122121
Log Stream Path : AccountNuker-NukeCleanser/logPath
123122
------------------------------------------------------------------
124-
################ Removed the following resources #################
123+
################ Nuke Cleanser Logs #################
124+
125+
FAILED RESOURCES
126+
-------------------------------
127+
Total number of Resources that would be removed:
128+
3
129+
us-west-1 - SQSQueue - https://sqs.us-east-1.amazonaws.com/123456789012/test-nuke-queue - would remove
130+
us-west-1 - SNSTopic - TopicARN: arn:aws:sns:us-east-1:123456789012:test-nuke-topic - [TopicARN: "arn:aws:sns:us-east-1:123456789012:test-topic"] - would remove
131+
us-west-1 - S3Bucket - s3://test-nuke-bucket-us-west-1 - [CreationDate: "2023-01-25 11:13:14 +0000 UTC", Name: "test-nuke-bucket-us-west-1"] - would remove
125132

126133
```
127134
* By default the stack runs aws-nuke in DryRun mode, To actually delete resources update the stack with AWSNukeDryRunFlag parameter flipped to false OR udpate manually in the CodeBuild environment variables section.

config/nuke_config_update.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import argparse
88
import copy
99

10-
GLOBAL_EXCEPTIONS = [
10+
GLOBAL_RESOURCE_EXCEPTIONS = [
1111
{"property": "tag:DoNotNuke", "value": "True"},
1212
{"property": "tag:Permanent", "value": "True"},
1313
{"type": "regex", "value": ".*auto-account-cleanser*.*"},
@@ -27,10 +27,10 @@ def __init__(self, account, target_regions):
2727
self.account = account
2828

2929
def Populate(self):
30-
self.BuildResourceList()
31-
self.MergeWithDefaultConfig()
30+
self.UpdateCFNStackList()
31+
self.OverrideDefaultConfig()
3232

33-
def BuildResourceList(self):
33+
def UpdateCFNStackList(self):
3434
try:
3535
for region in self.regions:
3636
cfn_client = self.session.client("cloudformation", region_name=region)
@@ -46,12 +46,12 @@ def BuildResourceList(self):
4646
)
4747
for page in responses:
4848
for stack in page.get("StackSummaries"):
49-
self.GetStackResources(stack, cfn_client)
49+
self.GetCFNResources(stack, cfn_client)
5050
self.BuildIamExclusionList(region)
5151
except Exception as e:
52-
print("Exception calling BuildResourceList:\n {}".format(e))
52+
print("Error in calling UpdateCFNStackList:\n {}".format(e))
5353

54-
def GetStackResources(self, stack, cfn_client):
54+
def GetCFNResources(self, stack, cfn_client):
5555
try:
5656
stack_name = stack.get("StackName")
5757

@@ -76,10 +76,9 @@ def GetStackResources(self, stack, cfn_client):
7676
)
7777
for resource in stack_resources.get("StackResourceSummaries"):
7878
if resource.get("ResourceType") == "AWS::CloudFormation::Stack":
79-
# recurse if the resource type is a stack (nested stacks)
80-
self.GetStackResources(resource, cfn_client)
79+
self.GetCFNResources(resource, cfn_client)
8180
else:
82-
nuke_type = self.FixResourceName(resource["ResourceType"])
81+
nuke_type = self.UpdateResourceName(resource["ResourceType"])
8382
if nuke_type in self.resources:
8483
self.resources[nuke_type].append(
8584
{
@@ -95,9 +94,9 @@ def GetStackResources(self, stack, cfn_client):
9594
}
9695
]
9796
except Exception as e:
98-
print("Exception calling GetStackResources:\n {}".format(e))
97+
print("Error calling GetCFNResources:\n {}".format(e))
9998

100-
def FixResourceName(self, resource):
99+
def UpdateResourceName(self, resource):
101100
nuke_type = str.replace(resource, "AWS::", "")
102101
nuke_type = str.replace(nuke_type, "::", "")
103102
nuke_type = str.replace(nuke_type, "Config", "ConfigService", 1)
@@ -126,7 +125,7 @@ def BuildIamExclusionList(self, region):
126125
role.get("RoleName")
127126
]
128127

129-
def MergeWithDefaultConfig(self):
128+
def OverrideDefaultConfig(self):
130129
# Open the nuke_generic_config.yaml and merge the captured resources/exclusions with it
131130
try:
132131
with open(r"nuke_generic_config.yaml") as config_file:
@@ -148,7 +147,7 @@ def MergeWithDefaultConfig(self):
148147
self.config["accounts"].pop("ACCOUNT", None)
149148
# Global exclusions apply to every type of resource
150149
for resource in self.config["accounts"][self.account]["filters"]:
151-
for exception in GLOBAL_EXCEPTIONS:
150+
for exception in GLOBAL_RESOURCE_EXCEPTIONS:
152151
self.config["accounts"][self.account]["filters"][resource].append(
153152
exception.copy()
154153
)
@@ -184,7 +183,7 @@ def WriteConfig(self):
184183
exit(1)
185184

186185
if __name__ == "__main__":
187-
186+
print("Incoming Args: ", args)
188187
stackInfo = StackInfo(args.account, [args.region])
189188
stackInfo.Populate()
190189
stackInfo.WriteConfig()

config/nuke_generic_config.yaml

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
regions:
2+
# add `global` here to include IAM entities to be nuked
23
- TARGET_REGION # will be overridden during run time based on region parameter
34

45
account-blocklist:
@@ -16,15 +17,20 @@ resource-types:
1617
- S3Bucket
1718
- SNSTopic
1819
- SQSQueue
20+
- CloudTrailTrail
1921

2022
accounts:
2123
ACCOUNT: # will be overridden during run time based on account param
2224
filters:
2325
IAMRole:
2426
- "ProdRoles"
2527
- "DoNotDeleteRoles"
28+
- type: regex
29+
value: ".*"
2630
IAMUser:
2731
- "admin"
32+
- type: regex
33+
value: ".*"
2834
IAMUserPolicyAttachment:
2935
- property: RoleName
3036
value: "admin"
@@ -33,5 +39,10 @@ accounts:
3339
value: "admin"
3440
S3Bucket:
3541
- "s3://my-bucket"
36-
SNSTopic: [] # delete all SNS
42+
CloudTrailTrail: # filter all CloudTrail
43+
- type: regex
44+
value: ".*"
45+
SNSTopic: [] # SNS is protected based on global exception tags inside the nuke_config_update.py
46+
- type: regex
47+
value: ".*"
3748
SQSQueue: [] # delete all SQS

0 commit comments

Comments
 (0)