AWS CodeDeploy にローカルテスト機能が追加されました

2017.11.22

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

ども、藤本です。

現地時間 11/16、CodeDeploy に簡単にデプロイする機能が追加されました。

早速試してみましょう。

概要

CodeDeploy はデプロイを自動化してくれるサービスです。デプロイメントグループでグルーピングしたインスタンス群に対して、マネジメントコンソールや、API からオペレーションするだけでインスタンス群に対してアプリケーションを展開することができます。非常に簡単です。

ただ今までデプロイするには上記のデプロイメントグループを作らなくてはいけない、マネジメントコンソールや、API のオペレーションをしなくてはいけない、つまり AWS の IAM を持ち、CodeDeploy の必要な権限を持つ人にしかデプロイすることができませんでした。本番環境やステージング環境であればそれでいいです。個人の開発環境、個人の開発用インスタンスはどうでしょうか? 各個人環境の CodeDeploy に登録すると、CodeDeploy や IAM の管理が大変です。でも、最新のソースコードや、修正したappspec.ymlや、フックスクリプトをサクッと個人の開発環境で試したい時ありますよね。

そこで今回のアップデートです。

今回のアップデートでは CodeDeploy Agent にテストデプロイ用のコマンドが追加され、デプロイメントグループに登録されていないくても、CodeDeploy の権限を持たなくても CodeDeploy と同じようにデプロイできるようになりました。それにより、CodeDeploy のデプロイ処理に則ったアプリケーションデプロイを個人の環境で簡単に動作確認できます。

試してみた

環境

  • ソースコードリポジトリ:GitHub
  • 個人環境:Amazon Linux on EC2

サンプルアプリケーションの準備

今回はサンプルアプリケーションとして、CodeStar を利用します。CodeStar によるアプリケーションの展開は下記をご参照ください。

[新サービス] 一撃でCI環境を作れる AWS CodeStar

今回選択したテンプレートは Python + Django + EC2 です。

CodeDeploy Agent のインストール・アップグレード

今回のアップデートを試すにはインスタンスに CodeDeploy Agent がインストールされている必要があり、かつ Agent のバージョンが1.0.1.1352以上である必要があります。

[ec2-user@ip-172-31-59-138 ~]$ wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
--2017-11-22 03:42:52--  https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
Resolving aws-codedeploy-us-east-1.s3.amazonaws.com (aws-codedeploy-us-east-1.s3.amazonaws.com)... 54.231.81.72
Connecting to aws-codedeploy-us-east-1.s3.amazonaws.com (aws-codedeploy-us-east-1.s3.amazonaws.com)|54.231.81.72|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 13510 (13K) []
Saving to: ‘install’

install                                           100%[=============================================================================================================>]  13.19K  --.-KB/s    in 0.001s

2017-11-22 03:42:52 (17.1 MB/s) - ‘install’ saved [13510/13510]

