EC2 태깅 MKII
volumes
및 snapshots
를 찾기 위해 인스턴스 ID를 사용하도록 스크립트를 조정했습니다.또한 AWS 구성 파일에서 사용할 계정을 동적으로 가져옵니다.
즐거운 시간 보내세요
#!/bin/bash
## Functions
tag_rescources () {
aws --profile=$profile --region=$region ec2 create-tags --resources $1 \
--tags Key="COST CENTRE",Value="$cost" \
Key="APP",Value="$app" \
Key="ENVIRONMENT",Value="$environment" \
Key="OWNER",Value="$owner"
}
get_volumes (){
aws --profile=$profile --region=$region ec2 describe-volumes \
--filters Name=attachment.instance-id,Values=$1 \
--query "Volumes[].VolumeId" --output=text
}
get_snapshots(){
aws --profile=$profile --region=$region ec2 describe-snapshots \
--filters Name=volume-id,Values=$1 \
--query "Snapshots[].SnapshotId" --output=text
}
## Script
echo "Use this script to tag EC2 instance in the desired account"
echo "Multiple can be enter at once seperated by a singe space."
echo "Below resources are supported using the ID"
echo " Instance ID"
echo " Security Group ID"
echo " Elastic IPs Allocation ID"
echo " "
echo "----------------------------------------------------------- "
echo "Please choose AWS Account Profile"
select profile in `grep '^\[' ~/.aws/config|sed -r 's/\[|\]//g'|awk '{print $2}'`
do
echo "Please choose AWS Region"
select region in eu-west-1 eu-central-1 us-east-1
do
echo "Please list EC2 Instance ID"
echo "Multiple can be entered at once, seperated by a singe space."
read instance
echo "Please Enter COST CENTRE"
read cost
echo "Please Enter APP"
read app
echo "Please Enter ENVIRONMENT"
read environment
echo "Please Enter OWNER"
read owner
## Tagging EC2 Instances
for i in $instance
do
tag_rescources $i
echo "Tagging EC2 Instances $i"
done
## Tagging Volunmes
#! a nested loop is used as the get volume function can only filter one volume at a time
for i in $instance
do
for j in `get_volumes $i`
do
echo "Tagging Volume $j beloning to Instance $i"
tag_rescources $j
done
done
## Tagging Snapshots
#! a nested loop is used as the get snapshot function can only filter one volume at a time
volumes=`get_volumes $instance`
for i in $volumes
do
for j in `get_snapshots $i`
do
echo "Tagging SnapShot $j belonging to Volume $i"
tag_rescources $j
done
done
echo "If no error's above tagging complete"
break
done
break
done
Reference
이 문제에 관하여(EC2 태깅 MKII), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/paulmicheli/ec2-tagging-mkii-1p5h텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)