[AWS Config/CLI] AWS EC2 ์ธ์Šคํ„ด์Šค ๋ชฉ๋ก ๋ฐ์ดํ„ฐ easyํ•˜๊ฒŒ ์ถœ๋ ฅํ•˜๊ธฐ (export data to CSV/JSON)

2022. 8. 2. 22:04ใ†AWS

โ˜„๏ธ ๋ฌธ์ œ

EC2 ์ธ์Šคํ„ด์Šค ๋ชฉ๋ก์„ ์ถ”์ถœํ•ด์•ผ ํ–ˆ๋Š”๋ฐ,

๋ณด์•ˆ ๊ทธ๋ฃน ์ฝ˜์†”์˜ export security groups to CSV ๋ฉ”๋‰ด์™€ ๊ฐ™์ด EC2 ์ธ์Šคํ„ด์Šค ๋ชฉ๋ก์„ ์ถ”์ถœํ•˜๋Š” ๊ธฐ๋Šฅ์€ EC2 ์ฝ˜์†”์—๋Š” ์—†์—ˆ๋‹ค.

AWS Security Group์—์„œCSV๋กœ ๋ฐ์ดํ„ฐ ์ถ”์ถœํ•˜๋Š” ๊ฒฝ์šฐ

์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ธ์Šคํ„ด์Šค ๋ชฉ๋ก ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ผ๋กœ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์„์ง€ ์•Œ์•„๋ณด์ž.

 


 

 

๐Ÿ”ซ Trouble Shooting

AWS CLI์˜ EC2 Command๋ฅผ ์ด์šฉํ•˜๊ฑฐ๋‚˜ AWS Config์˜ Advanced Queries ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

AWS Config๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• 2๋ฒˆ์€ EC2์— ํ•œ์ •๋˜์ง€ ์•Š๊ณ  AWS ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

๊ฐ์ž ํŽธํ•œ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•ด๋ณด์•„์š”~

๋ฐฉ๋ฒ• 1. AWS CLI ์ด์šฉํ•˜๊ธฐ

ec2 describe-instances --output text
	--query 'Reservations[*].Instances[*].[InstanceId, InstanceType, ImageId, State.Name, LaunchTime, Placement.AvailabilityZone, Placement.Tenancy, PrivateIpAddress, PrivateDnsName, PublicDnsName, [Tags[?Key==`Name`].Value] [0][0], [Tags[?Key==`purpose`].Value] [0][0], [Tags[?Key==`environment`].Value] [0][0], [Tags[?Key==`team`].Value] [0][0] ]'
    > instances.csv

๋ฐฉ๋ฒ• 2. AWS Config ์ด์šฉํ•˜๊ธฐ

AWS Config

AWS Config๋ž€?

  • AWS ๊ณ„์ •์˜ ๋ฆฌ์†Œ์Šค ๊ตฌ์„ฑ์„ ์„ธ๋ถ€์ ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋Š” AWS ์ธก์ •, ๊ฐ์‚ฌ, ํ‰๊ฐ€ ์„œ๋น„์Šค
  • ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”์ง€, ๊ฐ๊ฐ์˜ ๋ฆฌ์†Œ์Šค๊ฐ€ ์„œ๋กœ ์–ด๋–ป๊ฒŒ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š”์ง€ ๊ทธ๋ฆฌ๊ณ  ์‹œ๊ฐ„์— ๋”ฐ๋ผ ์–ด๋–ป๊ฒŒ ๊ตฌ์„ฑ๊ณผ ์—ฐ๊ฒฐ๊ด€๊ณ„๊ฐ€ ๋ฐ”๋€Œ์—ˆ๋Š”์ง€๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
  • Config๋Š” AWS ๋ฆฌ์†Œ์Šค ๊ตฌ์„ฑ์„ ์ง€์†์ ์œผ๋กœ ๋ชจ๋‹ˆํ„ฐ๋ง ๋ฐ ๊ธฐ๋กํ•˜๊ณ , ์›ํ•˜๋Š” ๊ตฌ์„ฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ธฐ๋ก๋œ ๊ตฌ์„ฑ์„ ์ž๋™์œผ๋กœ ํ‰๊ฐ€ํ•œ๋‹ค.
  • AWS ๋ฆฌ์†Œ์Šค ๊ฐ„ ๊ตฌ์„ฑ ๋ฐ ๊ด€๊ณ„ ๋ณ€ํ™”๋ฅผ ๊ฒ€ํ† ํ•˜๊ณ , ์ž์„ธํ•œ ๋ฆฌ์†Œ์Šค ๊ตฌ์„ฑ ๊ธฐ๋ก์„ ๋ถ„์„ํ•˜๊ณ , ๋‚ด๋ถ€ ์ง€์นจ์— ์ง€์ •๋˜์–ด ์žˆ๋Š” ๊ตฌ์„ฑ์„ ๊ธฐ์ค€์œผ๋กœ ์ „๋ฐ˜์ ์ธ ๊ทœ์ • ์ค€์ˆ˜ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

