[AWS] aws ๊ณ„์ • ํ•ด์ง€ ์ „, aws-nuke๋กœ ๊ฐ„ํŽธํ•˜๊ฒŒ ์ž์› ์ผ๊ด„ ์‚ญ์ œํ•˜๊ธฐ

2023. 11. 5. 18:27ใ†AWS

โ˜„๏ธ ๊ฐœ์š”

์šด์˜ํ•˜๋Š” ์„œ๋น„์Šค๊ฐ€ ์ข…๋ฃŒ๋˜์–ด AWS ๊ณ„์ •์„ ํ•ด์ง€ํ•˜๊ธฐ ์ „ ํ•ด๋‹น AWS ๊ณ„์ •์— ์žˆ๋Š” ๋ชจ๋“  ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ญ์ œํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ–ˆ๋‹ค. AWS ๊ณ„์ • ๋‚ด ์กด์žฌํ•˜๋Š” ํ•œ ์‹œ์Šคํ…œ์„ ์ข…๋ฃŒํ•˜๋ ค ํ•˜๋ฉด EC2 ๋ฐ RDS ์‚ญ์ œ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์Šค๋ƒ…์ƒท, ๋ณผ๋ฅจ, ์—ฐ๊ฒฐ๋œ ๋ณด์•ˆ ๊ทธ๋ฃน, ๋“ฑ๋“ฑ ํ•จ๊ป˜ ์‚ญ์ œ๋ฅผ ๊ณ ๋ คํ•ด์•ผํ•  ์ž์›๋“ค์ด ๋งŽ๋‹ค. ๊ณ„์ •์— ์ƒ์„ฑ๋œ ๋ฆฌ์†Œ์Šค๋ฅผ ํ•œ ๋ˆˆ์— ํŒŒ์•…ํ•˜๊ธฐ ์‰ฝ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ˆ„๋ฝ๋˜๋Š” ์ž์›๋“ค๋„ ์ƒ๊ธด๋‹ค. ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์„ ํ›„๊ด€๊ณ„๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•ด์„œ ์‚ญ์ œํ•ด์•ผ ํ•˜๋Š” ์ž์›๋“ค๋„ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฝค ๋ฒˆ๊ฑฐ๋กœ์šด ์ž‘์—…์ด๋‹ค. IaC๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค์–ด์ง„ ์•„ํ‚คํ…์ฒ˜๋ผ๋ฉด destroy ๋ช…๋ น์–ด ๋ฐ ์Šคํƒ ์‚ญ์ œ ๋“ฑ์œผ๋กœ ๋ฆฌ์†Œ์Šค ์‚ญ์ œ๋ฅผ ํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ํ”„๋กœ๋น„์ €๋‹ ์ดํ›„ ๊ณ„์†ํ•ด์„œ sync๋ฅผ ๋งž์ถ”์–ด ์šด์˜ํ•˜๊ณ  ์žˆ์—ˆ๋˜ ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด ๋ˆ„๋ฝ๋˜๋Š” ์ž์›๋“ค๋„ ์กด์žฌํ•  ๊ฒƒ์ด๋‹ค. ๋ณด๋‹ค ๊ฐ„ํŽธํ•œ ์‚ญ์ œ๋ฅผ ์œ„ํ•ด ํšŒ์‚ฌ ๋™๋ฃŒ๊ฐ€ ์ถ”์ฒœํ•ด์ค€ AWS-nuke๋ฅผ ์ด์šฉํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

๐Ÿ’ฃ AWS-nuke๋ž€?
nuke๋Š” ํ•ต๋ฌด๊ธฐ๋ผ๋Š” ๋œป์œผ๋กœ AWS ๊ณ„์ •๊ณผ AWS ๋ฆฌ์ „์˜ ๊ฑฐ์˜ ๋ชจ๋“  ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ญ์ œํ•˜๋Š” ์˜คํ”ˆ ์†Œ์Šค ๋„๊ตฌ์ด๋‹ค. AWS-nuke๋Š” ๊ณต์‹ Github:README์—์„œ ๋ชจ๋“  ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ญ์ œํ•ด๋„ ๊ดœ์ฐฎ์€ AWS ๊ณ„์ •์—์„œ๋งŒ aws-nuke๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ๊ฐ•๋ ฅํžˆ ๊ถŒ๊ณ ํ•˜๊ณ  ์žˆ๋‹ค. ์ฃผ์˜ํ•ด์„œ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜์ž.

 

 

๐ŸŒŽ ํ™˜๊ฒฝ

  • macOS Monterey

 


 

๐Ÿ”ซ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

1. aws-nuke ์„ค์น˜

brew install aws-nuke

*Windows ๋˜๋Š” Linux์˜ ๊ฒฝ์šฐ์—๋Š” ๊ณต์‹ Github:Releases์—์„œ ์••์ถ• ํŒŒ์ผ์„ ๋‹ค์šด ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

 

