【AWS】Windows から WSL(CentOS 7)の bash を使って AWS CLI で EC2 インスタンスを作成及び設定し情報を取得する手順

Windows 10 がインストールされているローカル PC(自宅 PC)より WSL 2(Windows Subsystem for Linux 2)の CentOS を利用して Amazon EC2 インスタンスを作成及び設定し、情報を取得する手順について解説します。

 

ややこしいですが、Windows 標準の PowerShell ではなく、Windows に WSL 2(Windows Subsystem for Linux 2)をインストールして、Windows 上で CentOS を起動して bash で AWS を操作するという構図です。

 

Windows でも AWS CLI をインストールできるので PowerShell からやろうと思えばできるのですが、あえて Windows 上に CentOS をインストールして bash で操作しようとする理由は「学習コストの削減」です。

 

学習する時間が無限にあれば、bash も PowerShell も完璧に学習できますが、時間は有限のため PowerShell ではなく bash での操作にこだわっています。(しかし PowerShell の知識も必要なので、それはそれで学習する必要はありますが)

 

以下、Windows 10 に Windows Subsystem for Linux 2(WSL 2)をインストールして Linux(CentOS)が使えるようにする手順について解説しています。

 

【Windows】Windows 10 に Windows Subsystem for Linux 2(WSL 2)をインストールして Linux(CentOS)が使えるようにする手順

 

 

Linux に AWS CLI をインストールする手順については以下の記事を参考にしてください。

【AWS】【Windows&Linux】「EC2 インスタンス」または「パソコン」に AWS CLI(aws コマンド)及び jq コマンドをインストールする手順

 

 

 

 

事前準備(WSL に pip をインストール)

事前準備として WSL 2 の CentOS 上に pip をインストールします。

 

【RedHat 8】Python3.6 および pip3.6 のインストール手順【AWS EC2 インスタンス】

 

 

すでに easy_install はインストールされているので以下のコマンド一発で pip をインストールできます。

※warning が出力されているので最初は WSL に pip をインストールできないかと思いましたが、問題なく pip コマンドは機能しています。

[root@DELL-NOTE ~]# easy_install pip 
Searching for pip 
Reading https://pypi.python.org/simple/pip/ 
Best match: pip 19.3.1 
Downloading https://files.pythonhosted.org/packages/ce/ea/9b445176a65ae4ba22dce1d93e4b5fe182f953df71a145f557cffaffc1bf/pip-19.3.1.tar.gz#sha256=21207d76c1031e517668898a6b46a9fb1501c7a4710ef5dfd6a40ad9e6757ea7 
Processing pip-19.3.1.tar.gz 
Writing /tmp/easy_install-nm60ge/pip-19.3.1/setup.cfg 
Running pip-19.3.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-nm60ge/pip-19.3.1/egg-dist-tmp-inLgT2 
/usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires' 
  warnings.warn(msg) 
warning: no files found matching 'docs/docutils.conf' 
warning: no previously-included files found matching '.coveragerc' 
warning: no previously-included files found matching '.mailmap' 
warning: no previously-included files found matching '.appveyor.yml' 
warning: no previously-included files found matching '.travis.yml' 
warning: no previously-included files found matching '.readthedocs.yml' 
warning: no previously-included files found matching '.pre-commit-config.yaml' 
warning: no previously-included files found matching 'tox.ini' 
warning: no previously-included files found matching 'noxfile.py' 
warning: no files found matching 'Makefile' under directory 'docs' 
warning: no files found matching '*.bat' under directory 'docs' 
warning: no previously-included files found matching 'src/pip/_vendor/six' 
warning: no previously-included files found matching 'src/pip/_vendor/six/moves' 
warning: no previously-included files matching '*.pyi' found under directory 'src/pip/_vendor' 
no previously-included directories found matching '.github' 
no previously-included directories found matching '.azure-pipelines' 
no previously-included directories found matching 'docs/build' 
no previously-included directories found matching 'news' 
no previously-included directories found matching 'tasks' 
no previously-included directories found matching 'tests' 
no previously-included directories found matching 'tools' 
Adding pip 19.3.1 to easy-install.pth file 
Installing pip script to /usr/bin 
Installing pip2.7 script to /usr/bin 
Installing pip2 script to /usr/bin 
 
Installed /usr/lib/python2.7/site-packages/pip-19.3.1-py2.7.egg 
Processing dependencies for pip 
Finished processing dependencies for pip 
[root@DELL-NOTE ~]# 

 

 

pip のバージョンを確認します。

[root@DELL-NOTE ~]# pip --version
pip 19.3.1 from /usr/lib/python2.7/site-packages/pip-19.3.1-py2.7.egg/pip (python 2.7)
[root@DELL-NOTE ~]#

 

 

 

pip コマンドで AWS CLI をインストールします。

AWS CLI も問題なくインストールできました。

[root@DELL-NOTE ~]# pip install awscli 
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support 
Collecting awscli 
  Downloading https://files.pythonhosted.org/packages/6d/d4/f16ce2e6f21eba7a0f2fabb221aa7f9e6b41735984111a4c6bdf129ace24/awscli-1.16.310-py2.py3-none-any.whl (2.7MB) 
     |████████████████████████████████| 2.7MB 2.8MB/s 
Collecting rsa<=3.5.0,>=3.1.2 
  Downloading https://files.pythonhosted.org/packages/e1/ae/baedc9cb175552e95f3395c43055a6a5e125ae4d48a1d7a924baca83e92e/rsa-3.4.2-py2.py3-none-any.whl (46kB) 
     |████████████████████████████████| 51kB 2.9MB/s 
Collecting docutils<0.16,>=0.10 
  Downloading https://files.pythonhosted.org/packages/3a/dc/bf2b15d1fa15a6f7a9e77a61b74ecbbae7258558fcda8ffc9a6638a6b327/docutils-0.15.2-py2-none-any.whl (548kB) 
     |████████████████████████████████| 552kB 3.4MB/s 
Requirement already satisfied: PyYAML<5.3,>=3.10; python_version != "2.6" and python_version != "3.3" in /usr/lib64/python2.7/site-packages (from awscli) (3.10) 
Collecting botocore==1.13.46 
  Downloading https://files.pythonhosted.org/packages/74/17/40f9fb50219e8a24fd9c09c909d26f074d4a2fc83b66065dff3770ecbebb/botocore-1.13.46-py2.py3-none-any.whl (5.9MB) 
     |████████████████████████████████| 5.9MB 2.4MB/s 
Collecting colorama<0.4.2,>=0.2.5; python_version != "2.6" and python_version != "3.3" 
  Downloading https://files.pythonhosted.org/packages/4f/a6/728666f39bfff1719fc94c481890b2106837da9318031f71a8424b662e12/colorama-0.4.1-py2.py3-none-any.whl 
Collecting s3transfer<0.3.0,>=0.2.0 
  Downloading https://files.pythonhosted.org/packages/16/8a/1fc3dba0c4923c2a76e1ff0d52b305c44606da63f718d14d3231e21c51b0/s3transfer-0.2.1-py2.py3-none-any.whl (70kB) 
     |████████████████████████████████| 71kB 3.9MB/s 
Collecting pyasn1>=0.1.3 
  Downloading https://files.pythonhosted.org/packages/62/1e/a94a8d635fa3ce4cfc7f506003548d0a2447ae76fd5ca53932970fe3053f/pyasn1-0.4.8-py2.py3-none-any.whl (77kB) 
     |████████████████████████████████| 81kB 2.8MB/s 
Collecting jmespath<1.0.0,>=0.7.1 
  Downloading https://files.pythonhosted.org/packages/83/94/7179c3832a6d45b266ddb2aac329e101367fbdb11f425f13771d27f225bb/jmespath-0.9.4-py2.py3-none-any.whl 
Collecting python-dateutil<3.0.0,>=2.1; python_version >= "2.7" 
  Downloading https://files.pythonhosted.org/packages/d4/70/d60450c3dd48ef87586924207ae8907090de0b306af2bce5d134d78615cb/python_dateutil-2.8.1-py2.py3-none-any.whl (227kB) 
     |████████████████████████████████| 235kB 5.1MB/s 
Collecting urllib3<1.26,>=1.20; python_version == "2.7" 
  Downloading https://files.pythonhosted.org/packages/b4/40/a9837291310ee1ccc242ceb6ebfd9eb21539649f193a7c8c86ba15b98539/urllib3-1.25.7-py2.py3-none-any.whl (125kB) 
     |████████████████████████████████| 133kB 2.9MB/s 
Collecting futures<4.0.0,>=2.2.0; python_version == "2.6" or python_version == "2.7" 
  Downloading https://files.pythonhosted.org/packages/d8/a6/f46ae3f1da0cd4361c344888f59ec2f5785e69c872e175a748ef6071cdb5/futures-3.3.0-py2-none-any.whl 