AWS Config > Advanced queries๋ž€?

AWS Config > Advanced queries

  • ๊ตฌ์„ฑ ์†์„ฑ์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ AWS ๋ฆฌ์†Œ์Šค์˜ ํ˜„์žฌ ๊ตฌ์„ฑ ์ƒํƒœ์— ๋Œ€ํ•ด ์งˆ์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ ‘์†ํ•ด์žˆ๋Š” ํ•ด๋‹น ๋ฆฌ์ „๊ณผ ๊ณ„์ •์—์„œ ์งˆ์˜๊ฐ€ ๊ฐ€๋Šฅํ•˜๋ฉฐ, aggregator ์ด์šฉ ์‹œ ์ค‘์•™ ๊ณ„์ •์—์„œ ๋‹ค์ˆ˜์˜ ๋ฆฌ์ „๊ณผ ๋‹ค์ˆ˜์˜ ๊ณ„์ •์— ์งˆ์˜ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • SQL์˜ SELECT ๊ตฌ๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์ฟผ๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ง€์›๋˜๋Š” ํ‚ค์›Œ๋“œ๋Š” ์•„๋ž˜ ๋งํฌ์—์„œ ์‚ดํŽด๋ณผ ์ˆ˜ ์žˆ๋‹ค.
  • AWS ์„œ๋น„์Šค๋ณ„ API ํ˜ธ์ถœ์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ ๋„ ํ•„์š”ํ•œ ํ˜„์žฌ ๋ฆฌ์†Œ์Šค ์ƒํƒœ๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ์ฟผ๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฆฌ์†Œ์Šค ์ข…๋ฅ˜์™€ ๋ฆฌ์†Œ์Šค ์†์„ฑ์€ ๋‹ค์Œ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅ : โญ๏ธgithubโญ๏ธ

1. Advanced queries์—์„œ ์ƒˆ ์ฟผ๋ฆฌ ์„ ํƒ

AWS Config > Advanced queries

  • Advanced queries ์ฝ˜์†”์—์„œ New query ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์ƒˆ ์ฟผ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

2. query scope ์„ ํƒ ๋ฐ ์›ํ•˜๋Š” ์ฟผ๋ฆฌ ์งˆ์˜

AWS Config > Advanced queries > Query Ediitor

// ๊ณ„์ • ID, EC2 ์ธ์Šคํ„ด์Šค ID, ์ธ์Šคํ„ด์Šค ํƒ€์ž…, ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ์‹œ๊ฐ„
SELECT
  accountId,
  resourceId,
  configuration.instanceType,
  resourceCreationTime
WHERE
  resourceType = 'AWS::EC2::Instance'
ORDER BY
  accountId
  • ํ™”๋ฉด ์ขŒ์ธก์˜ [Query Scope]์—์„œ ๊ตฌ์„ฑํ•ด๋†“์€ aggregator๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, aggregator๋ฅผ ์„ ํƒํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ณ„์ •๊ณผ ๋ฆฌ์ „์—์„œ ์ฟผ๋ฆฌ๋ฅผ ์งˆ์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • resourceType์ด AWS::EC2::Instance์ธ ๊ฒฝ์šฐ ์ฐธ๊ณ  : github
  • [Export as] ๋ฒ„ํŠผ ์„ ํƒ ์‹œ, JSON๊ณผ CSV ์ค‘ ์ถ”์ถœ ํƒ€์ž…์„ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด EC2 ์ธ์Šคํ„ด์Šค ๋ชฉ๋ก ๋ฐ์ดํ„ฐ ์ถ”์ถœ ์™„๋ฃŒ~~!

 

 

 


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

  1. AWS Document: What Is AWS Config?
  2. AWS Document: Querying the Current Configuration State of AWS Resources
  3. Github: aws-config-resource-schema