production.log

株式会社リブセンスでエンジニアをやっている星直史のブログです。

【AWS CLI】OpsWorksAgentのインストーラがAgent installation failed.になる場合の対処方法

概要

AWS 既存のEC2インスタンスをOpsWorksに登録する際、インストーラーを実行するのですが、Agent installation failed.になってしまいハマったので、対処方法を書きます。

環境

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="14.04.5 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.5 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

実行したコマンド

$ aws opsworks register --infrastructure-class ec2 --region ap-northeast-1 --stack-id {{stackid}} --local
...
...
[Wed, 04 Oct 2017 07:35:14 +0000] opsworks-init: Starting the installer
/tmp/opsworks-agent-installer.41mxNiW1JxzOdN0t/opsworks-agent-installer/opsworks-agent/lib/bootstrap/installer.rb:74:in `install_instance_agent': /opt/aws/opsworks/releases/20170402215230_4023-20170402215230 already exists and is current - aborting. (RuntimeError)
    from /tmp/opsworks-agent-installer.41mxNiW1JxzOdN0t/opsworks-agent-installer/opsworks-agent/lib/bootstrap/instance_agent_registration_installer.rb:27:in `block in run'
    from /tmp/opsworks-agent-installer.41mxNiW1JxzOdN0t/opsworks-agent-installer/opsworks-agent/lib/bootstrap/log.rb:96:in `measure'
    from /tmp/opsworks-agent-installer.41mxNiW1JxzOdN0t/opsworks-agent-installer/opsworks-agent/lib/bootstrap/instance_agent_registration_installer.rb:27:in `run'
    from /tmp/opsworks-agent-installer.41mxNiW1JxzOdN0t/opsworks-agent-installer/opsworks-agent/lib/bootstrap/instance_agent_registration_installer.rb:11:in `run'
    from /tmp/opsworks-agent-installer.41mxNiW1JxzOdN0t/opsworks-agent-installer/opsworks-agent/bin/opsworks-agent-registration-installer.rb:8:in `<main>'
[Wed, 04 Oct 2017 07:35:19 +0000] opsworks-init: Agent installation failed.
[Wed, 04 Oct 2017 07:35:19 +0000] opsworks-init: Please verify the log files found under /var/log/aws/opsworks and submit findings to AWS Support.

解決方法

調べてみると、インスタンスに既にOpsWorksAgentが入っていた場合、それを削除しなければならないようでした。

docs.aws.amazon.com

ドキュメントを確認し、下記コマンドを実行し、既存のOpsWorksエージェントを削除。

sudo /etc/init.d/monit stop
sudo /etc/init.d/opsworks-agent stop
sudo rm -rf /etc/aws/opsworks/ /opt/aws/opsworks/ /var/log/aws/opsworks/ /var/lib/aws/opsworks/ /etc/monit.d/opsworks-agent.monitrc /etc/monit/conf.d/opsworks-agent.monitrc /var/lib/cloud/ /var/chef /opt/chef /etc/chef
sudo apt-get -y remove chef
sudo dpkg -r opsworks-agent-ruby

再度インストールコマンドを実行し、無事にsuccessになりました。

$ aws opsworks register --infrastructure-class ec2 --region ap-northeast-1 --stack-id {{stackid}} --local