Requirement already satisfied: six>=1.5 in /usr/lib/python2.7/site-packages (from python-dateutil<3.0.0,>=2.1; python_version >= "2.7"->botocore==1.13.46->awscli) (1.9.0) 
Installing collected packages: pyasn1, rsa, docutils, jmespath, python-dateutil, urllib3, botocore, colorama, futures, s3transfer, awscli 
  Found existing installation: urllib3 1.10.2 
    Uninstalling urllib3-1.10.2: 
      Successfully uninstalled urllib3-1.10.2 
Successfully installed awscli-1.16.310 botocore-1.13.46 colorama-0.4.1 docutils-0.15.2 futures-3.3.0 jmespath-0.9.4 pyasn1-0.4.8 python-dateutil-2.8.1 rsa-3.4.2 s3transfer-0.2.1 urllib3-1.25.7 
[root@DELL-NOTE ~]# 

 

 

AWS CLI のバージョンを確認します。

[root@DELL-NOTE ~]# aws --version
aws-cli/1.16.310 Python/2.7.5 Linux/4.4.0-18362-Microsoft botocore/1.13.46
[root@DELL-NOTE ~]#

 

 

最新の CLI のバージョン番号は、

https://github.com/aws/aws-cli/blob/master/CHANGELOG.rst

を参照してください。

 

2020年1月現在は「1.16.310」です。

 

 

 

 

 

jq コマンドのインストール

jq コマンドもあると便利なのでインストールします。

yum コマンドでインストール可能です。

[root@DESKTOP-KSVJFCR ~]# yum install jq

 

 

 

 

効率のよい EC2 インスタンス管理をローカル PC よりコマンドラインで行う

せっかく AWS というクラウドサービスを利用しているので、EC2 インスタンスを使用したい時に EC2 インスタンスを新規作成し、使用し終わったら削除するという使い方もできますし、コスト削減にもなると思います。

 

 

■AWS CLI の認証情報の確認

値が「None」なのでまだ何も設定されていないことが分かります。

[root@DESKTOP-KSVJFCR ~]# aws configure list 
      Name                    Value             Type    Location 
      ----                    -----             ----    -------- 
   profile                <not set>             None    None 
access_key                <not set>             None    None 
secret_key                <not set>             None    None 
    region                <not set>             None    None 
[root@DESKTOP-KSVJFCR ~]#

 

 

 

■AWS CLI の認証情報の新規作成

aws configure コマンドでアクセスキーとシークレットアクセスキーを設定します。

[root@DESKTOP-KSVJFCR ~]# aws configure 
AWS Access Key ID [None]: AXXXXXXXXXXXXXXXXXXYJL 
AWS Secret Access Key [None]: DXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX6z 
Default region name [None]: ap-northeast-1 
Default output format [None]: json 
[root@DESKTOP-KSVJFCR ~]#

 

 

 

 

 

セキュリティグループを作成する

EC2 インスタンスを作成する際に「セキュリティグループ」を指定する必要があります。

そのため、最初にセキュリティグループを作成します。

 

 

■セキュリティグループの作成

[root@DESKTOP-KSVJFCR ~]# aws ec2 create-security-group --group-name Test-EC2-Instance-001-sg --vpc-id vpc-xxxxxxxxxxx6bb --description "Test-EC2-Instance-001-sg" 

    "GroupId": "sg-0a805239854d7d1da" 

[root@DESKTOP-KSVJFCR ~]#

 

 

 

上記コマンドだけでは Name タグはついていないので別途 Name タグをつける必要があります。

 

 

 

 

セキュリティグループにタグを付ける

別途「create-tags」コマンドでセキュリティグループにタグをつけます。

create-security-group コマンドではセキュリティグループを作成する際に同時にタグ付けはできません。タグ付のオプションがありません。

 

上記コマンドを実行する前は下図のように Name タグがない状態です。

 

 

 

■セキュリティグループにタグを付ける

[root@DESKTOP-KSVJFCR ~]# aws ec2 create-tags --resources `aws ec2 describe-security-groups --filters "Name=group-name,Values=Test-EC2-Instance-001-sg" | jq '.SecurityGroups[].GroupId' -r` --tags Key=Name,Value=Test-EC2-Instance-001-sg

[root@DESKTOP-KSVJFCR ~]#

 

 

 

上記コマンドを実行すると下図のように Name タグが付きました。

 

 

 

 

 

 

 

セキュリティグループにルールを追加する

セキュリティグループを作成したら、次にセキュリティグループに「ルール」を作成します。

 

セキュリティグループ作成直後は、ルールが存在していないことが確認できます。

 

■設定前のセキュリティグループの状態

 

 

 

セキュリティグループのルールを作成する前に事前準備をします。

 

■事前準備

セキュリティを考慮し、インターネット経由で EC2 に ssh ログインをする場合は、確認くんでグローバル IP アドレスを調べて対象のグローバル IP のみ許可します。

 

確認くん

https://www.ugtop.com/spill.shtml

 

下図のようにグローバル IP アドレスが表示されるので控えておきます。

 

以下の2つのルールを追加します。

 

■セキュリティグループへのルールの追加

[root@DESKTOP-KSVJFCR ~]# aws ec2 authorize-security-group-ingress --group-id `aws ec2 describe-security-groups --filters "Name=group-name,Values=Test-EC2-Instance-001-sg" | jq '.SecurityGroups[].GroupId' -r` --ip-permissions IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges='[{CidrIp=1xx.xxx.xxx.xx9/32,Description="NetworkSegment0"}]' IpProtocol=icmp,FromPort=-1,ToPort=-1,IpRanges='[{CidrIp=1xx.xxx.xxx.xx9/32,Description="NetworkSegment0"}]'
[root@DESKTOP-KSVJFCR ~]#

 

 

上記コマンドを実行すると、下図のようにルールが追加されることが確認できます。

 

 

 

 

 

 

 

 

 

セキュリティグループ一覧の表示

先ほど設定したセキュリティグループの確認をします。

 

■セキュリティグループの一覧表示

[root@DESKTOP-KSVJFCR ~]# aws ec2 describe-security-groups --query "SecurityGroups[].[GroupName,GroupId]" --output table 
------------------------------------------------------ 
|               DescribeSecurityGroups               | 
+---------------------------+------------------------+ 
|  launch-wizard-6          |  sg-013f7ddaa18bc9376  | 
|  launch-wizard-7          |  sg-01a6807414610d624  | 
|  Test-EC2-Instance-001-sg |  sg-0a805239854d7d1da  | 
|  default                  |  sg-922e21ea           | 
+---------------------------+------------------------+ 
[root@DESKTOP-KSVJFCR ~]#

 

 

 

 

設定内容の確認コマンド

セキュリティグループのルールを確認します。

 

■セキュリティグループのルールを確認(Json形式)

※1xx.xxx.xxx.xx9/32 は確認くんで確認したグローバル IP アドレスです。

[root@DESKTOP-KSVJFCR ~]# aws ec2 describe-security-groups --filters "Name=group-name,Values=Test-EC2-Instance-001-sg" 

    "SecurityGroups": [ 
        { 
            "IpPermissionsEgress": [ 
                { 
                    "IpProtocol": "-1", 
                    "PrefixListIds": [], 
                    "IpRanges": [ 
                        { 
                            "CidrIp": "0.0.0.0/0" 
                        } 
                    ], 
                    "UserIdGroupPairs": [], 
                    "Ipv6Ranges": [] 
                } 
            ], 
            "Description": "Test-EC2-Instance-001-sg", 
            "Tags": [ 
                { 
                    "Value": "Test-EC2-Instance-001-sg", 
                    "Key": "Name" 
                } 
            ], 
            "IpPermissions": [ 
                { 
                    "PrefixListIds": [], 
                    "FromPort": 22, 
                    "IpRanges": [ 
                        { 
                            "Description": "NetworkSegment0", 
                            "CidrIp": "1xx.xxx.xxx.xx9/32" 
                        } 
                    ], 
                    "ToPort": 22, 
                    "IpProtocol": "tcp", 
                    "UserIdGroupPairs": [], 
                    "Ipv6Ranges": [] 
                }, 
                { 
                    "PrefixListIds": [], 
                    "FromPort": -1, 
                    "IpRanges": [ 
                        { 
                            "Description": "NetworkSegment0", 
                            "CidrIp": "1xx.xxx.xxx.xx9/32" 
                        } 
                    ], 
                    "ToPort": -1, 
                    "IpProtocol": "icmp", 
                    "UserIdGroupPairs": [], 
                    "Ipv6Ranges": [] 
                } 
            ], 
            "GroupName": "Test-EC2-Instance-001-sg", 
            "VpcId": "vpc-xxxxxxxxxxx6bb", 
            "OwnerId": "2xxxxxxxxxxxxxxx", 
            "GroupId": "sg-0a805239854d7d1da" 
        } 
    ] 

