AWS CodeDeploy にローカルテスト機能が追加されました
ども、藤本です。
現地時間 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 によるアプリケーションの展開は下記をご参照ください。
今回選択したテンプレートは Python + Django + EC2 です。
CodeDeploy Agent のインストール・アップグレード
今回のアップデートを試すにはインスタンスに CodeDeploy Agent がインストールされている必要があり、かつ Agent のバージョンが1.0.1.1352以上である必要があります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | [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/installResolving aws-codedeploy-us-east-1.s3.amazonaws.com (aws-codedeploy-us-east-1.s3.amazonaws.com)... 54.231.81.72Connecting 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 OKLength: 13510 (13K) []Saving to: ‘install’install 100%[=============================================================================================================>] 13.19K --.-KB/s in 0.001s2017-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 autoI, [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-helperExamining /tmp/codedeploy-agent-1.0-1.1352.noarch.tmp-20171122-2736-any7kd.rpm: codedeploy-agent-1.0-1.1352.noarchMarking /tmp/codedeploy-agent-1.0-1.1352.noarch.tmp-20171122-2736-any7kd.rpm to be installedResolving Dependencies--> Running transaction check---> Package codedeploy-agent.noarch 0:1.0-1.1352 will be installed--> Finished Dependency ResolutionDependencies 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 MTransaction Summary========================================================================================================================================================================================================Install 1 PackageTotal size: 16 MInstalled size: 16 MDownloading packages:Running transaction checkRunning transaction testTransaction test succeededRunning transactionpre hook : 1Checking the ruby version.Checking if there is already a process named codedeploy-agent running. Installing : codedeploy-agent-1.0-1.1352.noarch 1/1post hook : 1Check 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/1Installed: codedeploy-agent.noarch 0:1.0-1.1352Complete!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 statusThe AWS CodeDeploy agent is running as PID 2782 |
ローカルデプロイ
それでは CodeDeploy の Deployment Group に登録していない EC2 で GitHub にあるリポジトリからアプリケーションを展開してみましょう。
1 2 3 4 | [ec2-user@ip-172-31-59-138 ~]$ sudo /opt/codedeploy-agent/bin/codedeploy-local -l https://github.com/s-fujimoto/djangostarStarting to execute deployment from within folder /opt/codedeploy-agent/deployment-root/default-local-deployment-group/d-QJRQT90S5-localSee the deployment log at /opt/codedeploy-agent/deployment-root/default-local-deployment-group/d-QJRQT90S5-local/logs/scripts.log for more detailsAppSpec file valid. Local deployment successful |
※ 2017/11/22 時点でインストールされる CodeDeploy ではエラーになりました。エラー内容と修正内容はブログ最下部をご覧ください。
デプロイに成功しました。
Web ブラウザから EC2 インスタンスにアクセスしてみます。
うん、CodeStar で展開されるページが返ってきました。
まとめ
いかがでしたでしょうか? CodeDeploy でデプロイ管理しているアプリケーションを任意のインスタンスに簡単に展開することができました。条件は CodeDeploy Agent がインストールされていることです。個人の環境でぜひ利用しましょう。
エラーが出たら
たぶん、初歩的なエラーなので今後修正されるかと思いますが、、、
エラーメッセージ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [ec2-user@ip-172-31-59-138 ~]$ sudo /opt/codedeploy-agent/bin/codedeploy-local -l https://github.com/s-fujimoto/djangostarStarting to execute deployment from within folder /opt/codedeploy-agent/deployment-root/default-local-deployment-group/d-0RXQYXXQ2-localSee 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>' |
修正内容
1 2 3 4 | [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' |