Simple minds think alike

より多くの可能性を

AWSのEC2 macインスタンスにVNCクライアントから接続してみた

re:Invent 2020の前夜祭でAmazon EC2 Mac Instancesが公開されたということを聞き、iOS用アプリを開発する時とかに便利そうだな〜と思い、試しに触ってみました。最後に使用感に関しても書いてますので、よかったら参考にしてみて下さい!

試すにあたって、以下のAWSドキュメントを参照しました。

aws.amazon.com

2020/12/02現在、利用できるリージョンが限られており、アメリカ東部(バージニア北部)、アメリカ東部(オハイオ)、アメリカ西部(オレゴン)、ヨーロッパ(アイルランド)、アジアパシフィック(シンガポール)で利用可能です。

ゴール

VNCクライアントからEC2 macインスタンスに接続するまでに試した手順を書いてみます。

前提

  • AWS CLI (2.1.6)インストール済
  • aws configureでAccessキー、シークレット設定済
  • EC2ダッシュボードからキーペアを作成済

EC2 macインスタンス作成

専有ホストの割り当て

EC2 macインスタンスを作るには、専有ホストを割り当てる必要があるのですが、米国東部(バージニア北部) us-east-1 リージョンで作ろうとしたところ以下のエラーが発生しました。 Insufficient capacityというエラーなので mac1.metal の専有ホストを作れる最大の数でもあるんでしょうか。

$ aws ec2 allocate-hosts --instance-type mac1.metal \
>   --availability-zone us-east-1a --auto-placement on \
>   --quantity 1 --region us-east-1

An error occurred (InsufficientHostCapacity) when calling the 
AllocateHosts operation (reached max retries: 4): Insufficient capacity

なので、アメリカ東部(オハイオus-east-2 リージョンでインスタンスを作ってみました。しかし、最初オハイオにおいてもAvailability Zoneに us-east-2a を指定した時、以下のエラーが発生しました。

$ aws ec2 allocate-hosts --instance-type mac1.metal   --availability-zone us-east-2a --auto-placement on   --quantity 1 --region us-east-2

An error occurred (UnsupportedHostConfiguration) when calling 
the AllocateHosts operation: 
Your requested instance type (mac1.metal) is not supported in 
your requested Availability Zone (us-east-2a). 
Please retry your request by not specifying an Availability Zone 
or choosing us-east-2b, us-east-2c.

us-east-2a では、macインスタンス用の専有ホストを作れないようなので、以下のように us-east-2b で作りました。

$ aws ec2 allocate-hosts --instance-type mac1.metal   --availability-zone us-east-2b --auto-placement on   --quantity 1 --region us-east-2
{
    "HostIds": [
        "h-03089f5ba21964402"
    ]
}

macインスタンスの起動

無事専有ホストが割当られたので、AWS CLImacインスタンスを起動しようと試みました。が、再度エラー発生。

$ aws ec2 run-instances --region us-east-2 \
  --instance-type mac1.metal \
  --image-id  ami-00692c69a6f9c6ea1 \
  --key-name macos_catalina  --associate-public-ip-address

An error occurred (Unsupported) when calling the
 RunInstances operation: The requested configuration is 
currently not supported. Please check the documentation for supported configurations.

ドキュメント通りのコマンドでもエラーが発生したので、やむなくコンソール上からインスタンスを起動しました。

AMIはMojaveとCatalinaを選択できます。今回は、Catalinaで。

masOSのAMIを選択すると、インスタンスタイプの選択画面でファミリー: mac1、タイプ: mac1.metal が選択できます。以下のように他のAMIで選択できる t2.micro とかにはできませんでした。

割り当てた専有ホストIDを指定して、起動します。

作成済のキーペアを指定して、起動します。

十数分後に、ステータスチェックが合格し、インスタンスが実行中の状態になりました。

セキュリティグループの設定

無事、インスタンスが起動できたので、セキュリティグループを設定し、SSHVNC(5900)でアクセスできるようにします。

リモートからのアクセス(SSH)

では、キーペアを使ってアクセスしてみます。ec2-userでログインできます。

$ssh -i ~/.ssh/macos_catalina.pem ec2-user@ec2-xxx-xxx-xxx-xxx.us-east-2.compute.amazonaws.com


             .:'
         __ :'__       __|  __|_  )
      .'`  `-'  ``.    _|  (     /
     :          .-'   ___|\___|___|
     :         :
      :         `-;   Amazon EC2
       `.__.-.__.'    macOS Catalina 10.15.7

ec2-user@ip-xxx-xxx-xxx-xxx ~ %

接続できるようになりました!

VNC設定

ユーザ作成

VNCから操作するためのユーザを作ります。

以下のようにID1000 (idは任意です)のユーザがいないか確認します。

$ id 1000
id: 1000: no such user

IDが空いていることが確認できたらユーザを作ります。パスワードの設定と管理者権限の付与も行います。

# ユーザ作成
$ sudo dscl . -create /Users/takashi
$ sudo dscl . -create /Users/takashi UserShell /bin/bash
$ sudo dscl . -create /Users/takashi RealName "takashi morita"
$ sudo dscl . -create /Users/takashi UniqueID 1000
$ sudo dscl . -create /Users/takashi PrimaryGroupID 1000
$ sudo dscl . -create /Users/takashi NFSHomeDirectory /Users/takashi

# パスワードの設定と管理者権限付与
$ sudo dscl . -passwd /Users/takashi mynewpassword
$ sudo dscl . -append /Groups/admin GroupMembership takashi

macosの画面共有をONにします。vncpwオプションには上記のユーザ作成時に設定したパスワードを設定します。

$ sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart \
-activate -configure -access -on \
-clientopts -setvnclegacy -vnclegacy yes \
-clientopts -setvncpw -vncpw mynewpassword \
-restart -agent -privs -all

Starting...
Warning: macos 10.14 and later only allows control if Screen Sharing is enabled through System Preferences.
Activated Remote Management.
Stopped ARD Agent.
ec2-user: Set user remote control privileges.
ec2-user: Set user remote access.
takashi: Set user remote control privileges.
takashi: Set user remote access.
Set the client options.
Done.

VPNクライアントから接続

まずは暗号化せずに接続できるか確認

VPNクライアントにパブリック IPv4 DNS、ポートを指定して、作成したユーザ名、パスワードを入力すれば接続できます。

接続後、作ったユーザ、パスワードでログインできます。

暗号化して接続できるか確認

上記の方法だと、暗号化されずに接続しに行くので、業務で使用する際はポートフォワードして接続すると良さそうです。

$ssh -L 5111:localhost:5900 ec2-user@ec2-xxx-xxx-xxx-xxx.us-east-2.compute.amazonaws.com macos_catalina.pem

使用感

ちょっと使い物にならないくらいの遅さだったので、残念な印象でした。

リージョンが米国しか試せなかったのと、家のネットワーク回線の速度があまりよくない、というのが影響していると思います。

今度は、アジアパシフィックでインスタンスを作ったり、他の環境でも確認してみたいと思います! できたら、東京リージョンに来てくれたら嬉しいな。

料金

リージョンによって若干異なるとは思いますが、今回オハイオus-east-2 リージョンで14時間使ったら、$15.16でした。

オンデマンドで数日間だけ利用するか、長期で利用する場合にはSaving Planが良いかもしれません。

参考