티스토리 뷰

___퍼블릭 클라우드

[AWS] ec2 인스턴스 리스트 출력

변군 변군이글루 2020. 10. 21. 17:37

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
------------------------------------------------------------------------------------------------------------
|                                             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
------------------------------------------------------------------------------------------------------------
|                                             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  |
+--------------------+----------------+----------+-------------+-----------------------+-------------------+

 

ec2, rds, s3 html로 리스팅

$ vim awsgetlist.sh
#!/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
댓글
댓글쓰기 폼