OpsworksにEC2を登録できない問題への対応

## 事象
インスタンス登録コマンドを発行すると途中で接続が切れ登録が失敗する。

```
$ aws opsworks register --infrastructure-class ec2 --region us-east-1 --stack-id <STACK_ID> --ssh-username ec2-user --ssh-private-key <PRIVATE_KEY> <INSTANCE_ID>
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 7929k  100 7929k    0     0  47.5M      0 --:--:-- --:--:-- --:--:-- 47.5M
[Thu, 19 Sep 2019 08:14:44 +0000] opsworks-init: Initializing AWS OpsWorks environment
[Thu, 19 Sep 2019 08:14:44 +0000] opsworks-init: Checking if OS is supported
[Thu, 19 Sep 2019 08:14:44 +0000] opsworks-init: Running on supported OS
[Thu, 19 Sep 2019 08:14:44 +0000] opsworks-init: Setup motd
[Thu, 19 Sep 2019 08:14:44 +0000] opsworks-init: Executing: ln -sf --backup /etc/motd.opsworks-static /etc/motd
[Thu, 19 Sep 2019 08:14:44 +0000] opsworks-init: Using assets bucket from environment: 'opsworks-instance-assets-ap-northeast-1.s3.amazonaws.com'.
[Thu, 19 Sep 2019 08:14:44 +0000] opsworks-init: Installing Ruby for the agent
[Thu, 19 Sep 2019 08:14:44 +0000] opsworks-init: Executing: /tmp/opsworks-agent-installer.XbghhcTVDCDTtrGW/opsworks-agent-installer/opsworks-agent/bin/installer_wrapper.sh -r -R opsworks-instance-assets-ap-northeast-1.s3.amazonaws.com
Connection to <Elastic IP> closed.
```

## 調査

登録対象のEC2インスタンス側でログを確認する。

```
[root@ip-172-30-1-37 ec2-user]# tail -f /var/log/aws/opsworks/user-data.log
[Thu, 19 Sep 2019 08:14:44 +0000] opsworks-init: Checking if OS is supported
/tmp/opsworks-agent-installer.XbghhcTVDCDTtrGW/opsworks-agent-installer/boot-registration: line 48: lsb_release: command not found
[Thu, 19 Sep 2019 08:14:44 +0000] opsworks-init: Running on supported OS
[Thu, 19 Sep 2019 08:14:44 +0000] opsworks-init: Setup motd
[Thu, 19 Sep 2019 08:14:44 +0000] opsworks-init: Executing: ln -sf --backup /etc/motd.opsworks-static /etc/motd
[Thu, 19 Sep 2019 08:14:44 +0000] opsworks-init: Using assets bucket from environment: 'opsworks-instance-assets-ap-northeast-1.s3.amazonaws.com'.
[Thu, 19 Sep 2019 08:14:44 +0000] opsworks-init: Installing Ruby for the agent
[Thu, 19 Sep 2019 08:14:44 +0000] opsworks-init: Executing: /tmp/opsworks-agent-installer.XbghhcTVDCDTtrGW/opsworks-agent-installer/opsworks-agent/bin/installer_wrapper.sh -r -R opsworks-instance-assets-ap-northeast-1.s3.amazonaws.com
[Thu, 19 Sep 2019 08:14:44 +0000] installer-wrapper: Using opsworks-instance-assets-ap-northeast-1.s3.amazonaws.com for assets.
[Thu, 19 Sep 2019 08:14:44 +0000] installer-wrapper: Unsupported OS version: amazon 2018.03
```

OS versionが未サポートと言われるが、そんなはずはない。（https://aws.amazon.com/jp/about-aws/whats-new/2019/01/aws-opsworks-stacks-now-supports-amazon-linux-2--amazon-linux-20/）

原因が分からないのでググると以下のフォーラムにたどり着く。

https://forums.aws.amazon.com/thread.jspa?threadID=250571

Opsworks Agentが古いのでは？というコメントが見つかる。

## 対応

以下の設定を確認すると2016年付近のAgentを利用していたので、最新のバージョンを利用するように変更。

