티스토리 뷰

728x90
반응형

 포스팅은 AWS CLI 실습 세션입니다.


AWS CLI는 커맨드 라인에서 아마존 웹 서비스의 API를 직접 호출할 수 있게 도와주는 명령어입니다.

AWS CLI는 Python으로 만들어진 커맨드 라인 명령어입니다.

테스트 환경은 Ubunt에서 진행합니다. 다만 우분투 이미지 생성과정은 생략하겠습니다. aws 콘솔의 ec2 이미지를 손쉽게 생성할 수 있습니다. 마찬가지로 접속 방법은 ec2 dns 정보를 기반으로 키파일 인증을 통해 접속합니다. CentOS의 경우 ec2-user, Ubuntu의 경우 ubuntu 계정을 기본으로 사용합니다.



AWS CLI 설치

우분투에서 AWS CLI를 사용하기 위해서는 먼저 Python을 설치해야 합니다.

- apt-get을 이용한 python 설치


ubuntu@ip-172-31-21-21:~$ sudo apt-get install python3

Reading package lists... Done

Building dependency tree       

Reading state information... Done

python3 is already the newest version (3.5.1-3).

0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

ubuntu@ip-172-31-21-21:~$


- python3를 이용한 python 패키지 관리자 pip 설치


ubuntu@ip-172-31-21-21:~$ sudo python3 get-pip.py 

The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

...

Collecting pip

  Downloading 

...

...

...

Installing collected packages: pip, setuptools, wheel

Successfully installed pip-19.0.1 setuptools-40.8.0 wheel-0.32.3

ubuntu@ip-172-31-21-21:~$




awscli 패키지 설치 및 확인

pip를 이용하여 awscli를 설치합니다.

- awscli 패키지 설치


ubuntu@ip-172-31-21-21:~$ sudo pip install awscli --upgrade

The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

Collecting awscli

  Downloading 

...

...

...

Requirement already satisfied, skipping upgrade: pyasn1>=0.1.3 in /usr/lib/python3/dist-packages (from rsa<=3.5.0,>=3.1.2->awscli) (0.1.9)

Requirement already satisfied, skipping upgrade: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil<3.0.0,>=2.1; python_version >= "2.7"->botocore==1.12.88->awscli) (1.10.0)

Installing collected packages: python-dateutil, urllib3, jmespath, docutils, botocore, s3transfer, rsa, colorama, awscli

  Found existing installation: urllib3 1.13.1

    Uninstalling urllib3-1.13.1:

      Successfully uninstalled urllib3-1.13.1

Successfully installed awscli-1.16.98 botocore-1.12.88 colorama-0.3.9 docutils-0.14 jmespath-0.9.3 python-dateutil-2.8.0 rsa-3.4.2 s3transfer-0.1.13 urllib3-1.24.1

ubuntu@ip-172-31-21-21:~$ which aws

/usr/local/bin/aws

ubuntu@ip-172-31-21-21:~$ aws --version

aws-cli/1.16.98 Python/3.5.2 Linux/4.4.0-1067-aws botocore/1.12.88

ubuntu@ip-172-31-21-21:~$




인증 설정

먼저 awscli를 사용하기 위해선 AWS 계정이 필요합니다. AWS 루트 계정이나 루트 계정으로 생성된 사용자 계정의 액세스 키와 시크릿 액세스 키를 등록해야합니다.


 주요 인증 정보

AWS 액세스 키 ID(AWS Access Key ID)액세스 키의 ID 값을 지정합니다.

AWS 시크릿 액세스 키(AWS Secret Access Key) : 액세스 키의 ID 값애 대응하는 시크릿 키를 지정합니다. 시크릿 키는 오직 발급하는 시점에만 확인할 수 있습니다.

기본 리전(Default region name) : aws 명령어를 사용할 때 API를 호출할 기본 리전을 지정합니다. ap-northeast-2는 서울 리전입니다. 이 값은 명령어를 실행할 때 --region <REGION> 옵션으로 덮어쓸 수 있습니다.

기본 출력 포맷(Default output format) : API 호출한 결과를 출력할 포맷을 지정합니다. text, json, table 중에 하나를 사용할 수 있습니다. 이 값은 명령어를 실행할 때 --output <FORMAT> 옵션으로 덮어쓸 수 있습니다.


 configure 명령어

로컬에 액세스 키를 등록하는 가장 쉬운 방법은 configure 명령어를 사용하는 방법입니다.  aws configure를 실행하면 하단의 4개의 값을 즉석에서 지정할 수 있습니다.


ubuntu@ip-172-31-21-21:~$ aws configure

AWS Access Key ID [None]: AKIAJ754C*********

AWS Secret Access Key [None]: *******************************************

Default region name [None]: ap-northeast-2

Default output format [None]: text

ubuntu@ip-172-31-21-21:~$ ls -la

total 1696

drwxr-xr-x 5 ubuntu ubuntu    4096 Feb  6 05:31 .

drwxr-xr-x 3 root   root      4096 Feb  6 04:59 ..

drwxrwxr-x 2 ubuntu ubuntu    4096 Feb  6 05:31 .aws

