본문 바로가기

퍼블릭 클라우드

AWS CLI를 사용하여 Amazon EC2 인스턴스 리스트 출력

반응형

AWS CLI를 사용하여 Amazon EC2 인스턴스 리스트 출력(aws ec2 describe-instances)

태그 입력 전 - 출력

aws --region ap-northeast-2 ec2 describe-instances \
--query 'Reservations[*].Instances[*].[Tags[0].Value, \
PrivateIpAddress, State.Name, InstanceType, InstanceId, Placement.AvailabilityZone]' \
--output=table
  • ec2 describe-instances : EC2 인스턴스의 상세 정보를 가져오는 명령입니다.
  • --region ap-northeast-2 : 명령이 실행될 리전을 지정합니다. 여기서는 서울 리전(ap-northeast-2)으로 지정되어 있습니다.
  • --query 'Reservations[*].Instances[*].[Tags[0].Value, PrivateIpAddress, State.Name, InstanceType, InstanceId, Placement.AvailabilityZone]' : --query 옵션을 사용하여 출력 형식을 지정합니다. 여기서는 테이블 형식으로 원하는 속성을 선택하여 출력합니다.
  • --output=table : 결과를 테이블 형식으로 출력하도록 지정합니다.
$ aws --region ap-northeast-2 ec2 describe-instances \
--query 'Reservations[*].Instances[*].[Tags[0].Value, \
PrivateIpAddress, State.Name, InstanceType, InstanceId, Placement.AvailabilityZone]' \
--output=table
------------------------------------------------------------------------------------------------------------
|                                             DescribeInstances                                            |
+--------------------+----------------+----------+-------------+-----------------------+-------------------+
|  web11             |  10.99.3.20    |  stopped |  t3a.medium |  i-01899448123456789  |  ap-northeast-2a  |
|  web12             |  10.99.92.7    |  stopped |  t3a.large  |  i-0197ed40123456789  |  ap-northeast-2c  |
+--------------------+----------------+----------+-------------+-----------------------+-------------------+

태그 입력 후 - 출력

: 태그 입력 후 호스명 필드에 태그에 첫 번째 값을 가져와서 tag name으로 호스트명을 출력함

aws --region ap-northeast-2 ec2 describe-instances \
--query 'Reservations[*].Instances[*].[join(`,`,Tags[?Key==`Name`].Value), \
PrivateIpAddress, State.Name, InstanceType, InstanceId, Placement.AvailabilityZone]' \
--output=table
  • ec2 describe-instances : EC2 인스턴스의 상세 정보를 가져오는 명령입니다.
  • --region ap-northeast-2 : 명령이 실행될 리전을 지정합니다. 여기서는 서울 리전(ap-northeast-2)으로 지정되어 있습니다.
  • --query 'Reservations[*].Instances[*].[join(,,Tags[?Key==Name].Value), PrivateIpAddress, State.Name, InstanceType, InstanceId, Placement.AvailabilityZone]' : --query 옵션을 사용하여 출력 형식을 지정합니다. 여기서는 'Name' 키에 해당하는 태그 값을 쉼표로 결합하여 출력하도록 지정했습니다.
  • --output=table : 결과를 테이블 형식으로 출력하도록 지정합니다.
$ aws --region ap-northeast-2 ec2 describe-instances \
--query 'Reservations[*].Instances[*].[join(`,`,Tags[?Key==`Name`].Value), \
PrivateIpAddress, State.Name, InstanceType, InstanceId, Placement.AvailabilityZone]' \
--output=table
------------------------------------------------------------------------------------------------------------
|                                             DescribeInstances                                            |
+--------------------+----------------+----------+-------------+-----------------------+-------------------+
|  web11             |  10.99.3.20    |  stopped |  t3a.medium |  i-01899448123456789  |  ap-northeast-2a  |
|  web12             |  10.99.92.7    |  stopped |  t3a.large  |  i-0197ed40123456789  |  ap-northeast-2c  |
+--------------------+----------------+----------+-------------+-----------------------+-------------------+
728x90

여러 지역의 EC2 및 RDS 인스턴스 정보를 HTML 파일로 생성하는 스크립트(ec2, rds, s3)

vim awsgetlist.sh
#!/bin/bash

AWS_CONFIG_FILE="$HOME/.aws/config"
HTML_FILE="/usr/share/zabbix/aws/list.html"
BACKUP_DIR="/usr/share/zabbix/aws/.bk"
DATE_SUFFIX=$(date '+%Y%m%d-%H%M')

# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"

# Backup existing HTML file
cp "$HTML_FILE" "$BACKUP_DIR/list_$DATE_SUFFIX.html"

Region=("ap-northeast-2" "ap-southeast-1" "us-east-1")