[root@DESKTOP-KSVJFCR ~]# 

 

 

 

 

 

 

■セキュリティグループのルールを確認(Table形式)

[root@DESKTOP-KSVJFCR ~]# aws ec2 describe-security-groups --filters "Name=group-name,Values=Test-EC2-Instance-001-sg" --output table 
----------------------------------------------- 
|           DescribeSecurityGroups            | 
+---------------------------------------------+ 
||              SecurityGroups               || 
|+--------------+----------------------------+| 
||  Description |  Test-EC2-Instance-001-sg  || 
||  GroupId     |  sg-0a805239854d7d1da      || 
||  GroupName   |  Test-EC2-Instance-001-sg  || 
||  OwnerId     |  2xxxxxxxxxxxxxxx          || 
||  VpcId       |  vpc-xxxxxxxxxxx6bb        || 
|+--------------+----------------------------+| 
|||              IpPermissions              ||| 
||+---------------------------+-------------+|| 
|||  FromPort                 |  22         ||| 
|||  IpProtocol               |  tcp        ||| 
|||  ToPort                   |  22         ||| 
||+---------------------------+-------------+|| 
||||               IpRanges                |||| 
|||+---------------+-----------------------+||| 
||||  CidrIp       |  1xx.xxx.xxx.xx9/32   |||| 
||||  Description  |  NetworkSegment0      |||| 
|||+---------------+-----------------------+||| 
|||              IpPermissions              ||| 
||+-------------------------+---------------+|| 
|||  FromPort               |  -1           ||| 
|||  IpProtocol             |  icmp         ||| 
|||  ToPort                 |  -1           ||| 
||+-------------------------+---------------+|| 
||||               IpRanges                |||| 
|||+---------------+-----------------------+||| 
||||  CidrIp       |  1xx.xxx.xxx.xx9/32   |||| 
||||  Description  |  NetworkSegment0      |||| 
|||+---------------+-----------------------+||| 
|||           IpPermissionsEgress           ||| 
||+----------------------------+------------+|| 
|||  IpProtocol                |  -1        ||| 
||+----------------------------+------------+|| 
||||               IpRanges                |||| 
|||+----------------+----------------------+||| 
||||  CidrIp        |  0.0.0.0/0           |||| 
|||+----------------+----------------------+||| 
|||                  Tags                   ||| 
||+--------+--------------------------------+|| 
|||  Key   |  Name                          ||| 
|||  Value |  Test-EC2-Instance-001-sg      ||| 
||+--------+--------------------------------+|| 
[root@DESKTOP-KSVJFCR ~]# 

 

 

 

 

 

 

 

 

 

AWS CLI より AMI を検索する

以下のコマンドで最新の AMI を検索できます。

 

■RedHatの AMI 一覧を表示(テーブル形式)

PS C:\Users\masato> aws ec2 describe-images --owners 309956199498 --filters "Name=name,Values=RHEL-*" --output table 
-------------------------------------------------------------------------------------------- 
|                                      DescribeImages                                      | 
+------------------------------------------------------------------------------------------+ 
||                                         Images                                         || 
|+---------------------+------------------------------------------------------------------+| 
||  Architecture       |  x86_64                                                          || 
||  CreationDate       |  2018-08-10T18:41:25.000Z                                        || 
||  Description        |  Provided by Red Hat, Inc.                                       || 
||  EnaSupport         |  False                                                           || 
||  Hypervisor         |  xen                                                             || 
||  ImageId            |  ami-00436f752b63a5555                                           || 
||  ImageLocation      |  309956199498/RHEL-6.10_HVM_GA-20180810-x86_64-0-Hourly2-GP2     || 
||  ImageType          |  machine                                                         || 
||  KernelId           |                                                                  || 
||  Name               |  RHEL-6.10_HVM_GA-20180810-x86_64-0-Hourly2-GP2                  || 
||  OwnerId            |  309956199498                                                    || 
||  Public             |  True                                                            || 
||  RootDeviceName     |  /dev/sda1                                                       || 
||  RootDeviceType     |  ebs                                                             || 
||  SriovNetSupport    |  simple                                                          || 
||  State              |  available                                                       || 
||  VirtualizationType |  hvm                                                             || 
|+---------------------+------------------------------------------------------------------+| 
|||                                  BlockDeviceMappings                                 ||| 
||+--------------------------------------------+-----------------------------------------+|| 
|||  DeviceName                                |  /dev/sda1                              ||| 
||+--------------------------------------------+-----------------------------------------+|| 
||||                                         Ebs                                        |||| 
|||+--------------------------------------+---------------------------------------------+||| 
||||  DeleteOnTermination                 |  True                                       |||| 
||||  Encrypted                           |  False                                      |||| 
||||  SnapshotId                          |  snap-0042912ac3b29db51                     |||| 
||||  VolumeSize                          |  10                                         |||| 
||||  VolumeType                          |  gp2                                        |||| 
|||+--------------------------------------+---------------------------------------------+||| 
||                                         Images                                         || 
|+----------------------+-----------------------------------------------------------------+| 
||  Architecture        |  x86_64                                                         || 
||  CreationDate        |  2019-02-06T00:23:09.000Z                                       || 
||  Description         |  Provided by Red Hat, Inc.                                      || 
||  EnaSupport          |  True                                                           || 
||  Hypervisor          |  xen                                                            || 
||  ImageId             |  ami-00b95502a4d51a07e                                          || 
||  ImageLocation       |  309956199498/RHEL-7.6_HVM_GA-20190128-x86_64-0-Hourly2-GP2     || 
||  ImageType           |  machine                                                        || 
||  KernelId            |                                                                 || 
||  Name                |  RHEL-7.6_HVM_GA-20190128-x86_64-0-Hourly2-GP2                  || 
||  OwnerId             |  309956199498                                                   || 
||  Public              |  True                                                           || 
||  RootDeviceName      |  /dev/sda1                                                      || 
||  RootDeviceType      |  ebs                                                            || 
||  SriovNetSupport     |  simple                                                         || 
||  State               |  available                                                      || 
||  VirtualizationType  |  hvm                                                            || 
|+----------------------+-----------------------------------------------------------------+| 
|||                                  BlockDeviceMappings                                 ||| 
||+--------------------------------------------+-----------------------------------------+|| 
|||  DeviceName                                |  /dev/sda1                              ||| 
||+--------------------------------------------+-----------------------------------------+|| 
||||                                         Ebs                                        |||| 
|||+--------------------------------------+---------------------------------------------+||| 
||||  DeleteOnTermination                 |  True                                       |||| 
||||  Encrypted                           |  False                                      |||| 
||||  SnapshotId                          |  snap-08eecb6dd4baa3f84                     |||| 
||||  VolumeSize                          |  10                                         |||| 
||||  VolumeType                          |  gp2                                        |||| 
|||+--------------------------------------+---------------------------------------------+||| 
||                                         Images                                         || 
|+----------------------+-----------------------------------------------------------------+| 
||  Architecture        |  x86_64                                                         || 
||  CreationDate        |  2019-06-26T16:22:14.000Z                                       || 
||  Description         |  Provided by Red Hat, Inc.                                      || 
||  EnaSupport          |  False                                                          || 
||  Hypervisor          |  xen                                                            || 
||  ImageId             |  ami-0143472ad41ac57de                                          || 
||  ImageLocation       |  309956199498/RHEL-6.10_HVM-20190621-x86_64-2-Hourly2-GP2       || 
||  ImageType           |  machine                                                        || 
||  KernelId            |                                                                 || 
||  Name                |  RHEL-6.10_HVM-20190621-x86_64-2-Hourly2-GP2                    || 
||  OwnerId             |  309956199498                                                   || 
||  Public              |  True                                                           || 
||  RootDeviceName      |  /dev/sda1                                                      || 
||  RootDeviceType      |  ebs                                                            || 
||  SriovNetSupport     |  simple                                                         || 
||  State               |  available                                                      || 
||  VirtualizationType  |  hvm                                                            || 
|+----------------------+-----------------------------------------------------------------+| 
|||                                  BlockDeviceMappings                                 ||| 
||+--------------------------------------------+-----------------------------------------+|| 
|||  DeviceName                                |  /dev/sda1                              ||| 
||+--------------------------------------------+-----------------------------------------+|| 
||||                                         Ebs                                        |||| 
|||+--------------------------------------+---------------------------------------------+||| 
||||  DeleteOnTermination                 |  True                                       |||| 
||||  Encrypted                           |  False                                      |||| 
||||  SnapshotId                          |  snap-091730ec922869c9b                     |||| 
||||  VolumeSize                          |  10                                         |||| 
||||  VolumeType                          |  gp2                                        |||| 
|||+--------------------------------------+---------------------------------------------+||| 
||                                         Images                                         ||

 