Stack Settings > OpsWorks Agent version ---> Latest version

再度登録コマンド発行。

```
$ aws opsworks register --infrastructure-class ec2 --region us-east-1 --stack-id <STACK_ID> --ssh-username ec2-user --ssh-private-key <PRIVATE_KEY> <INSTANCE_ID>
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 7933k  100 7933k    0     0  44.0M      0 --:--:-- --:--:-- --:--:-- 44.0M
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Initializing AWS OpsWorks environment
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Checking if OS is supported
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Running on supported OS
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Setup motd
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Executing: ln -sf --backup /etc/motd.opsworks-static /etc/motd
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Using assets bucket from environment: 'opsworks-instance-assets-ap-northeast-1.s3.amazonaws.com'.
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Installing Ruby for the agent
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Executing: /tmp/opsworks-agent-installer.YmkVCDV5L5hS1Ok3/opsworks-agent-installer/opsworks-agent/bin/installer_wrapper.sh -r -R opsworks-instance-assets-ap-northeast-1.s3.amazonaws.com
[Thu, 19 Sep 2019 08:22:23 +0000] opsworks-init: Starting the installer
Instance successfully registered. Instance ID: <Instance ID>
Connection to <Elastic IP> closed.
```

登録できた。

参考までにEC2側のログは以下の通り。

```
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Initializing AWS OpsWorks environment
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Checking if OS is supported
/tmp/opsworks-agent-installer.YmkVCDV5L5hS1Ok3/opsworks-agent-installer/boot-registration: line 47: lsb_release: command not found
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Running on supported OS
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Setup motd
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Executing: ln -sf --backup /etc/motd.opsworks-static /etc/motd
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Using assets bucket from environment: 'opsworks-instance-assets-ap-northeast-1.s3.amazonaws.com'.
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Installing Ruby for the agent
[Thu, 19 Sep 2019 08:22:21 +0000] opsworks-init: Executing: /tmp/opsworks-agent-installer.YmkVCDV5L5hS1Ok3/opsworks-agent-installer/opsworks-agent/bin/installer_wrapper.sh -r -R opsworks-instance-assets-ap-northeast-1.s3.amazonaws.com
[Thu, 19 Sep 2019 08:22:21 +0000] installer-wrapper: Using opsworks-instance-assets-ap-northeast-1.s3.amazonaws.com for assets.
[Thu, 19 Sep 2019 08:22:21 +0000] installer-wrapper: Downloading agent ruby package from https://opsworks-instance-assets-ap-northeast-1.s3.amazonaws.com/packages/amazon/2018.03/opsworks-agent-ruby-2.2.3-1.x86_64.rpm
[Thu, 19 Sep 2019 08:22:21 +0000] downloader: Successfully created temporary download directory. (/tmp/opsworks-downloader.I6n5yywV)
[Thu, 19 Sep 2019 08:22:21 +0000] downloader: File size test passed.
[Thu, 19 Sep 2019 08:22:21 +0000] downloader: Checksum proof skipped.
[Thu, 19 Sep 2019 08:22:21 +0000] downloader: Successfully downloaded https://opsworks-instance-assets-ap-northeast-1.s3.amazonaws.com/packages/amazon/2018.03/opsworks-agent-ruby-2.2.3-1.x86_64.rpm
[Thu, 19 Sep 2019 08:22:21 +0000] installer-wrapper: Installing agent ruby
Preparing...                          ########################################
Updating / installing...
opsworks-agent-ruby-2.2.3-1           ########################################
[Thu, 19 Sep 2019 08:22:23 +0000] installer-wrapper: Cleaning up
[Thu, 19 Sep 2019 08:22:23 +0000] opsworks-init: Starting the installer
```

## まとめ

https://docs.aws.amazon.com/ja_jp/opsworks/latest/userguide/registered-instances-register.html

登録フローの中でAgent Packageをダウンロードする部分があるが、そのダウンロードするパッケージのバージョンが古いため、登録コマンド（ローカル）のバージョンと不整合を起こしていたと思われる。
ログからは全く読めなかったので結構ハマってしまった。