AmazonElasticComputeCloud() {
    for region in "${Region[@]}"; do
        echo "$region"
        aws --region "$region" ec2 describe-instances \
            --query 'Reservations[*].Instances[*].[join(`,`,Tags[?Key==`Name`].Value), PrivateIpAddress, State.Name, InstanceType, InstanceId, Placement.AvailabilityZone]' \
            --output table
    done
}

AmazonRelationalDatabaseService() {
    for region in "${Region[@]}"; do
        echo "$region"
        aws --region "$region" rds describe-db-instances \
            --query 'DBInstances[*].[DBInstanceIdentifier,Endpoint.Address,DBInstanceStatus,DBInstanceClass,DbiResourceId,AvailabilityZone]' \
            --output table
    done
}

# HTML generation
cat <<EOF > "$HTML_FILE"
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
  table {
    width: 80%;
    border-top: 1px solid #444444;
    border-collapse: collapse;
    font-family: Monaco;
    font-size: 90%;
  }
  th, td {
    border-bottom: 1px solid #444444;
    padding: 10px;
    text-align: center;
  }
  th {
    background-color: #e3f2fd;
  }
  td {
    background-color: #FFFFFF;
  }
  pre {
    font-family: Monaco;
    font-size: 80%;
  }
</style>
</head>
<body>
<font face="Monaco">
EOF

cat <<EOF >> "$HTML_FILE"
<p style="text-align:right;">
web page update time : $(date '+%Y-%m-%d %H:%M:%S')
</p>
EOF

echo "<h3>Amazon Elastic Compute Cloud(EC2)</h3><pre>" >> "$HTML_FILE"
AmazonElasticComputeCloud >> "$HTML_FILE"

echo "</pre><br><h3>Amazon Relational Database Service(RDS)</h3><pre>" >> "$HTML_FILE"
AmazonRelationalDatabaseService >> "$HTML_FILE"

echo "</pre><br><h3>Amazon Simple Storage Service(S3)</h3><pre>" >> "$HTML_FILE"
aws s3 ls >> "$HTML_FILE"

cat <<EOF >> "$HTML_FILE"
</pre>
<br><br><br>
<h3>MONACO.TTF font download link</h3>
<a href="MONACO.TTF" download>Download it here for free</a>
</font>
</body>
</html>
EOF

echo "HTML file has been generated and saved to: $HTML_FILE"

 

더보기

---

#!/bin/bash -l

AWS_CONFIG_FILE="~/.aws/config"

cd /usr/share/zabbix/aws/

cp list.html .bk/list_$(date '+%Y%m%d-%H%M').html

Region="
ap-northeast-2
ap-southeast-1
us-east-1
"

AmazonElasticComputeCloud(){
for i in $Region
do
	echo ${i}
	aws --region ${i} ec2 describe-instances --query 'Reservations[*].Instances[*].[join(`,`,Tags[?Key==`Name`].Value), PrivateIpAddress, State.Name, InstanceType, InstanceId, Placement.AvailabilityZone]' --output=table
done
}

AmazonRelationalDatabaseService(){
for i in $Region
do
        echo ${i}
        aws --region ${i} rds describe-db-instances --query 'DBInstances[*].[DBInstanceIdentifier,Endpoint.Address,DBInstanceStatus,DBInstanceClass,DbiResourceId,AvailabilityZone]' --output=table
done
}

### html
cat <<EOF > list.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html\; charset=utf-8" />
<style>
  table {
    width: 80%;
    border-top: 1px solid #444444;
    border-collapse: collapse;
    font-family: Monaco;
    font-size:90%;
  }
  th, td {
    border-bottom: 1px solid #444444;
    padding: 10px;
    text-align: center;
  }
  th {
    background-color: #e3f2fd;
  }
  td {
    background-color: #FFFFFF;
  }
  pre {
    font-family: Monaco;
    font-size:80%;
  }
</style>
</head>
<body>
<font face="Monaco">
EOF

cat <<EOF >> list.html
<p style="text-align:right;">
web page update time : `date '+%Y-%m-%d %H:%M:%S'`
</p>
EOF

echo "<h3>Amazon Elastic Compute Cloud(EC2)</h3><pre>" >> list.html
AmazonElasticComputeCloud >> list.html

echo "</pre><br><h3>Amazon Relational Database Service(RDS)</h3><pre>" >> list.html
AmazonRelationalDatabaseService >> list.html

echo "</pre><br><h3>Amazon Simple Storage Service(S3)</h3><pre>" >> list.html
aws s3 ls >> list.html

cat <<EOF >> list.html
</pre>
<br><br><br>
<h3>MONACO.TTF font download link</h3>
<a href="MONACO.TTF" download>Download it here for free</a>
</font>
</body>
</html>
EOF

---

 

728x90
반응형