2. Account Alias ์„ค์ •

AWS-nuke ์‚ญ์ œ ์‹คํ–‰ ์‹œ, aws account alias๋ฅผ ๋‘๋ฒˆ ์ž…๋ ฅํ•˜๋„๋ก ํ•˜์—ฌ ์‚ญ์ œ๋ฅผ ์žฌํ™•์ธํ•˜๊ธฐ ๋•Œ๋ฌธ์— aws accoung alias๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. alias์—๋Š” "prod" ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค. AWS Account๋Š” IAM ์ฝ˜์†” ๋˜๋Š” CLI๋ฅผ ํ†ตํ•ด ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. 

aws iam create-account-alias --account-alias {account-alias}

 

3. AWS Credentials ์„ค์ •

์‚ญ์ œ๋ฅผ ์œ„ํ•ด admin ๊ถŒํ•œ์ด ์žˆ๋Š” AWS IAM ์œ ์ €๋ฅผ ์ด์šฉํ•˜์˜€๋‹ค. aws-nuke๋ฅผ ์‹คํ–‰ํ•  ๋กœ์ปฌ์˜ ~/.aws/credentials ํŒŒ์ผ์— aws_access_key_id์™€ aws_secret_access_key๋ฅผ ์ €์žฅํ•˜๊ณ  ~/.aws/config ํŒŒ์ผ์— profile์„ ์ง€์ •ํ•˜์˜€๋‹ค.

 

4. Config ํŒŒ์ผ ์ƒ์„ฑ

์‚ญ์ œ๋ฅผ ์ง„ํ–‰ํ•  ๋ฆฌ์ „๊ณผ ๊ณ„์ •์„ ๋ช…์‹œํ•˜๋Š” config ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค. 

regions:
  - ap-northeast-2 

account-blocklist:
  - "999999999999"

accounts:
  "{Account-ID}": {}

 

account-blocklist๋Š” ์‚ญ์ œํ•˜์ง€ ์•Š์„ account ID ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ์†์„ฑ์œผ๋กœ, ์‚ญ์ œ ์‹ค์ˆ˜ ๋ฐฉ์ง€๋ฅผ ์œ„ํ•ด ์ตœ์†Œ 1๊ฐœ์˜ account ID์— ๋Œ€ํ•ด ๋ช…์‹œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. config file ๋‚ด account-blocklist๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ํ•˜๊ธฐ์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์ž„์˜์˜ id "999999999999"๋ฅผ ์ž…๋ ฅํ•˜์˜€๋‹ค.

Error: The config file contains an empty blocklist. For safety reasons you need to specify at least one account ID. This should be your production account.

 

5. ์‚ญ์ œ ๊ฐ€๋Šฅํ•œ ์ž์› ๋ชฉ๋ก ํ™•์ธํ•˜๊ธฐ

aws-nuke ๋ช…๋ น์–ด๋Š” ๋‹จ์ง€ nukeableํ•œ ๋ฆฌ์†Œ์Šค๋งŒ ๋ฆฌ์ŠคํŠธํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ์‹ฌํ•˜๊ณ  ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ ๋˜ํ•œ alias ์ž…๋ ฅ์€ ํ•„์š”ํ•˜๋‹ค.

aws-nuke -c config.yaml --profile {3์—์„œ ์ง€์ •ํ•œ profile}

 

6. ์ผ๊ด„ ์‚ญ์ œํ•˜๊ธฐ

์‹ค์ œ๋กœ ์ž์›๋“ค์„ ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” --no-dry-run ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.

aws-nuke -c config.yaml --no-dry-run --profile {3์—์„œ ์„ค์ •ํ•œ profile}

--no-dry-run ์‹œ alias ์ž…๋ ฅ (1)
--no-dry-run ์‹œ alias ์ž…๋ ฅ (2)

--no-dry-run์„ ํ†ตํ•ด ๋ฆฌ์†Œ์Šค ์‚ญ์ œ ์‹œ๋„ ์‹œ, alias 2๋ฒˆ ์ž…๋ ฅํ•˜๋„๋ก ํ•˜์—ฌ ์‚ญ์ œํ•˜๊ณ ์ž ํ•˜๋Š” ๊ณ„์ •์„ ์žฌํ™•์ธํ•œ๋‹ค. ์ฒซ๋ฒˆ์งธ๋Š” ์ฒซ๋ฒˆ์งธ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋ช…๋ น์–ด ์‹คํ–‰ ์งํ›„, ๋‘๋ฒˆ์งธ๋Š” ๋‘๋ฒˆ์งธ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด nukeableํ•œ ์ž์›์„ ๋ชจ๋‘ ๋ฆฌ์ŠคํŠธํ•œ ๋’ค alias๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

 

