본문 바로가기
카테고리 없음

python _aws instance 제어하기 (a cloud guru)

by 책읽는구리 2020. 2. 12.
반응형

 

https://github.com/seulseul627/aws-snapshot.git

 

seulseul627/aws-snapshot

Demo project to manage AWS instance snapshots. Contribute to seulseul627/aws-snapshot development by creating an account on GitHub.

github.com

 

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/

 

Python용 AWS SDK (Boto3) 정식 출시 | Amazon Web Services

Boto로 알려진 Python용 AWS SDK의 신규 버전인 Boto3 정식 발표에 대한 Peter Moon의 글을 소개합니다. — Jeff; 2006년에 처음 Mitch Garnaat가 Amazon S3용 파이선 클라이언트를 시작하였고, Boto를 통해 많은 파이선 개발자들이 아마존 웹 서비스를 활용해 왔습니다. AWS 뿐만 아니라 파이선 커뮤니티에서 끊임없는 지원을 한 결과 커다란 진화를 이루어 왔습니다. 거의 40여개 AWS 서비스를 지원하고 있고,

aws.amazon.com

 

> 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()

print(sys.argv)를 이용해 argv 확인이 가능

 

argv에 여러개의 argument가 넘어온 것을 확인

 

 

python 패키지 - $click

https://click.palletsprojects.com/en/7.x/

 

Welcome to Click — Click Documentation (7.x)

 

click.palletsprojects.com

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

 

EC2 — Boto 3 Docs 1.11.16 documentation

PrivateIpAddresses (list) -- [REQUIRED] The secondary private IP addresses to unassign from the network interface. You can specify this option multiple times to unassign more than one IP address.

boto3.amazonaws.com

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


 

반응형

댓글