[ec2-user@ip-172-31-59-138 ~]$ sudo chmod +x ./install
[ec2-user@ip-172-31-59-138 ~]$ sudo ./install auto
I, [2017-11-22T03:43:03.552501 #2736]  INFO -- : Starting Ruby version check.
I, [2017-11-22T03:43:03.552687 #2736]  INFO -- : Starting update check.
I, [2017-11-22T03:43:03.552759 #2736]  INFO -- : Attempting to automatically detect supported package manager type for system...
I, [2017-11-22T03:43:03.560233 #2736]  INFO -- : Checking AWS_REGION environment variable for region information...
I, [2017-11-22T03:43:03.560286 #2736]  INFO -- : Checking EC2 metadata service for region information...
I, [2017-11-22T03:43:03.627315 #2736]  INFO -- : Downloading version file from bucket aws-codedeploy-us-east-1 and key latest/VERSION...
I, [2017-11-22T03:43:03.745679 #2736]  INFO -- : Downloading version file from bucket aws-codedeploy-us-east-1 and key latest/VERSION...
I, [2017-11-22T03:43:03.769787 #2736]  INFO -- : Downloading package from bucket aws-codedeploy-us-east-1 and key releases/codedeploy-agent-1.0-1.1352.noarch.rpm...
I, [2017-11-22T03:43:04.068068 #2736]  INFO -- : Executing `/usr/bin/yum -y localinstall /tmp/codedeploy-agent-1.0-1.1352.noarch.tmp-20171122-2736-any7kd.rpm`...
Loaded plugins: priorities, update-motd, upgrade-helper
Examining /tmp/codedeploy-agent-1.0-1.1352.noarch.tmp-20171122-2736-any7kd.rpm: codedeploy-agent-1.0-1.1352.noarch
Marking /tmp/codedeploy-agent-1.0-1.1352.noarch.tmp-20171122-2736-any7kd.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package codedeploy-agent.noarch 0:1.0-1.1352 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================================================
 Package                                  Arch                           Version                             Repository                                                                            Size
========================================================================================================================================================================================================
Installing:
 codedeploy-agent                         noarch                         1.0-1.1352                          /codedeploy-agent-1.0-1.1352.noarch.tmp-20171122-2736-any7kd                          16 M

Transaction Summary
========================================================================================================================================================================================================
Install  1 Package

Total size: 16 M
Installed size: 16 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction

pre hook : 1
Checking the ruby version.
Checking if there is already a process named codedeploy-agent running.
  Installing : codedeploy-agent-1.0-1.1352.noarch                                                                                                                                                   1/1

post hook : 1
Check if there is a codedeployagent config file.
Start codedeploy-agent in post hook if this is an install or update.
Installing codedeploy-agent auto-update cron in '/etc/cron.d/codedeploy-agent-update'...
Installing codedeploy-agent auto-update cron in '/etc/cron.d/codedeploy-agent-update'...Complete
  Verifying  : codedeploy-agent-1.0-1.1352.noarch                                                                                                                                                   1/1

Installed:
  codedeploy-agent.noarch 0:1.0-1.1352

Complete!
I, [2017-11-22T03:43:06.159643 #2736]  INFO -- : Update check complete.
I, [2017-11-22T03:43:06.159739 #2736]  INFO -- : Stopping updater.
[ec2-user@ip-172-31-59-138 ~]$ sudo service codedeploy-agent status
The AWS CodeDeploy agent is running as PID 2782

ローカルデプロイ

それでは CodeDeploy の Deployment Group に登録していない EC2 で GitHub にあるリポジトリからアプリケーションを展開してみましょう。

[ec2-user@ip-172-31-59-138 ~]$ sudo /opt/codedeploy-agent/bin/codedeploy-local -l https://github.com/s-fujimoto/djangostar
Starting to execute deployment from within folder /opt/codedeploy-agent/deployment-root/default-local-deployment-group/d-QJRQT90S5-local
See the deployment log at /opt/codedeploy-agent/deployment-root/default-local-deployment-group/d-QJRQT90S5-local/logs/scripts.log for more details
AppSpec file valid. Local deployment successful

※ 2017/11/22 時点でインストールされる CodeDeploy ではエラーになりました。エラー内容と修正内容はブログ最下部をご覧ください。

デプロイに成功しました。

Web ブラウザから EC2 インスタンスにアクセスしてみます。

うん、CodeStar で展開されるページが返ってきました。

まとめ

いかがでしたでしょうか? CodeDeploy でデプロイ管理しているアプリケーションを任意のインスタンスに簡単に展開することができました。条件は CodeDeploy Agent がインストールされていることです。個人の環境でぜひ利用しましょう。

エラーが出たら

たぶん、初歩的なエラーなので今後修正されるかと思いますが、、、

エラーメッセージ
[ec2-user@ip-172-31-59-138 ~]$ sudo /opt/codedeploy-agent/bin/codedeploy-local -l https://github.com/s-fujimoto/djangostar
Starting to execute deployment from within folder /opt/codedeploy-agent/deployment-root/default-local-deployment-group/d-0RXQYXXQ2-local
See the deployment log at /opt/codedeploy-agent/deployment-root/default-local-deployment-group/d-0RXQYXXQ2-local/logs/scripts.log for more details
/opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/application_specification/application_specification.rb:93:in `block in parse_permissions': uninitialized constant InstanceAgent::Plugins::CodeDeployPlugin::ApplicationSpecification::LinuxPermissionInfo (NameError)
	from /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/application_specification/application_specification.rb:88:in `each'
	from /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/application_specification/application_specification.rb:88:in `parse_permissions'
	from /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/application_specification/application_specification.rb:22:in `initialize'
	from /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/application_specification/application_specification.rb:26:in `new'
	from /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/application_specification/application_specification.rb:26:in `parse'
	from /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:220:in `parse_app_spec'
	from /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/hook_executor.rb:100:in `initialize'
	from /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:147:in `new'
	from /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:147:in `block (3 levels) in map'
	from /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:146:in `each'
	from /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:146:in `block (2 levels) in map'
	from /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/command_executor.rb:68:in `execute_command'
	from /opt/codedeploy-agent/lib/aws/codedeploy/local/deployer.rb:85:in `block in execute_events'
	from /opt/codedeploy-agent/lib/aws/codedeploy/local/deployer.rb:84:in `each'
	from /opt/codedeploy-agent/lib/aws/codedeploy/local/deployer.rb:84:in `execute_events'
	from /opt/codedeploy-agent/bin/codedeploy-local:117:in `<main>'
修正内容
[ec2-user@ip-172-31-59-138 ~]$ sudo vi /opt/codedeploy-agent/lib/instance_agent/plugins/codedeploy/application_specification/application_specification.rb
<下記2行を先頭に追記>
require 'instance_agent/plugins/codedeploy/application_specification/linux_permission_info'
require 'instance_agent/plugins/codedeploy/application_specification/mode_info'