7. ๊ฒ€ํ† ํ•˜๊ธฐ

ap-northeast-2 - RDSInstance - {DB-Instance} - [AvailabilityZone: "ap-northeast-2b", DeletionProtection: "true", Engine: "postgres", EngineVersion: "12.14", Identifier: "{DB-Instance}", InstanceClass: "{db-instance-type}", InstanceCreateTime: "2022-10-01T06:36:56Z", MultiAZ: "false", PubliclyAccessible: "false", tag:aws:cloudformation:logical-id: "{DB-Instance}", tag:aws:cloudformation:stack-id: "arn:aws:cloudformation:ap-northeast-2:{Account-ID}:{CF-Stack}", tag:aws:cloudformation:stack-name: "{Stack-Name}"] - failed

๋กœ๊ทธ์—์„œ ์œ„์™€ ๊ฐ™์ด ์‚ญ์ œ์— ์‹คํŒจํ•œ ์ž์›๋“ค์„ ๋ฐœ๊ฒฌํ–ˆ๋‹ค. ๋Œ€์ฒด๋กœ Termination Protection(์ข…๋ฃŒ ๋ณดํ˜ธ)๋ฅผ ํ™œ์„ฑํ™”ํ•œ EC2 ์ธ์Šคํ„ด์Šค ๋ฐ Deletion Protection(์‚ญ์ œ ๋ณดํ˜ธ)๋ฅผ ํ™œ์„ฑํ™”ํ•œ RDS ์ธ์Šคํ„ด์Šค๋“ค์ด์—ˆ์œผ๋ฉฐ ํ•ด๋‹น ์ธ์Šคํ„ด์Šค์—์„œ ๋ณดํ˜ธ๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•œ ํ›„ ๋‹ค์‹œ aws-nuke --no-dry-run ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋‹ˆ ์ •์ƒ์ ์œผ๋กœ ์‚ญ์ œ๊ฐ€ ๋˜์—ˆ๋‹ค.

ํ•œ๋ฒˆ ๋” ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋” ์ด์ƒ ์‚ญ์ œํ•  ๋ฆฌ์†Œ์Šค๊ฐ€ ์—†๋‹ค๋Š” ์ถœ๋ ฅ์„ ๋ฐ›์•˜๋‹ค. ์‹ค์ œ๋กœ ์ž์›๋“ค์ด ์‚ญ์ œ๋๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์ฝ˜์†”์— ๋“ค์–ด๊ฐ€์„œ ๋ฉ”์ธ ์„œ๋น„์Šค๋“ค์„ ํ™•์ธํ–ˆ๋Š”๋ฐ, Route53 ํ˜ธ์ŠคํŒ… ์˜์—ญ์€ ์‚ญ์ œ๋˜์ง€ ์•Š์•˜๋‹ค. ๊ณต์‹ Github: Resources์—๋Š” Route53HostedZone์ด๋ผ๋Š” ๋ฆฌ์†Œ์Šค๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ ์™œ ์‚ญ์ œ๊ฐ€ ๋˜์ง€ ์•Š์€์ง€๋Š” ์˜๋ฌธ์ด๋‹ค. ๊ณต์‹ Github: config-example์„ ์ฐธ๊ณ ํ•ด config.yaml ํŒŒ์ผ์— ์•„๋ž˜์™€ ๊ฐ™์ด ํƒ€๊ฒŸ์„ ์ง€์ •ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์˜€๋Š”๋ฐ๋„ ์‚ญ์ œํ•  ๋ฆฌ์†Œ์Šค๊ฐ€ ์—†๋‹ค๋Š” ์ถœ๋ ฅ์ด ๋‚˜์™€ Route53์˜ hosted zone์€ ์ˆ˜๋™์œผ๋กœ ์‚ญ์ œํ•ด์ฃผ์—ˆ๋‹ค.

resource-types:
  targets:
  - Route53HostedZone

 

 

๐Ÿ’ก ๋Š๋‚€ ์ 

์ˆ˜๋™์œผ๋กœ Route53์„ ์‚ญ์ œํ•ด์ฃผ๊ธด ํ–ˆ์ง€๋งŒ, ๋ช…๋ น์–ด ํ•˜๋‚˜๋กœ AWS Account ๋‚ด ์ž์›๋“ค์„ ์ผ๊ด„๋กœ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์–ด ๋„ˆ๋ฌด ํŽธ๋ฆฌํ–ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํ•œํŽธ์œผ๋กœ๋Š” ์œ„ํ—˜ํ•œ ํˆด์ด๊ธฐ๋„ ํ•˜์ง€๋งŒ, ๊ณ„์ • ํ•ด์ง€๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” ์ข…์ข… aws-nuke๋ฅผ ์ด์šฉํ•  ๊ฒƒ ๊ฐ™๋‹ค.


 

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