~ 以下、省略 ~

 

 

 

 

■RedHatの AMI 一覧を表示(ID と AMI 名をテーブル形式)

[root@DESKTOP-KSVJFCR ~]# aws ec2 describe-images --owners 309956199498 --filters "Name=name,Values=RHEL-*" --query 'Images[*].[ImageId,Name]' --output table 
-------------------------------------------------------------------------------- 
|                                DescribeImages                                | 
+------------------------+-----------------------------------------------------+ 
|  ami-00436f752b63a5555 |  RHEL-6.10_HVM_GA-20180810-x86_64-0-Hourly2-GP2     | 
|  ami-00b95502a4d51a07e |  RHEL-7.6_HVM_GA-20190128-x86_64-0-Hourly2-GP2      | 
|  ami-0143472ad41ac57de |  RHEL-6.10_HVM-20190621-x86_64-2-Hourly2-GP2        | 
|  ami-0302fadfb901ae198 |  RHEL-8.1.0_HVM-20191029-x86_64-0-Hourly2-GP2       | 
|  ami-03c6a4362c5fb8c61 |  RHEL-8.0.0_HVM-20190426-x86_64-1-Hourly2-GP2       | 
|  ami-04a12c0fbaeac005e |  RHEL-7.7_HVM-20191028-x86_64-1-Hourly2-GP2         | 
|  ami-04d511a6ab89b658d |  RHEL-6.10_HVM-20190923-x86_64-1-Hourly2-GP2        | 
|  ami-04eb805483f5607a7 |  RHEL-7.6_HVM-20190515-x86_64-0-Hourly2-GP2         | 
|  ami-05555f985af973005 |  RHEL-7.6_HVM_BETA-20180814-x86_64-0-Hourly2-GP2    | 
|  ami-076764d7a510da3f2 |  RHEL-8.0_HVM-20190920-arm64-0-Hourly2-GP2          | 
|  ami-07db7474f01e1ec62 |  RHEL-6.10_HVM-20190524-x86_64-0-Hourly2-GP2        | 
|  ami-08419d23bf91152e4 |  RHEL-7.6_HVM_GA-20181017-x86_64-0-Hourly2-GP2      | 
|  ami-08471dfdb9a833c1f |  RHEL-8.1.0_HVM-20191029-arm64-0-Hourly2-GP2        | 
|  ami-09b436e68bc3d1eea |  RHEL-7.8_HVM_BETA-20191009-x86_64-6-Hourly2-GP2    | 
|  ami-09f31cc5d5eecca1a |  RHEL-8.0.0_HVM-20190618-x86_64-1-Hourly2-GP2       | 
|  ami-0b355f24363d9f357 |  RHEL-7.7_HVM-20191119-x86_64-2-Hourly2-GP2         | 
|  ami-0b517025bb2f0ad4a |  RHEL-7.5_HVM-20180813-x86_64-0-Hourly2-GP2         | 
|  ami-0be4c0b05bbeb2afd |  RHEL-7.7_HVM-20190923-x86_64-0-Hourly2-GP2         | 
|  ami-0c09ca180274c253a |  RHEL-8.1.0_HVM_BETA-20190701-x86_64-0-Hourly2-GP2  | 
|  ami-0c3b878cc19d12d6c |  RHEL-8.0.0_HVM-20190520-x86_64-1-Hourly2-GP2       | 
|  ami-0c45b9b8b241f629f |  RHEL-8.0_HVM-20190920-x86_64-0-Hourly2-GP2         | 
|  ami-0d585463          |  RHEL-6.7_HVM-20160301-x86_64-3-Hourly2-GP2         | 
|  ami-0d938ac9c11076133 |  RHEL-8.0_HVM_BETA-20181113-x86_64-1-Hourly2-GP2    | 
|  ami-0dc41c7805e171046 |  RHEL-7.7_HVM_GA-20190723-x86_64-1-Hourly2-GP2      | 
|  ami-0dd8f963          |  RHEL-7.2_HVM_GA-20151112-x86_64-1-Hourly2-GP2      | 
|  ami-0e3e6ca71a19ccf06 |  RHEL-7.6_HVM-20190618-x86_64-0-Hourly2-GP2         | 
|  ami-0f2be75e3dfc43426 |  RHEL-7.7_HVM_BETA-20190530-x86_64-1-Hourly2-GP2    | 
|  ami-111f7910          |  RHEL-6.5_HVM_GA-x86_64-4-HVM-Hourly2               | 
|  ami-1130be10          |  RHEL-5.9_GA-x86_64-3-Hourly2                       | 
|  ami-1255d713          |  RHEL-6.4_GA_HVM-x86_64-5-HVM-Hourly2               | 
|  ami-13e1fa6f          |  RHEL-6.10_HVM_Beta-20180418-x86_64-1-Hourly2-GP2   | 
|  ami-194cd17f          |  RHEL-6.9_HVM-20180103-x86_64-1-Hourly2-GP2         | 
|  ami-1bdc551a          |  RHEL-5.5_GA-i386-4-Hourly2                         | 
|  ami-1c405d72          |  RHEL-6.7_HVM-20160412-x86_64-1-Hourly2-GP2         | 
|  ami-1e434370          |  RHEL-6.7_HVM-20160219-x86_64-1-Hourly2-GP2         | 
|  ami-1fe7b71e          |  RHEL-5.10_GA-x86_64-8-Hourly2                      | 
|  ami-2265c543          |  RHEL-7.2_HVM-20161025-x86_64-1-Hourly2-GP2         | 
|  ami-29c59d28          |  RHEL-5.11_Beta-x86_64-3-Hourly2-GP2                | 
|  ami-2b542b2a          |  RHEL-6.5_GA-i386-1-Hourly2                         | 
|  ami-2c14b44d          |  RHEL-6.8_HVM-20161026-x86_64-1-Hourly2-GP2         | 
|  ami-2d50214a          |  RHEL-6.9_HVM_Beta-20161219-x86_64-2-Hourly2-GP2    | 

 

~ 以下、省略 ~

[root@DESKTOP-KSVJFCR ~]# 

 

 

 

日付でソートする

上記の結果を日付で新しいもの順にソートします。

 

■RedHatの AMI 一覧を表示(バラバラに表示)