-rw-r--r-- 1 ubuntu ubuntu     220 Aug 31  2015 .bash_logout

-rw-r--r-- 1 ubuntu ubuntu    3771 Aug 31  2015 .bashrc

drwx------ 3 ubuntu ubuntu    4096 Feb  6 05:13 .cache

-rw-r--r-- 1 ubuntu ubuntu     655 May 16  2017 .profile

drwx------ 2 ubuntu ubuntu    4096 Feb  6 04:59 .ssh

-rw-r--r-- 1 ubuntu ubuntu       0 Feb  6 05:07 .sudo_as_admin_successful

-rw-rw-r-- 1 ubuntu ubuntu 1700321 Feb  6 05:12 get-pip.py

ubuntu@ip-172-31-21-21:~$ cd .aws

ubuntu@ip-172-31-21-21:~/.aws$ ls -la

total 16

drwxrwxr-x 2 ubuntu ubuntu 4096 Feb  6 05:31 .

drwxr-xr-x 5 ubuntu ubuntu 4096 Feb  6 05:31 ..

-rw------- 1 ubuntu ubuntu   48 Feb  6 05:31 config

-rw------- 1 ubuntu ubuntu  116 Feb  6 05:31 credentials

ubuntu@ip-172-31-21-21:~/.aws$ cat config 

[default]

output = text

region = ap-northeast-2

ubuntu@ip-172-31-21-21:~/.aws$ cat credentials 

[default]

aws_access_key_id = AKIAJ754C*********

aws_secret_access_key = *******************************************

ubuntu@ip-172-31-21-21:~/.aws$


인증정보는 configure 수행 시점에 위과 같이 저장됩니다. 위 파일을 저장하여 다른 서버로 복사하여 사용할 수 있습니다.


 profile & export 명령어

profile이나 export 명령어로 실행중인 셸에 환경변수를 등록할 수 있습니다.


ubuntu@ip-172-31-21-21:~/.aws$ vi .profile

...

export AWS_ACCESS_KEY_ID=AKIAJ754C*********

export AWS_SECRET_ACCESS_KEY=*******************************************

export AWS_DEFAULT_REGION=ap-northeast-2

export AWS_DEFAULT_OUTPUT=text

...

ubuntu@ip-172-31-21-21:~/.aws$




사용법

awscli를 사용하면 거의 모든 AWS 상의 API를 사용하는 것이 가능합니다.


 awscli 사용법

예를들어 ECS 명령어를 호출하는 기본적인 구조는 다음과 같습니다.


$ aws ecs <sub>

프로필을 변경하는 경우 aws 명령어 바로 다음에 --profile옵션을 지정해줍니다.

$ aws --profile=<profile> ecs <sub>

리전이나 출력 형식을 변경하는 경우에도 마찬가지 입니다.

$ aws --region=<region> --output=<form> ecs <sub>



 ECS 클러스터 생성

ECS 클러스터를 생성은 create-cluster서브 명령어를 사용합니다. create-cluster를 호출할 때 필수적으로 지정해야 옵션은 클러스터 이름을 지정하는 --cluster-name입니다. 서브 명령어의 옵션은 전체 명령어 맨 뒤에 붙여줍니다. 여기서는 클러스터의 이름을 aws-cluster_by_nrson으로 지정해 주었습니다.


ubuntu@ip-172-31-21-21:~$ aws ecs create-cluster --cluster-name aws-cluster-by-nrson

CLUSTER 0       arn:aws:ecs:ap-northeast-2:350781310226:cluster/aws-cluster-by-nrson    aws-cluster-by-nrson    0  ACTIVE

ubuntu@ip-172-31-21-21:~$ 


위와같이 정상적으로 생성된 Cluster는 아래와 같이 ECS 웹 콘솔에서 클러스터가 생성된 것을 확인할 수 있습니다.



마찬가지로 awscli 확인은 다음과 같습니다.


ubuntu@ip-172-31-21-21:~$ aws ecs list-clusters

CLUSTERARNS     arn:aws:ecs:ap-northeast-2:350781310226:cluster/aws-cluster-by-nrson

ubuntu@ip-172-31-21-21:~$ aws ecs list-clusters --output json

{

    "clusterArns": [

        "arn:aws:ecs:ap-northeast-2:350781310226:cluster/aws-cluster-by-nrson"

    ]

}

ubuntu@ip-172-31-21-21:~$ aws ecs list-clusters --output table

----------------------------------------------------------------------------

|                               ListClusters                               |

+--------------------------------------------------------------------------+

||                               clusterArns                              ||

|+------------------------------------------------------------------------+|

||  arn:aws:ecs:ap-northeast-2:350781310226:cluster/aws-cluster-by-nrson  ||

|+------------------------------------------------------------------------+|

ubuntu@ip-172-31-21-21:~$


configure 시점의 설정값인 text 이외에 직접 --output을 지정하여 json, table 형식으로도 확인할 수 있습니다.


#참조

AWS CLI Command Reference : AWS CLI 공식 홈페이지

AWS Command Line Interface : AWS CLI 사용 가이드

AWS Labs : AWS GitHub

728x90
반응형