警告
此 aws-nuke 儲存庫不再積極維護。我們建議使用者切換到該專案在 ekristen/aws-nuke 上積極維護的分支。我們感謝在該專案整個生命週期中獲得的所有支持和貢獻。我們相信,該分支將繼續提供您期望從 aws-nuke 獲得的功能和支援。請注意,此棄用意味著我們將不會解決問題、接受拉取請求或從此儲存庫發布未來版本。感謝您的理解與支持。
從 AWS 帳戶中刪除所有資源。
開發狀態aws-nuke很穩定,但很可能並未涵蓋所有 AWS 資源。鼓勵新增缺少的資源並建立 Pull 請求或建立問題。
請注意, aws-nuke是一種破壞性很強的工具,因此在使用它時必須非常小心。否則您可能會刪除生產資料。
我們強烈建議您不要在任何 AWS 帳戶上執行此應用程序,因為您無法承擔失去所有資源的後果。
為了減少事故的爆炸半徑,有一些安全預防措施:
--no-dry-run
才能實際刪除資源。prod
。該字串是硬編碼的,建議將其新增至每個實際生產帳戶(例如mycompany-production-ecr
)。如果您有任何改進安全程序的想法,請隨意建立問題。
一旦足夠的更改匯集在一起並經過一段時間的測試,我們通常會發布一個新版本。
您可以在發布頁面上找到 Linux、macOS 和 Windows 二進位文件,但我們也在 quay.io/rebuy/aws-nuke 和 docker.io/rebuy/aws-nuke 上提供容器化版本。兩者均可用於多種架構(amd64、arm64 和 armv7)。
首先,您需要為aws-nuke建立一個設定檔。這是一個最小的:
regions :
- eu-west-1
- global
account-blocklist :
- " 999999999999 " # production
accounts :
" 000000000000 " : {} # aws-nuke-example
透過此配置,我們可以運行aws-nuke :
$ aws-nuke -c config/nuke-config.yml --profile aws-nuke-example
aws-nuke version v1.0.39.gc2f318f - Fri Jul 28 16:26:41 CEST 2017 - c2f318f37b7d2dec0e646da3d4d05ab5296d5bce
Do you really want to nuke the account with the ID 000000000000 and the alias 'aws-nuke-example'?
Do you want to continue? Enter account alias to continue.
> aws-nuke-example
eu-west-1 - EC2DHCPOption - 'dopt-bf2ec3d8' - would remove
eu-west-1 - EC2Instance - 'i-01b489457a60298dd' - would remove
eu-west-1 - EC2KeyPair - 'test' - would remove
eu-west-1 - EC2NetworkACL - 'acl-6482a303' - cannot delete default VPC
eu-west-1 - EC2RouteTable - 'rtb-ffe91e99' - would remove
eu-west-1 - EC2SecurityGroup - 'sg-220e945a' - cannot delete group 'default'
eu-west-1 - EC2SecurityGroup - 'sg-f20f958a' - would remove
eu-west-1 - EC2Subnet - 'subnet-154d844e' - would remove
eu-west-1 - EC2Volume - 'vol-0ddfb15461a00c3e2' - would remove
eu-west-1 - EC2VPC - 'vpc-c6159fa1' - would remove
eu-west-1 - IAMUserAccessKey - 'my-user -> ABCDEFGHIJKLMNOPQRST' - would remove
eu-west-1 - IAMUserPolicyAttachment - 'my-user -> AdministratorAccess' - [UserName: "my-user", PolicyArn: "arn:aws:iam::aws:policy/AdministratorAccess", PolicyName: "AdministratorAccess"] - would remove
eu-west-1 - IAMUser - 'my-user' - would remove
Scan complete: 13 total, 11 nukeable, 2 filtered.
Would delete these resources. Provide --no-dry-run to actually destroy resources.
正如我們所看到的, aws-nuke僅列出所有找到的資源並退出。這是因為缺少--no-dry-run
標誌。它還想刪除管理員。我們不想這樣做,因為我們使用此用戶來存取我們的帳戶。因此我們必須擴展配置,使其忽略該用戶:
regions :
- eu-west-1
account-blocklist :
- " 999999999999 " # production
accounts :
" 000000000000 " : # aws-nuke-example
filters :
IAMUser :
- " my-user "
IAMUserPolicyAttachment :
- " my-user -> AdministratorAccess "
IAMUserAccessKey :
- " my-user -> ABCDEFGHIJKLMNOPQRST "
$ aws-nuke -c config/nuke-config.yml --profile aws-nuke-example --no-dry-run
aws-nuke version v1.0.39.gc2f318f - Fri Jul 28 16:26:41 CEST 2017 - c2f318f37b7d2dec0e646da3d4d05ab5296d5bce
Do you really want to nuke the account with the ID 000000000000 and the alias 'aws-nuke-example'?
Do you want to continue? Enter account alias to continue.
> aws-nuke-example
eu-west-1 - EC2DHCPOption - 'dopt-bf2ec3d8' - would remove
eu-west-1 - EC2Instance - 'i-01b489457a60298dd' - would remove
eu-west-1 - EC2KeyPair - 'test' - would remove
eu-west-1 - EC2NetworkACL - 'acl-6482a303' - cannot delete default VPC
eu-west-1 - EC2RouteTable - 'rtb-ffe91e99' - would remove
eu-west-1 - EC2SecurityGroup - 'sg-220e945a' - cannot delete group 'default'
eu-west-1 - EC2SecurityGroup - 'sg-f20f958a' - would remove
eu-west-1 - EC2Subnet - 'subnet-154d844e' - would remove
eu-west-1 - EC2Volume - 'vol-0ddfb15461a00c3e2' - would remove
eu-west-1 - EC2VPC - 'vpc-c6159fa1' - would remove
eu-west-1 - IAMUserAccessKey - 'my-user -> ABCDEFGHIJKLMNOPQRST' - filtered by config
eu-west-1 - IAMUserPolicyAttachment - 'my-user -> AdministratorAccess' - [UserName: "my-user", PolicyArn: "arn:aws:iam::aws:policy/AdministratorAccess", PolicyName: "AdministratorAccess"] - would remove
eu-west-1 - IAMUser - 'my-user' - filtered by config
Scan complete: 13 total, 8 nukeable, 5 filtered.
Do you really want to nuke these resources on the account with the ID 000000000000 and the alias 'aws-nuke-example'?
Do you want to continue? Enter account alias to continue.
> aws-nuke-example
eu-west-1 - EC2DHCPOption - 'dopt-bf2ec3d8' - failed
eu-west-1 - EC2Instance - 'i-01b489457a60298dd' - triggered remove
eu-west-1 - EC2KeyPair - 'test' - triggered remove
eu-west-1 - EC2RouteTable - 'rtb-ffe91e99' - failed
eu-west-1 - EC2SecurityGroup - 'sg-f20f958a' - failed
eu-west-1 - EC2Subnet - 'subnet-154d844e' - failed
eu-west-1 - EC2Volume - 'vol-0ddfb15461a00c3e2' - failed
eu-west-1 - EC2VPC - 'vpc-c6159fa1' - failed
eu-west-1 - S3Object - 's3://rebuy-terraform-state-138758637120/run-terraform.lock' - triggered remove
Removal requested: 2 waiting, 6 failed, 5 skipped, 0 finished
eu-west-1 - EC2DHCPOption - 'dopt-bf2ec3d8' - failed
eu-west-1 - EC2Instance - 'i-01b489457a60298dd' - waiting
eu-west-1 - EC2KeyPair - 'test' - removed
eu-west-1 - EC2RouteTable - 'rtb-ffe91e99' - failed
eu-west-1 - EC2SecurityGroup - 'sg-f20f958a' - failed
eu-west-1 - EC2Subnet - 'subnet-154d844e' - failed
eu-west-1 - EC2Volume - 'vol-0ddfb15461a00c3e2' - failed
eu-west-1 - EC2VPC - 'vpc-c6159fa1' - failed
Removal requested: 1 waiting, 6 failed, 5 skipped, 1 finished
--- truncating long output ---
正如您所看到的, aws-nuke現在嘗試刪除所有未過濾的資源,而不關心它們之間的依賴關係。這會導致 API 錯誤,可以忽略。如果這些錯誤持續出現,則會在aws-nuke運行結束時顯示。
aws-nuke會重試刪除所有資源,直到刪除所有指定的資源或直到只剩下有錯誤的資源。
有兩種方法可以驗證aws-nuke 。有靜態憑證和設定檔。後者可以在共用憑證檔案(即~/.aws/credentials
)或共用設定檔(即~/.aws/config
)中設定。
要使用靜態憑證,需要命令列標誌--access-key-id
和--secret-access-key
。僅臨時會話需要標誌--session-token
。
要使用共用設定文件,需要命令列標誌--profile
。設定檔必須使用共用憑證檔案中的靜態憑證或具有假定角色的共用設定檔中定義。
可以將 aws-nuke 配置為針對非預設 AWS 端點運行。它可用於指向本地端點(例如 S3 設備或 Stratoscale 叢集)的整合測試。
若要將 aws-nuke 設定為使用自訂終端節點,請新增設定指令,如下列範例所示:
regions :
- demo10
# inspired by https://www.terraform.io/docs/providers/aws/guides/custom-service-endpoints.html
endpoints :
- region : demo10
tls_insecure_skip_verify : true
services :
- service : ec2
url : https://10.16.145.115/api/v2/aws/ec2
- service : s3
url : https://10.16.145.115:1060
- service : rds
url : https://10.16.145.115/api/v2/aws/rds
- service : elbv2
url : https://10.16.145.115/api/v2/aws/elbv2
- service : efs
url : https://10.16.145.115/api/v2/aws/efs
- service : emr
url : https://10.16.145.115/api/v2/aws/emr
- service : autoscaling
url : https://10.16.145.115/api/v2/aws/autoscaling
- service : cloudwatch
url : https://10.16.145.115/api/v2/aws/cloudwatch
- service : sns
url : https://10.16.145.115/api/v2/aws/sns
- service : iam
url : https://10.16.145.115/api/v2/aws/iam
- service : acm
url : https://10.16.145.115/api/v2/aws/acm
account-blocklist :
- " account-id-of-custom-region-prod " # production
accounts :
" account-id-of-custom-region-demo10 " :
然後可以如下使用:
$ aws-nuke -c config/my.yaml --access-key-id <access-key> --secret-access-key <secret-key> --default-region demo10
aws-nuke version v2.11.0.2.gf0ad3ac.dirty - Tue Nov 26 19:15:12 IST 2019 - f0ad3aca55eb66b93b88ce2375f8ad06a7ca856f
Do you really want to nuke the account with the ID account-id-of-custom-region-demo10 and the alias 'account-id-of-custom-region-demo10'?
Do you want to continue? Enter account alias to continue.
> account-id-of-custom-region-demo10
demo10 - EC2Volume - vol-099aa1bb08454fd5bc3499897f175fd8 - [tag:Name: "volume_of_5559b38e-0a56-4078-9a6f-eb446c21cadf"] - would remove
demo10 - EC2Volume - vol-11e9b09c71924354bcb4ee77e547e7db - [tag:Name: "volume_of_e4f8c806-0235-4578-8c08-dce45d4c2952"] - would remove
demo10 - EC2Volume - vol-1a10cb3f3119451997422c435abf4275 - [tag:Name: "volume-dd2e4c4a"] - would remove
demo10 - EC2Volume - vol-1a2e649df1ef449686ef8771a078bb4e - [tag:Name: "web-server-5"] - would remove
demo10 - EC2Volume - vol-481d09bbeb334ec481c12beee6f3012e - [tag:Name: "volume_of_15b606ce-9dcd-4573-b7b1-4329bc236726"] - would remove
demo10 - EC2Volume - vol-48f6bd2bebb945848b029c80b0f2de02 - [tag:Name: "Data volume for 555e9f8a"] - would remove
demo10 - EC2Volume - vol-49f0762d84f0439da805d11b6abc1fee - [tag:Name: "Data volume for acb7f3a5"] - would remove
demo10 - EC2Volume - vol-4c34656f823542b2837ac4eaff64762b - [tag:Name: "wpdb"] - would remove
demo10 - EC2Volume - vol-875f091078134fee8d1fe3b1156a4fce - [tag:Name: "volume-f1a7c95f"] - would remove
demo10 - EC2Volume - vol-8776a0d5bd4e4aefadfa8038425edb20 - [tag:Name: "web-server-6"] - would remove
demo10 - EC2Volume - vol-8ed468bfab0b42c3bc617479b8f33600 - [tag:Name: "web-server-3"] - would remove
demo10 - EC2Volume - vol-94e0370b6ab54f03822095d74b7934b2 - [tag:Name: "web-server-2"] - would remove
demo10 - EC2Volume - vol-9ece34dfa7f64dd583ab903a1273340c - [tag:Name: "volume-4ccafc2e"] - would remove
demo10 - EC2Volume - vol-a3fb3e8800c94452aff2fcec7f06c26b - [tag:Name: "web-server-0"] - would remove
demo10 - EC2Volume - vol-a53954e17cb749a283d030f26bbaf200 - [tag:Name: "volume-5484e330"] - would remove
demo10 - EC2Volume - vol-a7afe64f4d0f4965a6703cc0cfab2ba4 - [tag:Name: "Data volume for f1a7c95f"] - would remove
demo10 - EC2Volume - vol-d0bc3f2c887f4072a9fda0b8915d94c1 - [tag:Name: "physical_volume_of_39c29f53-eac4-4f02-9781-90512cc7c563"] - would remove
demo10 - EC2Volume - vol-d1f066d8dac54ae59d087d7e9947e8a9 - [tag:Name: "Data volume for 4ccafc2e"] - would remove
demo10 - EC2Volume - vol-d9adb3f084cd4d588baa08690349b1f9 - [tag:Name: "volume_of_84854c9b-98aa-4f5b-926a-38b3398c3ad2"] - would remove
demo10 - EC2Volume - vol-db42e471b19f42b7835442545214bc1a - [tag:Name: "lb-tf-lb-20191126090616258000000002"] - would remove
demo10 - EC2Volume - vol-db80932fb47243efa67c9dd34223c647 - [tag:Name: "web-server-5"] - would remove
demo10 - EC2Volume - vol-dbea1d1083654d30a43366807a125aed - [tag:Name: "volume-555e9f8a"] - would remove
--- truncating long output ---
aws-nuke刪除了大量資源,並且在任何版本中都可能會添加更多資源。最終,所有資源都應該被刪除。您可能想要限制要刪除的資源。有多種方法可以配置此功能。
一種方法是過濾器,這已經提到過。這需要知道每個資源的標識符。也可以透過兩種方法防止整個資源類型(例如S3Bucket
)被刪除。
--target
標誌將核攻擊限制為指定的資源類型。--exclude
標誌可防止對指定資源類型進行核武攻擊。也可以在設定檔中配置資源類型,如下列範例所示:
---
regions:
- "eu-west-1"
account-blocklist:
- 1234567890
resource-types:
# only nuke these three resources
targets:
- S3Object
- S3Bucket
- IAMRole
accounts:
555133742: {}
---
regions:
- "eu-west-1"
account-blocklist:
- 1234567890
resource-types:
# don't nuke IAM users
excludes:
- IAMUser
accounts:
555133742: {}
如果在多個位置指定目標(例如 CLI 和特定於帳戶),則必須在所有位置指定資源類型。換句話說,每個配置都會限制先前的配置。
如果使用排除,則不會刪除其所有資源類型。
提示:您可以使用以下命令查看所有可用的資源類型:
aws-nuke resource-types
此功能尚未發布,可能是
v2.18
的一部分。
aws-nuke支援透過 AWS Cloud Control API 刪除資源。執行aws-nuke時,它將透過雲端控制自動刪除一組手動管理的資源。
僅雲控制支援的資源的子集將自動刪除,因為可能存在已實現的資源,並且添加它們也會繞過用戶配置中的現有過濾器,因為雲端控制具有另一個命名方案和一組不同的屬性。此外,還有一些需要特殊處理的雲端控制資源,而aws-nuke尚不支援這些資源。
即使自動支援的雲端控制資源的子集有限,您也可以設定aws-nuke以使其嘗試任何其他資源。透過命令列標誌或透過設定檔。
對於設定文件,您必須將資源新增至resource-types.cloud-control
清單:
resource-types :
cloud-control :
- AWS::EC2::TransitGateway
- AWS::EC2::VPC
如果您想使用命令列,則必須為要新增的每個資源新增--cloud-control
標誌:
aws-nuke
-c nuke-config.yaml
--cloud-control AWS::EC2::TransitGateway
--cloud-control AWS::EC2::VPC
注意:有些資源受 Cloud Control 支持,並且已由aws-nuke原生實作。如果您配置對這些資源使用 Cloud Control,它將不會執行該資源的本機實作的程式碼。例如,使用--cloud-control AWS::EC2::VPC
時,它將不會使用EC2VPC
資源。
有一些功能是相當自以為是的。為了讓這些功能適用於所有人, aws-nuke提供了手動啟用這些功能的標誌。這些可以在配置的根級別進行配置,如下所示:
---
feature-flags :
disable-deletion-protection :
RDSInstance : true
EC2Instance : true
CloudformationStack : true
force-delete-lightsail-addons : true
過濾這一點對於不刪除目前使用者或具有全域共用命名空間且可能難以重新建立的資源(例如 S3 儲存桶)非常重要。目前過濾是基於資源標識符的。該標識符將作為aws-nuke的第一步列印(例如,對於 EC2 執行個體為i-01b489457a60298dd
)。
注意:即使使用篩選器,您也不應該在任何 AWS 帳戶上執行 aws-nuke,因為您無法承擔遺失所有資源的後果。過濾器配置很容易出錯。此外,由於 aws-nuke 處於持續開發階段,因此無論我們如何仔細審查新程式碼,總是有可能引入新的錯誤。
篩選器是特定於帳戶的配置的一部分,並按資源類型分組。這是刪除admin
使用者及其存取權限和兩個存取金鑰之外的所有資源的設定範例:
---
regions :
- global
- eu-west-1
account-blocklist :
- 1234567890
accounts :
0987654321 :
filters :
IAMUser :
- " admin "
IAMUserPolicyAttachment :
- " admin -> AdministratorAccess "
IAMUserAccessKey :
- " admin -> AKSDAFRETERSDF "
- " admin -> AFGDSGRTEWSFEY "
資源標識符與清單中任何篩選器完全匹配的任何資源都將被跳過。這些將在aws-nuke運行時標記為「按配置過濾」。
某些資源支援透過屬性進行過濾。當資源支援這些屬性時,它們將在輸出中列出,如下例所示:
global - IAMUserPolicyAttachment - 'admin -> AdministratorAccess' - [RoleName: "admin", PolicyArn: "arn:aws:iam::aws:policy/AdministratorAccess", PolicyName: "AdministratorAccess"] - would remove
要使用屬性,需要指定具有properties
和value
對象,而不是純字串。
這些類型可用於簡化配置。例如,可以保護單一使用者的所有存取金鑰:
IAMUserAccessKey :
- property : UserName
value : " admin "
除了完全匹配之外,還有其他比較類型:
exact
– 標識符必須與給定字串完全匹配。這是預設值。contains
– 識別碼必須包含給定的字串。glob
– 標識符必須與給定的 glob 模式相符。這意味著該字串可能包含通配符,例如*
和?
。請注意,通配符是為檔案路徑設計的,因此通配符與目錄分隔符號 ( /
) 不符。有關 glob 模式的詳細資訊可以在庫文檔中找到。regex
– 識別碼必須與給定的正規表示式相符。有關語法的詳細資訊可以在庫文檔中找到。dateOlderThan
- 識別碼被解析為時間戳記。新增偏移量後(在value
欄位中指定),產生的時間戳記必須在目前時間之後。有關偏移語法的詳細資訊可以在庫文件中找到。支援的日期格式為紀元時間、 2006-01-02
、 2006/01/02
、 2006-01-02T15: 2006-01-02T15:04:05Z
、 2006-01-02T15:04 2006-01-02T15:04:05Z07:00
2006-01-02T15:04:05.999999999Z07:00
2006-01-02T15:04:05Z07:00
。要使用非預設比較類型,需要指定具有type
和value
對象,而不是純字串。
這些類型可用於簡化配置。例如,可以使用glob
保護單一使用者的所有存取金鑰:
IAMUserAccessKey :
- type : glob
value : " admin -> * "
也可以一起使用過濾器屬性和過濾器類型。例如,要保護特定 TLD 的所有託管區域:
Route53HostedZone :
- property : Name
type : glob
value : " *.rebuy.cloud. "
任何過濾器結果都可以使用invert: true
反轉,例如:
CloudFormationStack :
- property : Name
value : " foo "
invert : true
在這種情況下,除名為「foo」的 CloudFormationStack以外的任何CloudFormationStack 都會被篩選。請注意, aws-nuke在內部獲取每個資源並對其應用每個過濾器。如果過濾器匹配,則將節點標記為已過濾。
某些過濾器在多個帳戶中可能是相同的。如果使用像 Terraform 這樣的配置工具或 IAM 資源遵循相同的模式,這種情況尤其可能發生。
對於這種情況, aws-nuke支援可套用於多個帳戶的過濾器預設。配置可能如下所示:
---
regions :
- " global "
- " eu-west-1 "
account-blocklist :
- 1234567890
accounts :
555421337 :
presets :
- " common "
555133742 :
presets :
- " common "
- " terraform "
555134237 :
presets :
- " common "
- " terraform "
filters :
EC2KeyPair :
- " notebook "
presets :
terraform :
filters :
S3Bucket :
- type : glob
value : " my-statebucket-* "
DynamoDBTable :
- " terraform-lock "
common :
filters :
IAMRole :
- " OrganizationAccountAccessRole "
brew install aws-nuke
最簡單的安裝方法是從 GitHub 下載最新版本。
下載並解壓縮$ wget -c https://github.com/rebuy-de/aws-nuke/releases/download/v2.25.0/aws-nuke-v2.25.0-linux-amd64.tar.gz -O - | tar -xz -C $HOME/bin
運行$ aws-nuke-v2.25.0-linux-amd64
要從原始碼編譯aws-nuke,您需要一個有效的 Golang 開發環境。來源必須克隆到$GOPATH/src/github.com/rebuy-de/aws-nuke
。
您還需要安裝 golint 和 GNU Make。
然後,您只需執行make build
將二進位檔案編譯到專案目錄中,或make install
go install aws-nuke到$GOPATH/bin
。使用make xc
您可以為其他平台交叉編譯aws-nuke 。
您可以使用以下命令透過 Docker 運行aws-nuke :
$ docker run
--rm -it
-v /full-path/to/nuke-config.yml:/home/aws-nuke/config.yml
-v /home/user/.aws:/home/aws-nuke/.aws
quay.io/rebuy/aws-nuke:v2.25.0
--profile default
--config /home/aws-nuke/config.yml
為了使其正常運作,您需要調整 AWS 配置和aws-nuke配置的路徑。
您還需要指定正確的 AWS 設定檔。您可以使用--access-key-id
和--secret-access-key
標誌,而不是掛載 AWS 目錄。
確保您在圖像標籤中使用最新版本。或者,您可以使用main
來取得最新的開發版本,但請注意,這很可能隨時中斷。
要對aws-nuke進行單元測試,某些測試需要 gomock 才能運作。這將透過go generate ./...
運行,但透過make test
自動運行。要運行單元測試:
make test
請隨意為任何錯誤報告或功能請求建立 GitHub 問題。請使用我們的郵件清單來提問:[email protected]。您也可以在郵件清單檔案中搜尋是否有人已經遇到相同的問題:https://groups.google.com/d/forum/aws-nuke
您可以透過分叉此儲存庫、進行變更並針對我們的儲存庫建立拉取請求來為aws-nuke做出貢獻。如果您不確定如何解決問題或對貢獻有其他疑問,請建立 GitHub 問題。