[root@DESKTOP-KSVJFCR ~]# aws ec2 describe-images --owners 309956199498 --filters "Name=name,Values=RHEL-*" --query 'Images[*].[ImageId,Name,CreationDate]' --output table 
------------------------------------------------------------------------------------------------------------ 
|                                              DescribeImages                                              | 
+-----------------------+-----------------------------------------------------+----------------------------+ 
|  ami-00436f752b63a5555|  RHEL-6.10_HVM_GA-20180810-x86_64-0-Hourly2-GP2     |  2018-08-10T18:41:25.000Z  | 
|  ami-00b95502a4d51a07e|  RHEL-7.6_HVM_GA-20190128-x86_64-0-Hourly2-GP2      |  2019-02-06T00:23:09.000Z  | 
|  ami-0143472ad41ac57de|  RHEL-6.10_HVM-20190621-x86_64-2-Hourly2-GP2        |  2019-06-26T16:22:14.000Z  | 
|  ami-0302fadfb901ae198|  RHEL-8.1.0_HVM-20191029-x86_64-0-Hourly2-GP2       |  2019-10-30T10:14:36.000Z  | 
|  ami-03c6a4362c5fb8c61|  RHEL-8.0.0_HVM-20190426-x86_64-1-Hourly2-GP2       |  2019-04-26T20:05:27.000Z  | 
|  ami-04a12c0fbaeac005e|  RHEL-7.7_HVM-20191028-x86_64-1-Hourly2-GP2         |  2019-10-29T13:27:23.000Z  | 
|  ami-04d511a6ab89b658d|  RHEL-6.10_HVM-20190923-x86_64-1-Hourly2-GP2        |  2019-09-23T18:05:43.000Z  | 
|  ami-04eb805483f5607a7|  RHEL-7.6_HVM-20190515-x86_64-0-Hourly2-GP2         |  2019-05-15T17:07:36.000Z  | 
|  ami-05555f985af973005|  RHEL-7.6_HVM_BETA-20180814-x86_64-0-Hourly2-GP2    |  2018-08-16T19:01:34.000Z  | 
|  ami-076764d7a510da3f2|  RHEL-8.0_HVM-20190920-arm64-0-Hourly2-GP2          |  2019-09-23T19:15:03.000Z  | 
|  ami-07db7474f01e1ec62|  RHEL-6.10_HVM-20190524-x86_64-0-Hourly2-GP2        |  2019-05-29T16:09:31.000Z  | 
|  ami-08419d23bf91152e4|  RHEL-7.6_HVM_GA-20181017-x86_64-0-Hourly2-GP2      |  2018-10-17T14:14:10.000Z  | 
|  ami-08471dfdb9a833c1f|  RHEL-8.1.0_HVM-20191029-arm64-0-Hourly2-GP2        |  2019-10-30T10:49:02.000Z  | 
|  ami-09b436e68bc3d1eea|  RHEL-7.8_HVM_BETA-20191009-x86_64-6-Hourly2-GP2    |  2019-10-21T12:09:29.000Z  | 
|  ami-09f31cc5d5eecca1a|  RHEL-8.0.0_HVM-20190618-x86_64-1-Hourly2-GP2       |  2019-06-19T19:55:31.000Z  | 
|  ami-0b355f24363d9f357|  RHEL-7.7_HVM-20191119-x86_64-2-Hourly2-GP2         |  2019-11-19T15:43:50.000Z  | 
|  ami-0b517025bb2f0ad4a|  RHEL-7.5_HVM-20180813-x86_64-0-Hourly2-GP2         |  2018-08-15T15:42:22.000Z  | 
|  ami-0be4c0b05bbeb2afd|  RHEL-7.7_HVM-20190923-x86_64-0-Hourly2-GP2         |  2019-09-23T13:47:26.000Z  | 
|  ami-0c09ca180274c253a|  RHEL-8.1.0_HVM_BETA-20190701-x86_64-0-Hourly2-GP2  |  2019-07-21T19:37:12.000Z  | 
|  ami-0c3b878cc19d12d6c|  RHEL-8.0.0_HVM-20190520-x86_64-1-Hourly2-GP2       |  2019-05-20T22:19:27.000Z  | 
|  ami-0c45b9b8b241f629f|  RHEL-8.0_HVM-20190920-x86_64-0-Hourly2-GP2         |  2019-09-23T20:01:56.000Z  | 
|  ami-0d585463         |  RHEL-6.7_HVM-20160301-x86_64-3-Hourly2-GP2         |  2016-03-02T16:34:00.000Z  | 
|  ami-0d938ac9c11076133|  RHEL-8.0_HVM_BETA-20181113-x86_64-1-Hourly2-GP2    |  2018-11-13T21:36:49.000Z  | 
|  ami-0dc41c7805e171046|  RHEL-7.7_HVM_GA-20190723-x86_64-1-Hourly2-GP2      |  2019-07-24T09:17:45.000Z  | 
|  ami-0dd8f963         |  RHEL-7.2_HVM_GA-20151112-x86_64-1-Hourly2-GP2      |  2015-11-12T21:11:15.000Z  | 
|  ami-0e3e6ca71a19ccf06|  RHEL-7.6_HVM-20190618-x86_64-0-Hourly2-GP2         |  2019-06-18T20:13:57.000Z  | 
|  ami-0f2be75e3dfc43426|  RHEL-7.7_HVM_BETA-20190530-x86_64-1-Hourly2-GP2    |  2019-06-05T08:49:41.000Z  | 
|  ami-111f7910         |  RHEL-6.5_HVM_GA-x86_64-4-HVM-Hourly2               |  2013-11-21T13:28:14.000Z  | 
|  ami-1130be10         |  RHEL-5.9_GA-x86_64-3-Hourly2                       |  2013-05-19T07:29:41.000Z  | 
|  ami-1255d713         |  RHEL-6.4_GA_HVM-x86_64-5-HVM-Hourly2               |  2013-04-11T04:03:54.000Z  | 

 

~ 以下、省略 ~

[root@DESKTOP-KSVJFCR ~]#

 

 

 

 

最新の AMI からソートをします。「CreationDate」の項目でソートすると良さそうです。

