https://github.com/seulseul627/aws-snapshot.git
PS C:\Users\Administrator\code\aws-snapshot> aws configure --profile shotty
AWS Access Key ID [None]: --
AWS Secret Access Key [None]: --
Default region name [None]: ap-northeast-2
Default output format [None]:
> pipenv --three
> ls
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/12/2020 2:29 PM 1928 .gitignore
-a---- 2/12/2020 3:46 PM 138 Pipfile
-a---- 2/12/2020 2:29 PM 63 README.md
boto3
> pipenv install boto3
https://aws.amazon.com/ko/blogs/korea/now-available-aws-sdk-for-python-3-boto3/
> pipenv install -d ipython
> pipenv run ipython
# AWS로 Session을 생성해서 Ec2 Instance 리스트 불러오기
In [1]: import boto3
In [2]: session = boto3.Session(profile_name='shotty')
In [3]: ec2 = session.resource('ec2')
In [4]: for i in ec2.instances.all():
...: print(i)
...:
ec2.Instance(id='i-###')
ec2.Instance(id='i-###')
ec2.Instance(id='i-###')
ec2.Instance(id='i-###')
=> 권한 부족으로 코드가 실패하는 경우, 해당 AWS 계정에 적절한 Policy(ex. AmazonEC2FullAccess)를 부여해주어야 한다.
shotty.py 생성
import boto3
if __name__ == '__main__':
session = boto3.Session(profile_name='shotty')
ec2 = session.resource('ec2')
for i in ec2.instances.all():
print(i)
`pipenv run "python shotty/shotty.py" -> 실행안됨.. 에러 확인 필요
python shotty/shotty.py -> 잘 실행됨
shotty.py 수정
import boto3
session = boto3.Session(profile_name='shotty')
ec2 = session.resource('ec2')
def list_instances():
for i in ec2.instances.all():
print(i)
if __name__ == '__main__':
list_instances()
instance list를 출력하는 부분을 함수화
아래 명령으로 스크립트 실행하여 결과 확인
> pipenv run python shotty/shotty.py
명령어 argument
import boto3
import sys
session = boto3.Session(profile_name='shotty')
ec2 = session.resource('ec2')
def list_instances():
for i in ec2.instances.all():
print(i)
if __name__ == '__main__':
print(sys.argv)
list_instances()
python 패키지 - $click
https://click.palletsprojects.com/en/7.x/
Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary. It’s the “Command Line Interface Creation Kit”. It’s highly configurable but comes with sensible defaults out of the box.
# click package를 이용해 --help 옵션 구현
import boto3
import click
session = boto3.Session(profile_name='shotty')
ec2 = session.resource('ec2')
@click.command()
def list_instances():
"List EC2 instances" # --help 옵션을 위해 추가
for i in ec2.instances.all():
print(i)
if __name__ == '__main__':
list_instances()
# Boto3 API를 이용한 Instance 정보 Get
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#instance
PS C:\Users\Administrator\code\aws-snapshot> ipython
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.9.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import boto3
In [2]: session = boto3.Session(profile_name='shotty')
...: ec2 = session.resource('ec2')
In [3]: ins = ec2.instances.all()
In [4]: ins
Out[4]: ec2.instancesCollection(ec2.ServiceResource(), ec2.Instance)
In [5]: list(ins)
Out[5]:
[ec2.Instance(id='i-123'),
ec2.Instance(id='i-345'),
ec2.Instance(id='i-456'),
In [6]: i = list(ins)[0]
In [8]: i.placement
Out[8]: {'AvailabilityZone': 'ap-northeast-2c', 'GroupName': '', 'Tenancy': 'default'}
In [9]: i.state
Out[9]: {'Code': 80, 'Name': 'stopped'}
click 모듈을 이용한 project 태그로 filter 걸기
import boto3
import click
session = boto3.Session(profile_name='shotty')
ec2 = session.resource('ec2')
@click.command()
@click.option('--project', default=None,
help="Only instances for project (tag Project:<name>)")
def list_instances(project):
"List EC2 instances"
instances = []
if project: # default값으로 들어오는 경우 false임
print("tag project : " + project)
filters = [{'Name':'tag:Project', 'Values':[project]}]
instances = ec2.instances.filter(Filters=filters)
else:
instances = ec2.instances.all()
for i in instances:
print(i)
if __name__ == '__main__':
list_instances()
PS C:\Users\Administrator\code\aws-snapshot> pipenv run python .\shotty\shotty.py --project=python-test
tag project : python-test
ec2.Instance(id='i-123')
tag 정보 같이 출력하기
import boto3
import click
session = boto3.Session(profile_name='shotty')
ec2 = session.resource('ec2')
@click.command()
@click.option('--project', default=None,
help="Only instances for project (tag Project:<name>)")
def list_instances(project):
"List EC2 instances"
instances = []
if project: # default값으로 들어오는 경우 false임
print("tag project : " + project)
filters = [{'Name':'tag:Project', 'Values':[project]}]
instances = ec2.instances.filter(Filters=filters)
else:
instances = ec2.instances.all()
for i in instances:
tags = { t['Key']:t['Value'] for t in i.tags or [] }
print(', '.join((
i.instance_type,
tags.get('Project', '<no project>')
)))
if __name__ == '__main__':
list_instances()
PS C:\Users\Administrator\code\aws-snapshot> pipenv run python .\shotty\shotty.py
t2.micro, <no project>
t2.micro, python-test
@click.group을 이용한 list, start 기능 구현
import boto3
import click
session = boto3.Session(profile_name='shotty')
ec2 = session.resource('ec2')
def filter_instances(project):
instances = []
if project: # default값으로 들어오는 경우 false임
print("tag project : " + project)
filters = [{'Name':'tag:Project', 'Values':[project]}]
instances = ec2.instances.filter(Filters=filters)
else:
instances = ec2.instances.all()
return instances
@click.group()
def instances():
"""Command for instances"""
@instances.command('list')
@click.option('--project', default=None,
help="Only instances for project (tag Project:<name>)")
def list_instances(project):
"List EC2 instances"
instances = filter_instances(project)
for i in instances:
tags = { t['Key']:t['Value'] for t in i.tags or [] }
print(', '.join((
i.instance_type,
tags.get('Project', '<no project>'),
i.state['Name']
)))
return
@instances.command('stop')
@click.option('--project', default=None,
help='Only instances for project')
def stop_instances(project):
"Stop EC2 instances"
instances = filter_instances(project)
for i in instances:
print("Stopping {0}...".format(i.id))
i.stop()
return
if __name__ == '__main__':
instances()
PS C:\Users\Administrator\code\aws-snapshot> pipenv run python .\shotty\shotty.py list --project=python-test
tag project : python-test
t2.micro, python-test, running
PS C:\Users\Administrator\code\aws-snapshot> pipenv run python .\shotty\shotty.py stop --project=python-test
tag project : python-test
Stopping i-123...
댓글