LoginSignup
1
1

More than 3 years have passed since last update.

AWSインスタンスをコマンドラインで起動・停止する

Posted at

AWSのEC2インスタンスをシェルで起動するのをやりたかったのですが、とりあえず今回はコマンドで起動できるところまでを確認しました。ので、備忘録。

AWS-CLIをインストール

参考はこちら

$ curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 20.7M  100 20.7M    0     0  14.6M      0  0:00:01  0:00:01 --:--:-- 14.6M
$ sudo installer -pkg ./AWSCLIV2.pkg -target /
Password:
installer: Package name is AWS Command Line Interface
installer: Installing at base path /
installer: The install was successful.

以下コマンドでインストールできたことを確認。

$ which aws
/usr/local/bin/aws

$ aws --version
aws-cli/2.0.24 Python/3.7.4 Darwin/18.7.0 botocore/2.0.0dev28

起動・停止にトライ

シェルを書く前に、コマンドで試してみると、、、おろ。

$ aws ec2 start-instances --instance-ids インスタンスID 
You must specify a region. You can also configure your region by running "aws configure".

どうやら、aws configureという初期設定が必要な模様。参考はこちら

aws-cliというユーザーを別に作り、ポリシーをアタッチし、生成したアクセスキーを使って以下のように設定。

$ aws configure
AWS Access Key ID [None]: XXXXXXXXXXXXXXX
AWS Secret Access Key [None]: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Default region name [None]: ap-northeast-1
Default output format [None]: json

今度こそ、、、!!

$ aws ec2 start-instances --instance-ids インスタンスID 
{
    "StartingInstances": [
        {
            "CurrentState": {
                "Code": 0,
                "Name": "pending"
            },
            "InstanceId": "XXXXXXXXXXXXXXXXXX",
            "PreviousState": {
                "Code": 80,
                "Name": "stopped"
            }
        }
    ]
}

コンソール見たら、ちゃんとrunnningになっていました。キターーーーーーーーーーーー

停止もできました。コンソールにて、stoppedになっているのを確認。

$ aws ec2 stop-instances --instance-ids インスタンスID
{
    "StoppingInstances": [
        {
            "CurrentState": {
                "Code": 64,
                "Name": "stopping"
            },
            "InstanceId": "XXXXXXXXXXXXXXXXXX",
            "PreviousState": {
                "Code": 16,
                "Name": "running"
            }
        }
    ]
}

今回は、まだシェルを書くに至りませんでした。
次は、シェルを頑張ってみたいと思います。。

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