概要
CodePipelineを使用してGitHub + CodeBuild + CodeDeployを使用してオンプレミスな環境にASP.NET Coreアプリを自動ビルド・デプロイする環境を構築していました。
構築の過程でデプロイの際にタイムアウトとなりエラーログも何も出力されない状況となりどん詰まりしたのでその記録です。
※AWSのこのアイコン古いやつなんですね。図を作ってから知りました。
今度から新しいやつで書こうと思います。
目次
環境・前提
環境
- Lightsail
$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
$ aws --version
aws-cli/1.16.121 Python/2.7.5 Linux/3.10.0-957.5.1.el7.x86_64 botocore/1.12.111
$ sudo yum info codedeploy-agent
バージョン : 1.0
リリース : 1.1597
前提
CodePipelineを使用したオンプレミスインスタンスに向けた自動ビルド・デプロイ環境
- ソース:GitHub
- ビルド:CodeBuild(MS公式Dockerイメージを使用)
- デプロイ:CodeDeploy
LightsailもAWSのサービスの一つではあるがCodeDeployと統合されておらずオンプレミスインスタンスと言う扱いでデプロイする必要がある。
CodeDeployにインスタンスタグを紐付け済み。
- CodeDeploy Agentインストールコマンド
# AWS CLIインストール
$ curl -O https://bootstrap.pypa.io/get-pip.py
$ python get-pip.py --user
$ pip install awscli --upgrade --user
$ aws --version
aws-cli/1.16.121 Python/2.7.5 Linux/3.10.0-957.5.1.el7.x86_64 botocore/1.12.111
$ aws configure
AWS Access Key ID :XXXXXXXXXXXXXXXXXXXX
AWS Secret Access Key :XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Default region name :ap-northeast-1
Default output format :json
# CodeDeployインスタンス登録&専用IAMユーザ作成
$ aws deploy register --instance-name <hogehoge>-CentOS-512MB-Tokyo-1 --tags Key=Name,Value=CodeDeploy<hogehoge> --region ap-northeast-1
# 省略
Creating the on-premises instance configuration file named codedeploy.onpremises.yml...DONE
Registering the on-premises instance... DONE
Adding tags to the on-premises instance... DONE
# 省略
# CodeDeployAgentインストール
$ sudo yum install -y ruby wget
$ wget https://aws-codedeploy-ap-northeast-1.s3.amazonaws.com/latest/install
$ chmod +x ./install
$ sudo ./install auto
インストール:
codedeploy-agent.noarch 0:1.0-1.1597
完了しました!
# 実行状態確認
$ sudo service codedeploy-agent status
The AWS CodeDeploy agent is running as PID 9149
事象・原因・対策
事象
CodePipelineを実行し、CodeDeployでのデプロイまで到達するがタイムアウトとなり失敗する。
エラーメッセージには失敗した旨のメッセージしか表示されていない。 また、デプロイEventを確認確認するも全てスキップとなっている。(下記画像を参照)
CodeDeploy Agent側のLogを確認したがそもそもデプロイの命令を受け取れていないようでデプロイに関するログが確認できなかった。
CodeDeploy側は5分でエラーとなっているためタイムアウトによるエラーと断定。
原因
CodeDeploy Agentの設定にデプロイインスタンス登録時のIAMユーザ情報が設定されていなかったため。
CodeDeploy Agentのインストール時に通常下記のコマンドを使用する様に公式ドキュメントで案内されている。
しかし、CentOSではエラーとなり別コマンドでインストールしたためIAMユーザ情報が反映されていなかった。
# 公式ドキュメントのコマンド例(CentOSでは失敗する。)
$ aws deploy install --config-file codedeploy.onpremises.yml
Only Ubuntu Server, Red Hat Enterprise Linux Server and Windows Server operating systems are supported.
# CentOSでインストールした場合
$ wget https://aws-codedeploy-ap-northeast-1.s3.amazonaws.com/latest/install
$ sudo ./install auto
対策
CodeDeployのオンプレミスインスタンス登録時に作成されたcodedeploy.onpremises.yml
を
CodeDeploy Agentの設定ファイルに設定する。
$ sudo vi /etc/codedeploy-agent/conf/codedeployagent.yml
/etc/codedeploy-agent/conf/codedeployagent.yml
:log_aws_wire: false
:log_dir: "/var/log/aws/codedeploy-agent/"
:pid_dir: "/opt/codedeploy-agent/state/.pid/"
:program_name: codedeploy-agent
:root_dir: "/opt/codedeploy-agent/deployment-root"
:verbose: false
:wait_between_runs: 1
:proxy_uri:
:max_revisions: 5
# 下記を追記
:on_premises_config_file: "/home/centos/codedeploy.onpremises.yml"
番外:原因特定の経緯
エラーログが一つも吐かれておらず八方塞がりでしたが
LoggingモードをDEBUGに変更してAgentの再起動を行ったところ下記のエラーログが確認できました。
2019-03-13 02:21:47 INFO [codedeploy-agent(3484)]: master 3484: Spawned child 1/1
2019-03-13 02:21:47 DEBUG [codedeploy-agent(3491)]: Registering Plugins: ["codedeploy"].
2019-03-13 02:21:47 DEBUG [codedeploy-agent(3491)]: Loading plugin codedeploy from /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/register_plugin
2019-03-13 02:21:47 DEBUG [codedeploy-agent(3491)]: Registered Plugins: #<Set: {InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller}>.
2019-03-13 02:21:47 INFO [codedeploy-agent(3491)]: On Premises config file does not exist or not readable
2019-03-13 02:21:47 ERROR [codedeploy-agent(3491)]: booting child: error during start or run: Errno::ENETUNREACH - Network is unreachable - connect(2) - /usr/share/ruby/net/http.rb:878:in `initialize'
//省略
2019-03-13 02:21:47 ERROR [codedeploy-agent(3491)]: booting child: error during start or run: SystemExit - exit - /opt/codedeploy-agent/lib/instance_agent/runner/child.rb:90:in `exit'
エラーログの1行手前(上から4行目)にOn Premises config file does not exist or not readable
と出力されています。
- 英語
On Premises config file does not exist or not readable- Google翻訳
オンプレミスの設定ファイルが存在しないか、読み取り不可能です。
ここで設定ファイルが読み込めていないことがわかりました。
しかしググっても何も出てこないのでもうCodeDeployAgentのソースを確認するぐらいしかないと思い調べます。
調べているとそれっぽいのを見つけました。
$ less /opt/codedeploy-agent/lib/instance_agent/config.rb
/opt/codedeploy-agent/lib/instance_agent/config.rb
なんとなくだけどここらへんが関係してそう。
def initialize
super
@config.update({
:program_name => 'codedeploy-agent',
:wait_between_spawning_children => 1,
:log_dir => nil,
:pid_dir => nil,
:shared_dir => nil,
:user => nil,
:ongoing_deployment_tracking => 'ongoing-deployment',
:children => 1,
:http_read_timeout => 80,
:instance_service_region => nil,
:instance_service_endpoint => nil,
:instance_service_port => nil,
:wait_between_runs => 30,
:wait_after_error => 30,
:codedeploy_test_profile => 'prod',
:kill_agent_max_wait_time_seconds => 7200,
:on_premises_config_file => '/etc/codedeploy-agent/conf/codedeploy.onpremises.yml',
:proxy_uri => nil,
:enable_deployments_log => true,
:use_fips_mode => false
})
end
:on_premises_config_file => '/etc/codedeploy-agent/conf/codedeploy.onpremises.yml',
この部分でconfigファイルを設定していますがフォーマットは/etc/codedeploy-agent/conf/codedeployagent.yml
と同じです。
念の為確認しましたが/etc/codedeploy-agent/conf/codedeploy.onpremises.yml
はもちろん存在していませんでした。
そこで対策で記載した部分を反映するに至りました。
雑感
本当にどん詰まりで2日間ぐらいどん詰まりしてました。
恐らくaws deploy register
コマンドを実行したユーザと先程のコマンドで作成されたユーザが違うから発生したのかな〜とか思いつつ
まあうまいこと自動ビルド・デプロイの環境を手に入れることができたので良しとします。