AWS
[Python] AWS EC2 인스턴스 ID 출력
freesunny
2019. 7. 13. 21:21
python 으로 EC2 인스턴스 ID를 출력하는 방법이다.
> boto3 모듈이 설치되어 있어야 한다.
> aws credentials 설정이 되어 있어야 한다. (aws configure 로 설정 가능)
import boto3
ec2 = boto3.resource('ec2')
for instance in ec2.instances.all():
print(instance.id)
다른 리전의 정보를 가지고 오기 위해서는 region_name 을 설정하면 된다.
ec2 = boto3.resource('ec2', region_name='ap-southeast-1')
정지된 인스턴스의 정보를 출력할 수도 있다.
for instance in ec2.instances.all():
if instance.state['Name'] == 'stopped':
print(instance.id)
실행중인 인스턴스의 정보를 출력할 수도 있다.
for instance in ec2.instances.all():
if instance.state['Name'] == 'running':
print(instance.id)
실행중인 인스턴스 ID를 배열에 넣기
array = []
for instance in ec2.instances.all():
if instance.state['Name'] == 'running':
array.append(instance.id)