PS C:\> aws ec2 describe-images --owners 309956199498 --filters "Name=name,Values=RHEL-*"  

    "Images": [ 
        { 
            "Architecture": "x86_64", 
            "CreationDate": "2018-08-10T18:41:25.000Z", 
            "ImageId": "ami-00436f752b63a5555", 
            "ImageLocation": "309956199498/RHEL-6.10_HVM_GA-20180810-x86_64-0-Hourly2-GP2", 
            "ImageType": "machine", 
            "Public": true, 
            "OwnerId": "309956199498", 
            "State": "available", 
            "BlockDeviceMappings": [ 
                { 
                    "DeviceName": "/dev/sda1", 
                    "Ebs": { 
                        "DeleteOnTermination": true, 
                        "SnapshotId": "snap-0042912ac3b29db51", 
                        "VolumeSize": 10, 
                        "VolumeType": "gp2", 
                        "Encrypted": false 
                    } 
                } 
            ], 
            "Description": "Provided by Red Hat, Inc.", 
            "EnaSupport": false, 
            "Hypervisor": "xen", 
            "Name": "RHEL-6.10_HVM_GA-20180810-x86_64-0-Hourly2-GP2", 
            "RootDeviceName": "/dev/sda1", 
            "RootDeviceType": "ebs", 
            "SriovNetSupport": "simple", 
            "VirtualizationType": "hvm" 
        },

 

~ 以下、省略 ~

 

 

 

■RedHatの AMI 一覧を表示(古いもの順に表示)

[root@DESKTOP-KSVJFCR ~]# aws ec2 describe-images --owners 309956199498 --filters "Name=name,Values=RHEL-*" --query "sort_by(Images,&CreationDate)[].[ImageId,Name,CreationDate]" --output table 
------------------------------------------------------------------------------------------------------------ 
|                                              DescribeImages                                              | 
+-----------------------+-----------------------------------------------------+----------------------------+ 
|  ami-f7890ef6         |  RHEL-6.4_GA_HVM-x86_64-4-HVM-Hourly2               |  2013-02-15T00:43:48.000Z  | 
|  ami-1255d713         |  RHEL-6.4_GA_HVM-x86_64-5-HVM-Hourly2               |  2013-04-11T04:03:54.000Z  | 
|  ami-8b4dc28a         |  RHEL-6.4_GA_HVM-x86_64-7-HVM-Hourly2               |  2013-05-10T03:35:06.000Z  | 
|  ami-d14dc2d0         |  RHEL-6.4_GA-i386-7-Hourly2                         |  2013-05-10T03:58:04.000Z  | 
|  ami-e74dc2e6         |  RHEL-6.4_GA-x86_64-7-Hourly2                       |  2013-05-10T04:05:56.000Z  | 
|  ami-a528a6a4         |  RHEL-6.0_GA-x86_64-6-Hourly2                       |  2013-05-18T12:37:12.000Z  | 
|  ami-5129a750         |  RHEL-6.1_GA-x86_64-5-Hourly2                       |  2013-05-18T13:52:25.000Z  | 
|  ami-9526a894         |  RHEL-6.2_GA-x86_64-4-Hourly2                       |  2013-05-18T15:17:55.000Z  | 
|  ami-a325aba2         |  RHEL-6.0_GA-i386-6-Hourly2                         |  2013-05-18T16:46:00.000Z  | 
|  ami-cf20aece         |  RHEL-6.1_GA-i386-5-Hourly2                         |  2013-05-18T18:17:58.000Z  | 

~ 以下、省略 ~

 

 

reverse を入れてソートを逆にします。

■RedHatの AMI 一覧を表示(新しいもの順に表示)

[root@DESKTOP-KSVJFCR ~]# aws ec2 describe-images --owners 309956199498 --filters "Name=name,Values=RHEL-*" --query "reverse(sort_by(Images,&CreationDate)[].[ImageId,Name,CreationDate])" --output table 
------------------------------------------------------------------------------------------------------------ 
|                                              DescribeImages                                              | 
+-----------------------+-----------------------------------------------------+----------------------------+ 
|  ami-0b355f24363d9f357|  RHEL-7.7_HVM-20191119-x86_64-2-Hourly2-GP2         |  2019-11-19T15:43:50.000Z  | 
|  ami-08471dfdb9a833c1f|  RHEL-8.1.0_HVM-20191029-arm64-0-Hourly2-GP2        |  2019-10-30T10:49:02.000Z  | 
|  ami-0302fadfb901ae198|  RHEL-8.1.0_HVM-20191029-x86_64-0-Hourly2-GP2       |  2019-10-30T10:14:36.000Z  | 
|  ami-04a12c0fbaeac005e|  RHEL-7.7_HVM-20191028-x86_64-1-Hourly2-GP2         |  2019-10-29T13:27:23.000Z  | 
|  ami-09b436e68bc3d1eea|  RHEL-7.8_HVM_BETA-20191009-x86_64-6-Hourly2-GP2    |  2019-10-21T12:09:29.000Z  | 
|  ami-0c45b9b8b241f629f|  RHEL-8.0_HVM-20190920-x86_64-0-Hourly2-GP2         |  2019-09-23T20:01:56.000Z  | 
|  ami-076764d7a510da3f2|  RHEL-8.0_HVM-20190920-arm64-0-Hourly2-GP2          |  2019-09-23T19:15:03.000Z  | 
|  ami-04d511a6ab89b658d|  RHEL-6.10_HVM-20190923-x86_64-1-Hourly2-GP2        |  2019-09-23T18:05:43.000Z  | 
|  ami-0be4c0b05bbeb2afd|  RHEL-7.7_HVM-20190923-x86_64-0-Hourly2-GP2         |  2019-09-23T13:47:26.000Z  | 
|  ami-0dc41c7805e171046|  RHEL-7.7_HVM_GA-20190723-x86_64-1-Hourly2-GP2      |  2019-07-24T09:17:45.000Z  | 
|  ami-0c09ca180274c253a|  RHEL-8.1.0_HVM_BETA-20190701-x86_64-0-Hourly2-GP2  |  2019-07-21T19:37:12.000Z  | 
|  ami-0143472ad41ac57de|  RHEL-6.10_HVM-20190621-x86_64-2-Hourly2-GP2        |  2019-06-26T16:22:14.000Z  | 
|  ami-09f31cc5d5eecca1a|  RHEL-8.0.0_HVM-20190618-x86_64-1-Hourly2-GP2       |  2019-06-19T19:55:31.000Z  | 
|  ami-0e3e6ca71a19ccf06|  RHEL-7.6_HVM-20190618-x86_64-0-Hourly2-GP2         |  2019-06-18T20:13:57.000Z  | 
|  ami-0f2be75e3dfc43426|  RHEL-7.7_HVM_BETA-20190530-x86_64-1-Hourly2-GP2    |  2019-06-05T08:49:41.000Z  | 

 

~ 以下、省略 ~

 

 

さらに絞ります。

■RedHatの AMI 一覧を表示(新しいもの順に RedHat 8 系のみ表示)

[root@DESKTOP-KSVJFCR ~]# aws ec2 describe-images --owners 309956199498 --filters "Name=name,Values=RHEL-8.*" --query "reverse(sort_by(Images,&CreationDate)[].[ImageId,Name,CreationDate])" --output table 
------------------------------------------------------------------------------------------------------------ 
|                                              DescribeImages                                              | 
+-----------------------+-----------------------------------------------------+----------------------------+ 
|  ami-08471dfdb9a833c1f|  RHEL-8.1.0_HVM-20191029-arm64-0-Hourly2-GP2        |  2019-10-30T10:49:02.000Z  | 
|  ami-0302fadfb901ae198|  RHEL-8.1.0_HVM-20191029-x86_64-0-Hourly2-GP2       |  2019-10-30T10:14:36.000Z  | 
|  ami-0c45b9b8b241f629f|  RHEL-8.0_HVM-20190920-x86_64-0-Hourly2-GP2         |  2019-09-23T20:01:56.000Z  | 
|  ami-076764d7a510da3f2|  RHEL-8.0_HVM-20190920-arm64-0-Hourly2-GP2          |  2019-09-23T19:15:03.000Z  | 
|  ami-0c09ca180274c253a|  RHEL-8.1.0_HVM_BETA-20190701-x86_64-0-Hourly2-GP2  |  2019-07-21T19:37:12.000Z  | 
|  ami-09f31cc5d5eecca1a|  RHEL-8.0.0_HVM-20190618-x86_64-1-Hourly2-GP2       |  2019-06-19T19:55:31.000Z  | 
|  ami-0c3b878cc19d12d6c|  RHEL-8.0.0_HVM-20190520-x86_64-1-Hourly2-GP2       |  2019-05-20T22:19:27.000Z  | 
|  ami-03c6a4362c5fb8c61|  RHEL-8.0.0_HVM-20190426-x86_64-1-Hourly2-GP2       |  2019-04-26T20:05:27.000Z  | 
|  ami-0d938ac9c11076133|  RHEL-8.0_HVM_BETA-20181113-x86_64-1-Hourly2-GP2    |  2018-11-13T21:36:49.000Z  | 
+-----------------------+-----------------------------------------------------+----------------------------+ 
[root@DESKTOP-KSVJFCR ~]# 

 

 

 

さらに 1つに絞ります。

 

■RedHatの AMI 一覧を表示(新しいもの順に RedHat 8 系のみ 1つ表示)

[root@DESKTOP-KSVJFCR ~]# aws ec2 describe-images --owners 309956199498 --filters "Name=name,Values=RHEL-8.*" --query "reverse(sort_by(Images,&CreationDate)[-1].[ImageId,Name,CreationDate])" --output table 
------------------------------------------------- 
|                DescribeImages                 | 
+-----------------------------------------------+ 
|  2019-10-30T10:49:02.000Z                     | 
|  RHEL-8.1.0_HVM-20191029-arm64-0-Hourly2-GP2  | 
|  ami-08471dfdb9a833c1f                        | 
+-----------------------------------------------+ 
[root@DESKTOP-KSVJFCR ~]# 

 

 

 

現時点(2020年1月11日)で RedHat 8 系で一番最新の AMI は以下です。

 

 

 

すべての EC2 インスタンスタイプで使える AMI に絞る

上記の方法で最新の AMI を検索できますが、ただすべての EC2 インスタンスタイプで利用できるかは不明です。

 

その理由ですが、1つの AMI はすべてのインスタンスタイプで利用できるわけではないからです。

例えば、Windows Server with SQL Server はある程度スペックの高いインスタンスタイプしか選択できないことがあったり、CPU が x86 ではなく ARM64 だったりすると、特定のインスタンスタイプのみしか選択できないようになっています。 

 

【AWS】AWS CLI でクイックスタートに出てくる EC2 インスタンスの AMI を検索するコマンド

 

そのため、クイックスタートで表示されるインスタンスタイプを選択することが無難です。

 

[root@DESKTOP ~]# aws ec2 describe-images --owners 309956199498 --filters 'Name=name,Values=RHEL-8.0.0_HVM-*' 'Name=state,Values=available' --query 'reverse(sort_by(Images, &CreationDate))[:1].ImageId' --output text
ami-09f31cc5d5eecca1a
[root@DESKTOP ~]#

 

 

 

 

VPC ID の確認

VPC ID を確認します。

 

■VPC の ID を確認する(デフォルト VPC のみの場合)

[root@DESKTOP-KSVJFCR ~]# aws ec2 describe-vpcs 

    "Vpcs": [ 
        { 
            "VpcId": "vpc-xxxxxxxxxxx6bb", 
            "InstanceTenancy": "default", 
            "CidrBlockAssociationSet": [ 
                { 
                    "AssociationId": "vpc-cidr-assoc-02a5f26a", 
                    "CidrBlock": "172.31.0.0/16", 
                    "CidrBlockState": { 
                        "State": "associated" 
                    } 
                } 
            ], 
            "State": "available", 
            "DhcpOptionsId": "dopt-cce166ab", 
            "OwnerId": "2xxxxxxxxxxxxxxx", 
            "CidrBlock": "172.31.0.0/16", 
            "IsDefault": true 
        } 
    ] 

[root@DESKTOP-KSVJFCR ~]#

 

 

さらに VPC の ID だけに絞ります。

■VPC の ID を確認する(デフォルト VPC のみの場合)

[root@DESKTOP-KSVJFCR ~]# aws ec2 describe-vpcs --query 'Vpcs[0]'.{VPCID:VpcId} 

    "VPCID": "vpc-xxxxxxxxxxx6bb" 

[root@DESKTOP-KSVJFCR ~]#

 

 

text 出力にします。

■VPC の ID を確認する(デフォルト VPC のみの場合)

[root@DESKTOP-KSVJFCR ~]# aws ec2 describe-vpcs --query 'Vpcs[0]'.VpcId --output text
vpc-xxxxxxxxxxx6bb
[root@DESKTOP-KSVJFCR ~]#

 

 

 

サブネット ID の確認

サブネットを確認します。

■サブネットを確認する

[root@DESKTOP ~]# aws ec2 describe-subnets 

    "Subnets": [ 
        { 
            "MapPublicIpOnLaunch": false, 
            "AvailabilityZoneId": "apne1-az4", 
            "Tags": [ 
                { 
                    "Value": "Subnet172-31-20-0", 
                    "Key": "Name" 
                } 
            ], 
            "AvailableIpAddressCount": 251, 
            "DefaultForAz": false, 
            "SubnetArn": "arn:aws:ec2:ap-northeast-1:2xxxxxxxxxxxx3:subnet/subnet-0d7548e5439488164", 
            "Ipv6CidrBlockAssociationSet": [], 
            "VpcId": "vpc-dc2376bb", 
            "State": "available", 
            "AvailabilityZone": "ap-northeast-1a", 
            "SubnetId": "subnet-0d7548e5439488164", ← 今回対象のサブネット ID です。  
            "OwnerId": "2xxxxxxxxxxxx3", 
            "CidrBlock": "172.31.20.0/24", ← 今回対象のサブネットです。 
            "AssignIpv6AddressOnCreation": false 
        }, 
        { 
            "MapPublicIpOnLaunch": false, 
            "AvailabilityZoneId": "apne1-az2", 
            "Tags": [ 
                { 
                    "Value": "Subnet172-31-22-0", 
                    "Key": "Name" 
                } 
            ], 
            "AvailableIpAddressCount": 251, 
            "DefaultForAz": false, 
            "SubnetArn": "arn:aws:ec2:ap-northeast-1:2xxxxxxxxxxxx3:subnet/subnet-00d80e7b72df96d9c", 
            "Ipv6CidrBlockAssociationSet": [], 
            "VpcId": "vpc-dc2376bb", 
            "State": "available", 
            "AvailabilityZone": "ap-northeast-1d", 
            "SubnetId": "subnet-00d80e7b72df96d9c", 
            "OwnerId": "2xxxxxxxxxxxx3", 
            "CidrBlock": "172.31.22.0/24", 
            "AssignIpv6AddressOnCreation": false 
        }, 
        { 
            "MapPublicIpOnLaunch": true, 
            "AvailabilityZoneId": "apne1-az4", 
            "Tags": [ 
                { 
                    "Value": "Subnet172-31-32-0", 
                    "Key": "Name" 
                } 
            ], 
            "AvailableIpAddressCount": 4089, 
            "DefaultForAz": true, 
            "SubnetArn": "arn:aws:ec2:ap-northeast-1:2xxxxxxxxxxxx3:subnet/subnet-653b2a2c", 
            "Ipv6CidrBlockAssociationSet": [], 
            "VpcId": "vpc-dc2376bb", 
            "State": "available", 
            "AvailabilityZone": "ap-northeast-1a", 
            "SubnetId": "subnet-653b2a2c", 
            "OwnerId": "2xxxxxxxxxxxx3", 
            "CidrBlock": "172.31.32.0/20", 
            "AssignIpv6AddressOnCreation": false 
        } 
    ] 

[root@DESKTOP ~]#

 

 

 

鍵の確認

鍵を確認します。

 

■鍵の一覧を取得する

[root@DESKTOP-KSVJFCR ~]# aws ec2 describe-key-pairs 

    "KeyPairs": [ 
        { 
            "Tags": [], 
            "KeyName": "basic-network", 
            "KeyFingerprint": "53:94:e8:33:8a:a9:2d:2b:21:69:cc:02:3b:76:b7:d3:f1:7a:08:20", 
            "KeyPairId": "key-04f61dfe31a5d07a0" 
        }, 
        { 
            "Tags": [], 
            "KeyName": "powershell-windows2016", 
            "KeyFingerprint": "de:db:43:69:d6:be:4f:20:bc:23:48:27:01:09:f8:82:d4:ea:8d:e0", 
            "KeyPairId": "key-016cc8d743d411a8a" 
        }, 
        { 
            "Tags": [], 
            "KeyName": "test-ec2instance-key", 
            "KeyFingerprint": "ce:20:9b:bb:17:d0:7b:90:15:60:da:93:80:ec:66:82:df:21:e8:62", 
            "KeyPairId": "key-08e4aa7d8d8fccac5" 
        } 
    ] 

[root@DESKTOP-KSVJFCR ~]#

 

 

■鍵の一覧を取得する

[root@DESKTOP-KSVJFCR ~]# aws ec2 describe-key-pairs --query 'KeyPairs[*]'.KeyName --output text
basic-network     powershell-windows2016     test-ec2instance-key
[root@DESKTOP-KSVJFCR ~]#

 

 

 

 

取得した情報まとめ

 

 

■インスタンス作成コマンド

aws ec2 run-instances \
--region ap-northeast-1 \
--image-id ami-09f31cc5d5eecca1a \
--count 1 \
--instance-type t2.micro \
--key-name basic-network \
--subnet-id subnet-0d7548e5439488164 \
--private-ip-address 172.31.20.111 \
--associate-public-ip-address \
--security-group-ids `aws ec2 describe-security-groups --filters "Name=group-name,Values=Test-EC2-Instance-001-sg" | jq '.SecurityGroups[].GroupId' -r` --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=Test-EC2-Instance-001}]' \
--placement AvailabilityZone=ap-northeast-1a \
--output table

 

 

 

