LoginSignup
8
5

More than 3 years have passed since last update.

AWS CloudShellで遊んでみる

Posted at

AWS CloudShellが発表されました。AWSコンソールからプリセットされたシェル環境を操作できるようになっています。

基本的な使い方は公式で。

AWSコンソールにアクセスするとヘッダ部分にCloudShellへのアクセスボタンが表示されるようになってます。

スクリーンショット 2020-12-16 8.07.11.png

アクセスしたらこんな案内が表示されました。

スクリーンショット 2020-12-16 8.01.23.png

  • AWS CLI, Python, Node.jsなどがプリセット
  • 1GBストレージを提供
  • homeディレクトリ配下のファイルは保持される

気になったことをいくつか調べたので列挙しておきます。

EC2のメタデータは取得できるのか?

パッと見た感じのコンソールはプライベートIPが振られていたりしたので、メタデータが取得できるか試してみましたが、さすがにだめでした。

Preparing your terminal...
Try these commands to get started:
aws help  or  aws <command> help  or  aws <command> --cli-auto-prompt
[cloudshell-user@ip-10-0-XX-XXX ~]$ 
[cloudshell-user@ip-10-0-XX-XXX ~]$ sudo curl http://169.254.169.254/latest/meta-data/
curl: (7) Couldn't connect to server

※公式ブログに書いてありましたね。内部通信はもう少し先なのか。
Network Access – Sessions can make outbound connections to the Internet, but do not allow any type of inbound connections. Sessions cannot currently connect to resources inside of private VPC subnets, but that’s also on the near-term roadmap.

PythonやNode.jsの環境について

Pythonは2系と3系、両方あるようです。

$ python --version
Python 2.7.18
$ node --version
v12.18.4
$ python3 --version
Python 3.7.9

homeフォルダ以外のデータは残るか?

アクセスしているhomeフォルダはデータが残るようですが、それ以外のフォルダはデータが残るのか、以下の手順で確認。

  1. テキストファイルを作成(/home/cloudshell-user/test.txtと/text_root.txtを作成)
  2. 一度タブをクローズ
  3. 再度CloudShellコンソールを開く
$ ls /home/cloudshell-user/
test.txt
$ ls /
aws  bin  boot  dev  etc  home  lib  lib64  local  media  mnt  opt  proc  root  run  sbin  srv  sys  text_root.txt  tmp  usr  var

この状態で一度ブラウザのタブを閉じて、再アクセスしてみました。

$ ls /home/cloudshell-user/
test.txt
$ ls /
aws  bin  boot  dev  etc  home  lib  lib64  local  media  mnt  opt  proc  root  run  sbin  srv  sys  text_root.txt  tmp  usr  var

ファイルがどちらも残ってますね。ある程度時間を空けたらインスタンスは破棄されるのでしょうか。どこかで確認したいですね。

pip3でpandasをインストールしてみました。sudoすればインストールできるようです。

$ sudo pip3 install pandas
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting pandas
  Downloading https://files.pythonhosted.org/packages/fd/70/e8eee0cbddf926bf51958c7d6a86bc69167c300fa2ba8e592330a2377d1b/pandas-1.1.5-cp37-cp37m-manylinux1_x86_64.whl (9.5MB)
    100% |████████████████████████████████| 9.5MB 126kB/s 
Collecting numpy>=1.15.4 (from pandas)
  Downloading https://files.pythonhosted.org/packages/5e/f2/9e562074f835b9b1227ca156f787be4554ae6bbe293c064337c4153cc4c8/numpy-1.19.4-cp37-cp37m-manylinux1_x86_64.whl (13.4MB)
    100% |████████████████████████████████| 13.4MB 91kB/s 
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/site-packages (from pandas)
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/site-packages (from pandas)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/site-packages (from python-dateutil>=2.7.3->pandas)
Installing collected packages: numpy, pandas
Successfully installed numpy-1.19.4 pandas-1.1.5

$ pip3 freeze | grep pandas
pandas==1.1.5

で、一度タブを閉じて開き直した上で再度pip3のfreezeをしてみたところ、pandasは残っていました。

$ pip3 freeze | grep pandas
pandas==1.1.5

となるとやはり初期化されるタイミングか、あるいは初期化の仕方を知りたいですね。
これを実行しているコンテナごと再生成するようなやり方はないかな。

IAMが違えば環境は違う

当然と言えば当然ですが、異なるIAMユーザーからアクセスしたら異なる環境でした。安心。

Preparing your terminal...
Try these commands to get started:
aws help  or  aws <command> help  or  aws <command> --cli-auto-prompt
[cloudshell-user@ip-10-1-XXX-XX ~]$ 

AWS CLIを実行しているユーザを確認しましたが、作成したIAMでした。

$ aws sts get-caller-identity
{
    "UserId": "ABCDEFGHIJKLMNOPQRSTU",
    "Account": "123456789012",
    "Arn": "arn:aws:iam::123456789012:user/test"
}

ローカル環境に依存しない実行環境

まだまだやれることは制限されていそうですが、とりあえずIAMユーザーを作成すれば「何かパッと試したい」環境としては使えるのではないでしょうか。「PC変えたから実行環境整えなきゃ・・・」という悩みからは解放されそうです。

VPCに対するアクセスなんかがサポートされるようになれば、さらに用途は広がりそうですし、また一つ便利になる機能がリリースされました。

8
5
2

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
8
5