LoginSignup
1
1

More than 3 years have passed since last update.

AWS cliのコマンドメモ

Posted at

記事の趣旨

AWS CLIのコマンドをメモしていく.必要が生じたときに調べて埋めていくので内容は偏ります.

1. AWS CLIとは

コマンドライン上でAWS操作を可能とするツール.インストールとかの話は以前の記事のAWS CLIの項目を参照.以下に書き並べるリファレンスはこちらを参照してコピペしている.

2. 基本操作

認証情報などの設定

aws configure

この設定情報はwindowsの場合C:\Users\USERNAME\.aws\configというファイルに保存される.アクセスキーはIAMサービスからIAMユーザを選択,「認証情報」タグ内の「アクセスキーの作成」ボタンから作成可能.

$ aws configure --profile myprofile //myprofileというプロファイル名で新規作成.存在する場合は更新される.
AWS Access Key ID [None]: //内緒
AWS Secret Access Key [None]: //内緒
Default region name [None]: ap-northeast-1
Default output format [None]: json

以後,各コマンドに--profile myprofileというオプションを付与することで,この認証情報を用いて各種操作を実行できるようになる.

3. EC2関連のコマンド

インスタンスの一覧の表示

aws ec2 describe-instances

$ aws ec2 describe-instances --profile myprofile
{
    "Reservations": [
      //めっちゃ長いので省略
    ]
}

--flitersオプションに続けて使うことで対象を絞り込める.keyの種類はこちらを参照.

$ aws ec2 describe-instances --filters Name=instance-id,Values=tekitou --profile myprofile  //存在しないidを指定してみる
{
    "Reservations": []  //何も出てこない.
}

--queryを使って表示項目を絞り込むとわかりやすい.

$ aws ec2 describe-instances \
--query "Reservations[*].Instances[*].{NAMAE: InstanceId, BASHO: PublicIpAddress, JOTAI: State.Name}" \
--profile myprofile
[
    [
        {
            "NAMAE": //内緒,
            "BASHO": //xxx.xxx.xxx.xxx",
            "JOTAI": "running"
        }
    ]
]

インスタンスの停止・起動

  • stop-instances
  • start-instances
$ aws ec2 stop-instances --instance-ids //内緒 --profile myprofile
{
    "StoppingInstances": [
        {
            "CurrentState": {
                "Code": 64,
                "Name": "stopping"
            },
            "InstanceId": //内緒,
            "PreviousState": {
                "Code": 16,
                "Name": "running"
            }
        }
    ]
}

$ aws ec2 describe-instances \
--query "Reservations[*].Instances[*].{NAMAE: InstanceId, BASHO: PublicIpAddress, JOTAI: State.Name}" \
--profile myprofile //どうなったか確認してみる
[
    [
        {
            "NAMAE": //内緒,
            "BASHO": //xxx.xxx.xxx.xxx",
            "JOTAI": "stopped" //停止している
        }
    ]
]

$ aws ec2 start-instances --instance-ids //内緒 --profile myprofile //再度起動してみる
{
    "StartingInstances": [
        {
            "CurrentState": {
                "Code": 0,
                "Name": "pending"
            },
            "InstanceId": //内緒,
            "PreviousState": {
                "Code": 80,
                "Name": "stopped"
            }
        }
    ]
}

$ aws ec2 describe-instances \
--query "Reservations[*].Instances[*].{NAMAE: InstanceId, BASHO: PublicIpAddress, JOTAI: State.Name}" \
--profile myprofile //どうなったか確認してみる
[
    [
        {
            "NAMAE": //内緒,
            "BASHO": xxx.xxx.xxx.xxx,
            "JOTAI": "running" //起動してくれた
        }
    ]

4. VPC関連のコマンド

セキュリティグループのルールを追加・削除

  • authorize-security-group-ingress
  • revoke-security-group-ingress

--group-idもしくは--group-nameというオプションを用いることで対象グループを指定.設定情報は--protocol--port--cidr--source-groupなどのオプションで指定可能.

$ aws ec2 authorize-security-group-ingress \
--group-id //内緒 \
--port 22 \
--cidr xxx.xxx.xxx.xxx/32 \
--protocol tcp \
--profile myprofile

// なんの反応もないが成功はしている,対象にsshできるようになっている

$ aws ec2 revoke-security-group-ingress \
--group-id //内緒 \
--port 22 \
--cidr xxx.xxx.xxx.xxx/32 \
--protocol tcp \
--profile myprofile

// こちらも同様,sshを試みるとタイムアウトした
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1