■インスタンス作成コマンド及び結果(Tableの場合)

[root@DESKTOP ~]# aws ec2 run-instances \ 
--region ap-northeast-1 \ 
> --image-id ami-09f31cc5d5eecca1a \ 
--count 1 \ 
--instance-type t2.micro \ 
--key-name basic-network \ 
> --subnet-id subnet-0d7548e5439488164 \ 
--private-ip-address 172.31.20.111 \ 
> --associate-public-ip-address \ 
> --security-group-ids `aws ec2 describe-security-groups --filters "Name=group-name,Values=Test-EC2-Instance-001-sg" | jq '.SecurityGroups[].GroupId' -r` --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=Test-EC2-Instance-001}]' \ 
> --placement AvailabilityZone=ap-northeast-1a \ 
--output table 
--------------------------------------------------------------------------------- 
|                                 RunInstances                                  | 
+--------------------------------+----------------------------------------------+ 
|  OwnerId                       |  2xxxxxxxxxxxxxxx                                | 
|  ReservationId                 |  r-04a42be4bb9fc6921                         | 
+--------------------------------+----------------------------------------------+ 
||                                  Instances                                  || 
|+------------------------+----------------------------------------------------+| 
||  AmiLaunchIndex        |  0                                                 || 
||  Architecture          |  x86_64                                            || 
||  ClientToken           |                                                    || 
||  EbsOptimized          |  False                                             || 
||  Hypervisor            |  xen                                               || 
||  ImageId               |  ami-09f31cc5d5eecca1a                             || 
||  InstanceId            |  i-09652b7f7053bb77b                               || 
||  InstanceType          |  t2.micro                                          || 
||  KeyName               |  basic-network                                     || 
||  LaunchTime            |  2020-01-12T00:59:31.000Z                          || 
||  PrivateDnsName        |  ip-172-31-20-111.ap-northeast-1.compute.internal  || 
||  PrivateIpAddress      |  172.31.20.111                                     || 
||  PublicDnsName         |                                                    || 
||  RootDeviceName        |  /dev/sda1                                         || 
||  RootDeviceType        |  ebs                                               || 
||  SourceDestCheck       |  True                                              || 
||  StateTransitionReason |                                                    || 
||  SubnetId              |  subnet-0d7548e5439488164                          || 
||  VirtualizationType    |  hvm                                               || 
||  VpcId                 |  vpc-xxxxxxxxxxx6bb                                      || 
|+------------------------+----------------------------------------------------+| 
|||                     CapacityReservationSpecification                      ||| 
||+------------------------------------------------------------+--------------+|| 
|||  CapacityReservationPreference                             |  open        ||| 
||+------------------------------------------------------------+--------------+|| 
|||                                CpuOptions                                 ||| 
||+----------------------------------------------------------+----------------+|| 
|||  CoreCount                                               |  1             ||| 
|||  ThreadsPerCore                                          |  1             ||| 
||+----------------------------------------------------------+----------------+|| 
|||                              MetadataOptions                              ||| 
||+---------------------------------------------------+-----------------------+|| 
|||  HttpEndpoint                                     |  enabled              ||| 
|||  HttpPutResponseHopLimit                          |  1                    ||| 
|||  HttpTokens                                       |  optional             ||| 
|||  State                                            |  pending              ||| 
||+---------------------------------------------------+-----------------------+|| 
|||                                Monitoring                                 ||| 
||+-------------------------------+-------------------------------------------+|| 
|||  State                        |  disabled                                 ||| 
||+-------------------------------+-------------------------------------------+|| 
|||                             NetworkInterfaces                             ||| 
||+---------------------+-----------------------------------------------------+|| 
|||  Description        |                                                     ||| 
|||  InterfaceType      |  interface                                          ||| 
|||  MacAddress         |  06:18:c6:74:83:4e                                  ||| 
|||  NetworkInterfaceId |  eni-0a30c030910c2af8a                              ||| 
|||  OwnerId            |  2xxxxxxxxxxxxxxx                                       ||| 
|||  PrivateDnsName     |  ip-172-31-20-111.ap-northeast-1.compute.internal   ||| 
|||  PrivateIpAddress   |  172.31.20.111                                      ||| 
|||  SourceDestCheck    |  True                                               ||| 
|||  Status             |  in-use                                             ||| 
|||  SubnetId           |  subnet-0d7548e5439488164                           ||| 
|||  VpcId              |  vpc-xxxxxxxxxxx6bb                                       ||| 
||+---------------------+-----------------------------------------------------+|| 
||||                               Attachment                                |||| 
|||+-----------------------------+-------------------------------------------+||| 
||||  AttachTime                 |  2020-01-12T00:59:31.000Z                 |||| 
||||  AttachmentId               |  eni-attach-053c301756b5f2baf             |||| 
||||  DeleteOnTermination        |  True                                     |||| 
||||  DeviceIndex                |  0                                        |||| 
||||  Status                     |  attaching                                |||| 
|||+-----------------------------+-------------------------------------------+||| 
||||                                 Groups                                  |||| 
|||+----------------------+--------------------------------------------------+||| 
||||  GroupId             |  sg-0a805239854d7d1da                            |||| 
||||  GroupName           |  Test-EC2-Instance-001-sg                        |||| 
|||+----------------------+--------------------------------------------------+||| 
||||                           PrivateIpAddresses                            |||| 
|||+-------------------+-----------------------------------------------------+||| 
||||  Primary          |  True                                               |||| 
||||  PrivateDnsName   |  ip-172-31-20-111.ap-northeast-1.compute.internal   |||| 
||||  PrivateIpAddress |  172.31.20.111                                      |||| 
|||+-------------------+-----------------------------------------------------+||| 
|||                                 Placement                                 ||| 
||+-------------------------------------+-------------------------------------+|| 
|||  AvailabilityZone                   |  ap-northeast-1a                    ||| 
|||  GroupName                          |                                     ||| 
|||  Tenancy                            |  default                            ||| 
||+-------------------------------------+-------------------------------------+|| 
|||                              SecurityGroups                               ||| 
||+----------------------+----------------------------------------------------+|| 
|||  GroupId             |  sg-0a805239854d7d1da                              ||| 
|||  GroupName           |  Test-EC2-Instance-001-sg                          ||| 
||+----------------------+----------------------------------------------------+|| 
|||                                   State                                   ||| 
||+------------------------------+--------------------------------------------+|| 
|||  Code                        |  0                                         ||| 
|||  Name                        |  pending                                   ||| 
||+------------------------------+--------------------------------------------+|| 
|||                                StateReason                                ||| 
||+------------------------------------+--------------------------------------+|| 
|||  Code                              |  pending                             ||| 
|||  Message                           |  pending                             ||| 
||+------------------------------------+--------------------------------------+|| 
|||                                   Tags                                    ||| 
||+------------------+--------------------------------------------------------+|| 
|||  Key             |  Name                                                  ||| 
|||  Value           |  Test-EC2-Instance-001                                 ||| 
||+------------------+--------------------------------------------------------+|| 
[root@DESKTOP ~]#

 

 

 

 

