aws ์ ์ดํ์์ cli๋ก ์ํฌํ๋ก๋ฅผ ์ ํํ๋ ค๊ณ ํ๋๋ฐ ๋งค์ฐ ์ด์ํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ง์ญ ๋ด ์ธ์คํด์ค๋ฅผ ์ค๋ช
ํ๋ ๋ฐฉ๋ฒ์ ์๊ณ ์์ง๋ง ์ง์ญ์ ๊ด๊ณ์์ด ๋ชจ๋ ์ธ์คํด์ค๋ฅผ ์ค๋ช
ํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผ ํฉ๋๊น? ๋๋ ์ ์ด๋ ๋ชจ๋ ec2 ์์ญ์ ์ถ๋ ฅ์ ์ป์ด ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก ํ๋์ฉ(์: aws ec2 list-regions
) ์ด ์์
์ ์ํํ ์ ์์ต๋๋ค.
cli๊ฐ ์ ์ดํ๊ณผ ๋ค๋ฅธ ์์ญ ์ด๋ฆ์ ์ฌ์ฉํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๊ธฐ ๋๋ฌธ์ ๋งค์ฐ ์ด์ํฉ๋๋ค. ํ๋๋ ๋ถ๋ช ํ ๋ด ๋ชจ๋ ์ธ์คํด์ค๊ฐ "us-west-2" ์ง์ญ์์ ์์๋๊ณ ์์ง๋ง(์ ์ด๋ ์ง๊ธ๊น์ง๋) ์ ์ดํ ์ด๋์์๋ ํด๋น ๋ฌธ์์ด์ ์ฐพ์ ์ ์๋ค๋ ๊ฒ์ ๋๋ค. ์ฌ์ค, ๋ด ๊ธฐ๋ณธ ์ง์ญ์ ๋์ดํ๋ ๊ณณ์ ์ ํ ์ฐพ์ ์ ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
aws ๋ด๋ถ์ ์ด ์์ ์ ์ํํ๋ ๋ช ๋ น์ด ์์ต๋๊น?
์ฝ์์์๋ ์ง์ญ์ ๊ด๊ณ์์ด ๋ชจ๋ ์ธ์คํด์ค๋ฅผ ๋์ดํ ์ ์์ง๋ง ๋ ์์ธํ ์ค๋ช
ํ
์คํธ๋ก ์ง์ญ ์๋ณ์๋ฅผ ์จ๊น๋๋ค. us-west-2
๋ ์๋ฅผ ๋ค์ด ์ค๋ฆฌ๊ฑด์ ์์นํ ๋ฐ์ดํฐ ์ผํฐ์
๋๋ค. ์ฝ์ ํ์ด์ง์ ์ค๋ฅธ์ชฝ ์๋จ์ ์ง์ญ์ ์ ํํ ์ ์๋ ๋๋กญ๋ค์ด์ด ์์ต๋๋ค(์๋ ๊ทธ๋ฆผ ์ฐธ์กฐ).
๋๋ถ๋ถ์ ์๋น์ค์ ๊ฒฝ์ฐ ํ์ฌ ์ฌ์ฉ ๊ฐ๋ฅํ ์ง์ญ์ ๊ฒ์ํ๋ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ด ์์ผ๋ฏ๋ก ์ด ๋ชฉ๋ก ์ ์ฐธ์กฐํด์ผ describe-regions
๊ธฐ๋ฅ์ ์ ๊ณตํ๋ฏ๋ก ์์ธ ์ค ํ๋์
๋๋ค. CLI๋ ์๋น์ค API์ ๋ํ ๋ํผ์ด๋ฏ๋ก ์๋น์ค๊ฐ ์ง์ํ์ง ์๋ ํ ๋ค์ค ์ง์ญ ๊ฒ์์ ์ํํ ์ ์์ต๋๋ค(์๋ฅผ ๋ค์ด S3๋ ๋๋ถ๋ถ ์ง์ญ์ ๊ตฌ์ ๋ฐ์ง ์์ต๋๋ค). ๋ค๋ฅธ ์ง์ญ์์ ์ฟผ๋ฆฌํ๋ ค๋ฉด ์ง์ญ์ --region
์ธ์๋ฅผ ์ ๊ณตํด์ผ ํฉ๋๋ค.
๊ธฐ๋ณธ ์ง์ญ์ ๊ตฌ์ฑ ํ์ผ(UNIX ์์คํ
์ ๊ฒฝ์ฐ ~/.aws/config
์ ์์ผ๋ฉฐ aws configure
๋ช
๋ น์ผ๋ก ํ์ํ๊ณ ์ค์ ํ ์ ์์ต๋๋ค.
Google์์ ์ฌ๊ธฐ๋ก ์ค๋ ์ฌ๋๋ค์ ์ํด ๋ค์์ AWS CLI๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ ์ง์ญ์ ๋ชจ๋ ์ธ์คํด์ค๋ฅผ ๋์ดํ๋ ํ ๊ฐ์ง ๋ฐฉ๋ฒ์ ๋๋ค(์์ ๋ ผ์๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํจ).
for region in `aws ec2 describe-regions --output text | cut -f3`
do
echo -e "\nListing Instances in region:'$region'..."
aws ec2 describe-instances --region $region
done
๋์ผ๋ก JSON์ ๊ตฌ๋ฌธ ๋ถ์ํ๊ณ ์ถ์ง ์์ ๋ถ๋ค์ ์ํด jq ๋ฅผ ์ฌ์ฉํ์ฌ ์ข ๋ ์ ํํ ์์ฝ์ ์ ๊ณตํ๋ ์ถ๊ฐ ๋ฒ์ ์ด ์์ต๋๋ค.
for region in `aws ec2 describe-regions --output text | cut -f3`
do
echo -e "\nListing Instances in region:'$region'..."
aws ec2 describe-instances --region $region | jq '.Reservations[] | ( .Instances[] | {state: .State.Name, name: .KeyName, type: .InstanceType, key: .KeyName})'
done
์ถ๋ ฅ ์:
Listing Instances in region:'us-west-1'...
{
"state": "stopped",
"name": "scanner-1",
"type": "t2.micro",
"key": "scan1"
}
{
"state": "stopped",
"name": "cors",
"type": "t2.micro",
"key": "cors"
}
{
"state": "stopped",
"name": "scanner-2",
"type": "t2.medium",
"key": "scan2"
}
@nonbeing ๋ฉ์ง๋ค ! ์์ฃผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ๋ณ์นญ์ผ๋ก ๋ง๋๋ ๊ฒ์ ๊ณ ๋ คํด์ผ ํฉ๋๋ค.
๋ ๋ค๋ฅธ ์ต์ ์ Linux, Mac ๋ฐ Windows์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์คํ๋๋ AWS PowerShell .NET Core ๋ชจ๋์ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ค.
foreach ($Region in (Get-AWSRegion)) {
Get-EC2Instance -Region $Region.Region
}
GroupNames : {}
Groups : {}
Instances : {asdf}
OwnerId : 123
RequesterId : 123
ReservationId : r-asdf
GroupNames : {}
Groups : {}
Instances : {asdf}
OwnerId : 123
RequesterId : 123
ReservationId : r-asdf
GroupNames : {}
Groups : {}
Instances : {asdf}
OwnerId : 123
RequesterId : 123
ReservationId : r-asdf
๊ธฐ๋ณธ์ ์ผ๋ก ํ์ฌ ๋ฒ์ ์ AWS PowerShell ๋ชจ๋์์ EC2 ์ธ์คํด์ค๋ PowerShell "๋ชฉ๋ก" ํ์์ผ๋ก ๋ฐํ๋ฉ๋๋ค. ๊ทธ๋ฌ๋ Format-Table
๋ช
๋ น์ ์ฌ์ฉํ์ฌ ํ ํ์์ผ๋ก ์ฝ๊ฒ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
$InstanceList = @()
foreach ($Region in (Get-AWSRegion)) {
$InstanceList += Get-EC2Instance -Region $Region.Region -ProfileName CIDBTest
}
$InstanceList | Format-Table
GroupNames Groups Instances OwnerId RequesterId ReservationId RunningInstance
---------- ------ --------- ------- ----------- ------------- ---------------
{} {} {asdf222} 123 123 r-123 {asdf222}
{} {} {asdf222} 123 123 r-123 {asdf222}
{} {} {asdf222} 123 123 r-123 {asdf222}
์ข
์์ฑ์ ์ถ๊ฐํ๋ ค๋ ๊ฒฝ์ฐ jq
์ฌ์ฉํ๊ฑฐ๋ AWS CLI์์ ๊ธฐ๋ณธ ์ ๊ณต --query
๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
aws --profile whatever ec2 describe-instances --query 'Reservations[].Instances[].[State.Name,InstanceType]'
[
[
"running",
"t2.small"
],
[
"running",
"t2.small"
],
[
"running",
"m3.medium"
]
]
์ฝ๋ ๋์ -rr ; ์์ฝ ์์ ์์ญ: $r ; ์ฝ๋ ๋์ ์ํ -ri ; echo instance-id: $i 2>&1 ; ์๋ฃ < <(aws ec2 describe-instances --query "Reservations[].Instances[].InstanceId" --region $r --profile prd|perl -nl -e 's/\s+/\n/g;print '); echo "์ ์ง ์ง์ญ: $r" ; ์๋ฃ < <(aws ec2 describe-regions --output text --profile prd| cut -f 3)
for region in `aws ec2 describe-regions --output text | cut -f3`
do
echo -e "\nListing Instances in region:'$region'..."
aws ec2 describe-instances --region $region | jq '.Reservations[] | ( .Instances[] | {state: .State.Name, name: .KeyName, Tag_Name: .Tags[].Value,type: .InstanceType, key: .KeyName})'
done
๋ฐฉ๊ธ ์ด ๋ ๋ค๋ฅธ ์๋ฃจ์
์
๋๋ค!
์ด ์์ ์คํฌ๋ฆฝํธ๋ ๋ชจ๋ ์ง์ญ์์ ๋ช
๋ น์ ์คํํ ์ ์์ต๋๋ค.
์ฌ์ฉ ์:
sudo ./aws-x.sh ec2 ์ค๋ช
์ธ์คํด์ค
sudo ./aws-x.sh ec2 ์ธ์คํด์ค ์ค๋ช
| grep PrivateDnsName
echo "AWS ์ฌ์ฉ ๊ฐ๋ฅํ ๋ฆฌ์ ๊ฐ์ ธ์ค๊ธฐ:"
sudo aws ec2 describe-regions --region eu-west-2 |grep RegionName |cut -d '"' -f 4 > /tmp/regions.txt
๊ณ ์์ด /tmp/regions.txt
์์ญ์ ์ฝ๋ ๋์; ํ๋ค
echo "" $region "์์ " $@ " ์คํ ์ค:"
aws --์ง์ญ $์ง์ญ $@
์๋ฃ
rm /tmp/regions.txt
echo ๋ชจ๋ ์ง์ญ์์ ์ธ์คํด์ค๋ฅผ ๊ฐ์ ธ์ค๋ ์ค์
๋๋ค. ์ ์๋ง ๊ธฐ๋ค๋ ค ์ฃผ์ญ์์ค.
aws ec2 describe-regions --output text | cut -f3
ํ๋ค
aws ec2 describe-instances --region $region --query 'Reservations[].Instances[].[State.Name,InstanceType,PublicIpAddress,Placement.AvailabilityZone]' --์ถ๋ ฅ ํ
์คํธ
์๋ฃ
๋์ผ๋ก JSON์ ๊ตฌ๋ฌธ ๋ถ์ํ๊ณ ์ถ์ง ์์ ๋ถ๋ค์ ์ํด jq ๋ฅผ ์ฌ์ฉํ์ฌ ์ข ๋ ์ ํํ ์์ฝ์ ์ ๊ณตํ๋ ์ถ๊ฐ ๋ฒ์ ์ด ์์ต๋๋ค.
aws cli๋ ์ด ์์ ์ ๋ด์ฅ์ผ๋ก ์ํํ๋
๊ณ ๋ง์์ ์ค๋น์! ์ ๋ ๋ช ๋ฌ ์ ์ jq๋ฅผ ์ฌ์ฉํ๊ธฐ ์์ํ๊ณ ์ ์ธ์์ ๋ฐ๊ฟจ์ต๋๋ค. :)
AWS Cli Docs ๋ ๊ณ ๊ธ ์ฌ์ฉ์ ์ํด ์ฌ์ ํ JQ๋ฅผ ๊ถ์ฅํฉ๋๋ค.
--query๋ก ๊ฐ๋ฅํ์ง ์์ ์ ์๋ ๊ณ ๊ธ ๊ธฐ๋ฅ์ด ๋ ํ์ํ ๊ฒฝ์ฐ ๋ช ๋ น์ค JSON ํ๋ก์ธ์์ธ jq๋ฅผ ํ์ธํ ์ ์์ต๋๋ค. http://stedolan.github.io/jq/ ์์ ๋ค์ด๋ก๋ํ๊ณ ๊ณต์ ํํ ๋ฆฌ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค
IMHO, --query
๋ AWS CLI์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ง์ํ๊ธฐ ๋๋ฌธ์ ์ฟผ๋ฆฌ ์ฌ์ฉ ์ฌ๋ก์ ์ ํฉํ์ง ์ฌ์ ํ ์ดํด๋ณผ ๊ฐ์น๊ฐ ์์ต๋๋ค. ์ด ์ต์
์ 2013/10/18 ์ ๋ฆด๋ฆฌ์ค๋ aws cli v. 1.2.0 ์
--query
๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉ ํ๋ ค๋ ๊ฒฝ์ฐ ์ฟผ๋ฆฌ ์ธ์ด๋ก ํํ ๋ฆฌ์ผ ๋ ์ถฉ๋ถํ ๊ฐ์น๊ฐ ์์ต๋๋ค.
๋ค์ ์ฟผ๋ฆฌ๋ aws cli๋ฅผ ์ฌ์ฉํ์ฌ ๊ธฐ๋ณธ ๋ฆฌ์ ์ RDS ์ธ์คํด์ค๋ฅผ ๋์ดํ๊ณ JQ ๋ฐ JMESPath๋ฅผ ์ฌ์ฉํ์ฌ json ์ถ๋ ฅ์ ๊ตฌ๋ฌธ ๋ถ์ํฉ๋๋ค.
์๋ฅผ ๋ค์ด, ์ ์ฌํ ์ฟผ๋ฆฌ๋ JMESPath์์ ํจ์ฌ ๋ ์ฝ๊ธฐ ์ฌ์ด OOTB์ธ ๊ฒ ๊ฐ์ต๋๋ค.
# Using Jquery
aws rds describe-db-instances --output json | jq -r '.DBInstances[] | "\(.DBInstanceIdentifier) -> \(.DBInstanceStatus) ( \(.DBInstanceClass), \(.Endpoint.Address):\(.Endpoint.Port) ) " '
## Outputs
someinstancename -> available ( db.t2.small, xxxxx.rds.amazonaws.com:1234 )
## Show a list of properties for DBInstances which have a status containing the word 'avail'
aws rds describe-db-instances --output json --query "DBInstances[?contains(DBInstanceStatus, 'avail')].[DBInstanceIdentifier, DBInstanceClass, DBInstanceStatus, Endpoint.[Address, Port]]"
## Outputs
[
[
"someinstancename",
"db.t2.small",
"available",
[
"xxxxx.rds.amazonaws.com",
1234
]
]
]
opt-in-not-required
๊ฐ aws ec2 describe-regions --output text
๋ฅผ ์คํํ ๋ ์ถ๋ ฅ์ ์ธ ๋ฒ์งธ ์ด์ด๋ฉด ๋ค์๊ณผ ๊ฐ์ด cut -f3
๋ฅผ cut -f4
๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
for region in `aws ec2 describe-regions --output text | cut -f4`
do
done
์๋ง๋ ๋ ๊ฐ๋ ฅํ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ด JMESPath ์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ค.
for region in $(aws ec2 describe-regions --query "Regions[*].RegionName" --output text)
do
done
grep
ํ์ฌ ์ํ๋ ์์ญ์ผ๋ก ํํฐ๋งํ๊ธฐ ์ํด ์ถ๊ฐ ๊ตฌ๋ฌธ ๋ถ์์ ์ํํ ์๋ ์์ต๋๋ค.
์๋ฅผ ๋ค์ด awscli-aliases ๋ฅผ ์ค์นํ ํ ~/.aws/cli/alias
์ ๋ค์์ ์ถ๊ฐํ์ฌ ์ธ์คํด์ค๋ฅผ ๋์ดํฉ๋๋ค.
list-instances =
!f() {
echo "Filtering by \`$1\`"
for region in $(aws ec2 describe-regions --query "Regions[*].RegionName" --output text | tr "\t" "\n" | grep "$1")
do
echo "\nListing Instances in region:'$region'..."
aws ec2 describe-instances --region $region \
--output table \
--query "Reservations[*].Instances[*].{
Instance:InstanceId,
Type:InstanceType,
AZ:Placement.AvailabilityZone,
KeyName:KeyName,
Name:Tags[?Key==\`Name\`]|[0].Value,
Project:Tags[?Key==\`project\`]|[0].Value,
IP:PublicIpAddress,
State:State.Name,
CPUcores:CpuOptions.CoreCount,
CPUThreads:CpuOptions.ThreadsPerCore
}"
done
}; f
์ฉ๋ฒ:
$ aws list-instances
์ง์ญ ์ฝ๋์ ํํฐ๋ฅผ ์ ์ฉํฉ๋๋ค(์: ๋ฏธ๊ตญ ์ง์ญ์ ๊ฒฝ์ฐ us
.
$ aws list-instances 'us'
aws ec2 describe-regions --output text | cut -f3
ํ๋ค
echo -e "\n์์ญ์ ์ธ์คํด์ค ๋์ด:'$region'..."
aws ec2 ์ค๋ช
์ธ์คํด์ค --region $region | grep ์คํ > file.txt
๊ณ ์์ด ํ์ผ.txt | ํ์ฅ์ค -l
์๋ฃ
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
Google์์ ์ฌ๊ธฐ๋ก ์ค๋ ์ฌ๋๋ค์ ์ํด ๋ค์์ AWS CLI๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ ์ง์ญ์ ๋ชจ๋ ์ธ์คํด์ค๋ฅผ ๋์ดํ๋ ํ ๊ฐ์ง ๋ฐฉ๋ฒ์ ๋๋ค(์์ ๋ ผ์๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํจ).