この記事には広告を含む場合があります。
記事内で紹介する商品を購入することで、当サイトに売り上げの一部が還元されることがあります。
ども。
朝晩は冷え込むものの、日中は暖かくて春の足音が近づいて来ているように思います。
春と言えば、入学式や卒業式があったりと出会いと別れの季節だったり、お花見をして皆でホッコリしたりと色々な行事があって楽しい季節ですが、皆さんはいかがお過ごしでしょうか。
さて、今回はAWSをCLIで操作するのに便利なawscli導入について書きたいと思います。
AWSコンソールはとても優秀で、インスタンスを1台立ち上げたり、セキュリティグループをチョコっと修正したりといった操作には向いているのですが、大量のインスタンスを立ち上げたり、大量のセキュリティグループを修正したりといった場合にはCLIからの操作が良いと思います。また、現状ではCLIでしか出来ない操作もいくつかあったりしますので、そういう面から考えてもawscliは導入しておいたほうが良いと思います。
では、早速導入をして行きましょう!
導入
今回の導入対象はCentOS7のEC2インスタンスに導入する事とします。
・epel-releaseインストール
# yum install -y epel-release
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
updates/7/x86_64/primary_db | 6.6 MB 00:00:00
Loading mirror speeds from cached hostfile
* base: mirror.vtti.vt.edu
* extras: mirror.symnds.com
* updates: mirror.cogentco.com
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-5 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================
Package Arch Version Repository Size
==================================================================================================================
Installing:
epel-release noarch 7-5 extras 14 k
Transaction Summary
==================================================================================================================
Install 1 Package
Total download size: 14 k
Installed size: 24 k
Downloading packages:
epel-release-7-5.noarch.rpm | 14 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : epel-release-7-5.noarch 1/1
Verifying : epel-release-7-5.noarch 1/1
Installed:
epel-release.noarch 0:7-5
Complete!
・python-setuptoolsのインストール
# yum install -y python-setuptools
Loaded plugins: fastestmirror
epel/x86_64/metalink | 14 kB 00:00:00
epel | 4.4 kB 00:00:00
epel/x86_64/primary_db | 4.0 MB 00:00:00
(1/2): epel/x86_64/updateinfo | 317 kB 00:00:00
(2/2): epel/x86_64/pkgtags | 1.4 MB 00:00:00
Loading mirror speeds from cached hostfile
* base: mirror.vtti.vt.edu
* epel: mirror.symnds.com
* extras: mirror.symnds.com
* updates: mirror.cogentco.com
Resolving Dependencies
--> Running transaction check
---> Package python-setuptools.noarch 0:0.9.8-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================
Package Arch Version Repository Size
==================================================================================================================
Installing:
python-setuptools noarch 0.9.8-3.el7 base 396 k
Transaction Summary
==================================================================================================================
Install 1 Package
Total download size: 396 k
Installed size: 1.9 M
Downloading packages:
python-setuptools-0.9.8-3.el7.noarch.rpm | 396 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : python-setuptools-0.9.8-3.el7.noarch 1/1
Verifying : python-setuptools-0.9.8-3.el7.noarch 1/1
Installed:
python-setuptools.noarch 0:0.9.8-3.el7
Complete!
・pipのインストール
# easy_install pip
Searching for pip
Best match: pip 6.0.8
Processing pip-6.0.8-py2.7.egg
pip 6.0.8 is already the active version in easy-install.pth
Installing pip script to /usr/bin
Installing pip2.7 script to /usr/bin
Installing pip2 script to /usr/bin
Using /usr/lib/python2.7/site-packages/pip-6.0.8-py2.7.egg
Processing dependencies for pip
Finished processing dependencies for pip
・awscliのインストール
# pip install awscli
Collecting awscli
Downloading awscli-1.7.15.tar.gz (301kB)
100% |################################| 303kB 1.3MB/s
Collecting botocore<0.97.0,>=0.96.0 (from awscli)
Downloading botocore-0.96.0.tar.gz (1.4MB)
100% |################################| 1.4MB 329kB/s
Requirement already satisfied (use --upgrade to upgrade): bcdoc<0.14.0,>=0.13.0 in /usr/lib/python2.7/site-packages (from awscli)
Requirement already satisfied (use --upgrade to upgrade): colorama<=0.3.3,>=0.2.5 in /usr/lib/python2.7/site-packages (from awscli)
Requirement already satisfied (use --upgrade to upgrade): docutils>=0.10 in /usr/lib/python2.7/site-packages (from awscli)
Requirement already satisfied (use --upgrade to upgrade): rsa<=3.1.4,>=3.1.2 in /usr/lib/python2.7/site-packages (from awscli)
Requirement already satisfied (use --upgrade to upgrade): jmespath==0.6.1 in /usr/lib/python2.7/site-packages (from botocore<0.97.0,>=0.96.0->awscli)
Requirement already satisfied (use --upgrade to upgrade): python-dateutil<3.0.0,>=2.1 in /usr/lib/python2.7/site-packages (from botocore<0.97.0,>=0.96.0->awscli)
Requirement already satisfied (use --upgrade to upgrade): six<2.0.0,>=1.8.0 in /usr/lib/python2.7/site-packages (from bcdoc<0.14.0,>=0.13.0->awscli)
Requirement already satisfied (use --upgrade to upgrade): pyasn1>=0.1.3 in /usr/lib/python2.7/site-packages (from rsa<=3.1.4,>=3.1.2->awscli)
Installing collected packages: botocore, awscli
Found existing installation: botocore 0.95.0
Uninstalling botocore-0.95.0:
Successfully uninstalled botocore-0.95.0
Running setup.py install for botocore
Running setup.py install for awscli
changing mode of build/scripts-2.7/aws from 644 to 755
changing mode of build/scripts-2.7/aws.cmd from 644 to 755
changing mode of build/scripts-2.7/aws_completer from 644 to 755
changing mode of build/scripts-2.7/aws_zsh_completer.sh from 644 to 755
changing mode of /usr/bin/aws to 755
changing mode of /usr/bin/aws.cmd to 755
changing mode of /usr/bin/aws_completer to 755
changing mode of /usr/bin/aws_zsh_completer.sh to 755
Successfully installed awscli-1.7.15 botocore-0.96.0
確認①
# aws --version
aws-cli/1.7.15 Python/2.7.5 Linux/3.10.0-123.8.1.el7.x86_64
これでawscliのインストールは終わりですが、この状態だとまだ使えません。
AWSにおいてCLIからAPIを実行するにはAPIキーと呼ばれるものを設定する必要があります。
IAMユーザを作成してAPIキーを取得するのですが、ここでは省略します。
APIキーの設定
#aws configure
AWS Access Key ID [None]: <アクセスキー>
AWS Secret Access Key [None]: <シークレットキー>
Default region name [None]: ap-northeast-1
Default output format [None]:
確認②
# aws ec2 describe-regions
{
"Regions": [
{
"Endpoint": "ec2.eu-central-1.amazonaws.com",
"RegionName": "eu-central-1"
},
{
"Endpoint": "ec2.sa-east-1.amazonaws.com",
"RegionName": "sa-east-1"
},
{
"Endpoint": "ec2.ap-northeast-1.amazonaws.com",
"RegionName": "ap-northeast-1"
},
{
"Endpoint": "ec2.eu-west-1.amazonaws.com",
"RegionName": "eu-west-1"
},
{
"Endpoint": "ec2.us-east-1.amazonaws.com",
"RegionName": "us-east-1"
},
{
"Endpoint": "ec2.us-west-1.amazonaws.com",
"RegionName": "us-west-1"
},
{
"Endpoint": "ec2.us-west-2.amazonaws.com",
"RegionName": "us-west-2"
},
{
"Endpoint": "ec2.ap-southeast-2.amazonaws.com",
"RegionName": "ap-southeast-2"
},
{
"Endpoint": "ec2.ap-southeast-1.amazonaws.com",
"RegionName": "ap-southeast-1"
}
]
}
以上でawscliを使える状態になります。
設定したAPIキーによってawscliを使って出来ることが制御出来たりするのですが、またの機会に説明したいと思います。
それではまた!