1. AWS API 라이브러리 boto download 방법
준비 : python2 or 3 설정 완료
1.1. (pip 설치) 사이트에서 get-pip.py 다운
site : https://bootstrap.pypa.io/
1.2. cmd 창에서 명령어 실행
[명령어]
python get-pip.py
설치 완료되면
%python_home%\Scipts 폴더에 pip 확인 가능
1.3. cmd 창에서 명령어 실행
[명령어]
pip install boto3
설치 완료되면
%python_home%\Lib\site-packages 폴더에 boto 폴더 확인 가능
2. AWS API 사용 전 인증받아야 함
Before you can begin using Boto3, you should set up authentication credentials.
Credentials for your AWS account can be found in the IAM Console.
You can create or use an existing user.
Go to manage access keys and generate a new set of keys.
2.1. AWS IAM 접속
2.2. 왼쪽 대시보드에서 액세스 관리 > 사용자 클릭
2.3. 사용자 추가
2.4. 사용자 이름 입력
2.5. AWS 액세스 유형 선택 : 프로그래밍 방식 액세스 선택
2.6. 그룹지정
기존에 그룹이 있는 경우는 아래 예제의 admin과 같이 본인이 생성했던 그룹이 있을 것
없다면 그룹 생성을 클릭
참고) 그룹 생성 방법
예제는 administratorAccess 권한을 가진 그룹을 생성함
2.7. 그룹 지정하고 다음 : 태그 클릭
태그는 사용자에 추가해줄 수 있는 key-value 페어인데
굳이 입력하지 않아도 상관없음
2.8. 다음 : 검토 클릭
2.9. 사용자 만들기
2.10. 완료하면 액세스 키 ID와 비밀 엑세스 키를 확인할 수 있음
(주의)
비밀 엑세스 key의 경우 한 번만 확인할 수 있음
해당 키를 사용하려면 반드시 기록해놔야 함
2.11. 계정 생성 완료 후 key 확인을 과정
AWS IAM> 대시보드 > 액세스 관리 > 사용자 > 상위에서 생성한 사용자 클릭 > 보안 자격 증명 tab 클릭
이동하면 액세스 키 ID만 확인 가능
비밀 엑세스 키는 확인 불가능
상위의 엑세스 키 만들기 버튼을 눌러서 새로 만들어야 함
비밀 액세스 키는 무조건 만들었을 때 1번만 확인 가능
AWS Access Key ID = 액세스 키 ID
AWS Secret Access Key = 비밀 액세스 키 정보를 입력
파이썬 예제 코드
import boto3
access_key = "key(찾는 방식은 상위 설명 참고)" secret_key = "key(찾는 방식은 상위 설명 참고)" region="ap-northeast-2"
def AWS_VM(num):
ec2 = boto3.resource('ec2', aws_access_key_id=access_key, aws_secret_access_key=secret_key,region_name=region)
## AWS EC2 Instances List ## if num == 0: print('[AWS EC2 Instances List]') VmList = ec2.instances.all() count_vm = 1 for instance in VmList: print("[{0}] id=({1}), type=({2}), state=({3})".format(str(count_vm), instance.id, instance.instance_type, instance.state['Name'])) count_vm += 1 #end for #end if
## ALL Start ## elif num == 1: print('[AWS EC2 Start]') VmList = ec2.instances.all() count_vm = 1 startIds = [] for instance in VmList: if 'stopped' == instance.state['Name']: startIds.append(instance.id) #end if #end for
if len(startIds) > 0: startList = ec2.instances.filter(InstanceIds=startIds).start() for startInstance in startList: print('ID=({0}) is START'.format(startInstance['StartingInstances'][0]['InstanceId'])) #print(startInstance) #end for else: print("No instances to start.") #end elif
## ALL Stop ## elif num == 2: print('[AWS EC2 Stop]') VmList = ec2.instances.all() stopIds = [] for instance in VmList: if 'running' == instance.state['Name']: stopIds.append(instance.id) # if #for
if len(stopIds) > 0: stopList = ec2.instances.filter(InstanceIds=stopIds).stop() for stopInstance in stopList: print('ID=({0}) is STOP'.format(stopInstance['StoppingInstances'][0]['InstanceId'])) #print(stopInstance) else: print("No instances to stop.") #end elif #end def AWS_VM
def Azure_VM(num):
if __name__=="__main__" :
while True: InputGet = input('cli> ') if 'q' == InputGet : break if '0' == InputGet : AWS_VM(0) if '1' == InputGet : AWS_VM(1) if '2' == InputGet : AWS_VM(2) |
제 글을 복사할 시 출처를 명시해주세요.
글에 오타, 오류가 있다면 댓글로 알려주세요! 바로 수정하겠습니다!
참고
AWS boto 공식 설명
https://boto3.amazonaws.com/v1/documentation/api/latest/index.html
EC2 API
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html
AWS Access key, secret key
https://wildpup.cafe24.com/archives/929
boto3 예제
https://computer-choco.tistory.com/35
stop, start 참고
https://aws.amazon.com/ko/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/
https://gist.github.com/gregarious-repo/b75eb8cb34e9b3644542c81fa7c7c23b
'클라우드 > AWS' 카테고리의 다른 글
EKS Network Policy 분석 (0) | 2021.05.06 |
---|---|
EKS AWS CNI - Pod 간 통신 (0) | 2021.05.06 |
EKS AWS CNI 분석 (2) | 2021.05.03 |
AWS Kubernetes (EKS) (0) | 2021.02.23 |
AWS 1년 무료 서버 생성법 (0) | 2020.08.12 |