■インスタンス作成コマンド及び結果(Jsonの場合)

[root@DESKTOP ~]# aws ec2 run-instances \ 
--region ap-northeast-1 \ 
> --image-id ami-09f31cc5d5eecca1a \ 
--count 1 \ 
--instance-type t2.micro \ 
--key-name basic-network \ 
> --subnet-id subnet-0d7548e5439488164 \ 
--private-ip-address 172.31.20.111 \ 
> --associate-public-ip-address \ 
> --security-group-ids `aws ec2 describe-security-groups --filters "Name=group-name,Values=Test-EC2-Instance-001-sg" | jq '.SecurityGroups[].GroupId' -r` --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=Test-EC2-Instance-001}]' \ 
> --placement AvailabilityZone=ap-northeast-1a

    "Instances": [ 
        { 
            "Monitoring": { 
                "State": "disabled" 
            }, 
            "PublicDnsName": "", 
            "StateReason": { 
                "Message": "pending", 
                "Code": "pending" 
            }, 
            "State": { 
                "Code": 0, 
                "Name": "pending" 
            }, 
            "EbsOptimized": false, 
            "LaunchTime": "2020-01-12T01:47:07.000Z", 
            "PrivateIpAddress": "172.31.20.111", 
            "ProductCodes": [], 
            "VpcId": "vpc-xxxxxxxxxxx6bb", 
            "CpuOptions": { 
                "CoreCount": 1, 
                "ThreadsPerCore": 1 
            }, 
            "StateTransitionReason": "", 
            "InstanceId": "i-0c4d2382a84a57200", 
            "ImageId": "ami-09f31cc5d5eecca1a", 
            "PrivateDnsName": "ip-172-31-20-111.ap-northeast-1.compute.internal", 
            "KeyName": "basic-network", 
            "SecurityGroups": [ 
                { 
                    "GroupName": "Test-EC2-Instance-001-sg", 
                    "GroupId": "sg-0a805239854d7d1da" 
                } 
            ], 
            "ClientToken": "", 
            "SubnetId": "subnet-0d7548e5439488164", 
            "InstanceType": "t2.micro", 
            "CapacityReservationSpecification": { 
                "CapacityReservationPreference": "open" 
            }, 
            "NetworkInterfaces": [ 
                { 
                    "Status": "in-use", 
                    "MacAddress": "06:3c:5a:71:14:c8", 
                    "SourceDestCheck": true, 
                    "VpcId": "vpc-xxxxxxxxxxx6bb", 
                    "Description": "", 
                    "NetworkInterfaceId": "eni-01611dea0b250539f", 
                    "PrivateIpAddresses": [ 
                        { 
                            "PrivateDnsName": "ip-172-31-20-111.ap-northeast-1.compute.internal", 
                            "Primary": true, 
                            "PrivateIpAddress": "172.31.20.111" 
                        } 
                    ], 
                    "PrivateDnsName": "ip-172-31-20-111.ap-northeast-1.compute.internal", 
                    "InterfaceType": "interface", 
                    "Attachment": { 
                        "Status": "attaching", 
                        "DeviceIndex": 0, 
                        "DeleteOnTermination": true, 
                        "AttachmentId": "eni-attach-0ad7b2dbc4bde5a27", 
                        "AttachTime": "2020-01-12T01:47:07.000Z" 
                    }, 
                    "Groups": [ 
                        { 
                            "GroupName": "Test-EC2-Instance-001-sg", 
                            "GroupId": "sg-0a805239854d7d1da" 
                        } 
                    ], 
                    "Ipv6Addresses": [], 
                    "OwnerId": "2xxxxxxxxxxxxxxx", 
                    "SubnetId": "subnet-0d7548e5439488164", 
                    "PrivateIpAddress": "172.31.20.111" 
                } 
            ], 
            "SourceDestCheck": true, 
            "Placement": { 
                "Tenancy": "default", 
                "GroupName": "", 
                "AvailabilityZone": "ap-northeast-1a" 
            }, 
            "Hypervisor": "xen", 
            "BlockDeviceMappings": [], 
            "Architecture": "x86_64", 
            "RootDeviceType": "ebs", 
            "RootDeviceName": "/dev/sda1", 
            "VirtualizationType": "hvm", 
            "Tags": [ 
                { 
                    "Value": "Test-EC2-Instance-001", 
                    "Key": "Name" 
                } 
            ], 
            "MetadataOptions": { 
                "State": "pending", 
                "HttpEndpoint": "enabled", 
                "HttpTokens": "optional", 
                "HttpPutResponseHopLimit": 1 
            }, 
            "AmiLaunchIndex": 0 
        } 
    ], 
    "ReservationId": "r-00ab99c7a4a96b7a0", 
    "Groups": [], 
    "OwnerId": "2xxxxxxxxxxxxxxx" 

[root@DESKTOP ~]#

 

 

EC2 インスタンスが作成されたことを確認できました。

 

※この時点(aws ec2 run-instances コマンドを実行した時点)ではパブリック IP アドレスは不明なので、別途パブリック IP アドレスを取得するコマンドが必要になります。

 

 

 

インスタンスの削除

インスタンスを削除します。

[root@DESKTOP ~]# aws ec2 terminate-instances --instance-ids `aws ec2 describe-instances --filter "Name=tag:Name,Values=Test-EC2-Instance-001"| jq '.Reservations[].Instances[].InstanceId' -r` 

    "TerminatingInstances": [ 
        { 
            "InstanceId": "i-09652b7f7053bb77b", 
            "CurrentState": { 
                "Code": 32, 
                "Name": "shutting-down" 
            }, 
            "PreviousState": { 
                "Code": 16, 
                "Name": "running" 
            } 
        } 
    ] 

[root@DESKTOP ~]#

 

 

 

 

 

Posted by 100%レンタルサーバーを使いこなすサイト管理人

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

AlphaOmega Captcha Medica  –  What Do You See?
     
 

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください