この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
ども、大瀧です。
開催中のAWSプライベートイベント、re:Inventで続々と新サービスが発表になっています(他のブログエントリーでチェックしましょう)!今回は、CodeDeployという新サービスに触ってみたのでレポートします。
AWS CodeDeployとは
CodeDeployは、その名の通りアプリケーションコードをAWSの仮想マシンサービスであるEC2にデプロイ、管理するサービスです。
従来からElastic Beanstalk、OpsWorksと言ったデプロイ系のサービスがありますが、CodeDeployは管理対象をEC2/AutoScalingのみに特化し、ローテーションやCIツールとの連携などより細やかなデプロイを意識したサービスと受け取れました。また、今後リリースされるAWS CodePipeline、CodeCommitとの連携にも期待したいところですね。費用はEC2インスタンスの利用料のみで、CodeDeploy自体の利用に費用はかかりません。
操作手順
CodeDeployの操作は、AWS Management Consoleから行います。ただ、コンソールのサービス一覧にはまだ載っていないので、製品WebページのGetting Startedにある[Deploy Sample]リンクを踏むのが早いでしょう。ちなみに、画面のリストにはほとんどのリージョンが出るんですが実際に管理画面が表示できたのはバージニア(us-east-1)とオレゴン(us-west-2)でした。順次他のリージョンでも有効化されるでしょう。今回はオレゴンで試してみました。
早速デプロイの種類をサンプルとカスタムから選択します。今回はCodeDeployの様子を探りたかったので[Sample Deployment]を選択しました。
デプロイ対象のEC2インスタンスはDeployment Groupというグループ単位で管理されます。あらかじめCodeDeployエージェントをインストールしなければならないなどDeployment Groupに含めるための事前要件があるので、ウィザードではサンプル用にCloudFormationで一括作成してくれるようになっています。 Amazon LinuxとWindows Serverのどちらかを選択し、インスタンスのタイプ、SSHキーペア、タグを確認・入力、[Launch Instance]ボタンをクリックします。
すると、新しいウィンドウが立ち上がり、CloudFormationのスタック一覧画面が表示されます。先ほど選んだ項目が、このCloudFormationスタックのテンプレートに反映されるというわけですね。
元の画面に戻ると、「スタックのステータスが[CREATE_COMPLETE]になるまで待ちましょう」とあるので、しばらく待ちます。
CloudFormationのスタック作成が完了(CREATE_COMPLETE)したら、[Next Step]をクリックします。
アプリケーションの構成
続いて、デプロイするアプリケーションの構成です。CodeDeployでは複数のアプリケーションを扱うことができるので、今回の最初のアプリケーションの名前を入力します。
アプリケーションのリビジョンを確認します。今回はサンプルがあらかじめS3に用意されており、[Download Sample Bundle]ボタンでダウンロード、中身を確認することができます。
Sample Bundleには以下のファイルが含まれていました。
$ tree SampleApp_Linux
SampleApp_Linux
├── LICENSE.txt
├── appspec.yml
├── index.html
└── scripts
├── install_dependencies
├── start_server
└── stop_server
$
メインの構成ファイルはappspec.ymlで、AppSpecファイルと呼ばれます。デプロイするファイルのほか、デプロイ前後に実行するスクリプト、パーミッションなどを設定できます。詳細はドキュメントのリファレンスを参照してください。
appspec.yml
version: 0.0
os: linux
files:
- source: /index.html
destination: /var/www/html/
hooks:
BeforeInstall:
- location: scripts/install_dependencies
timeout: 300
runas: root
- location: scripts/start_server
timeout: 300
runas: root
ApplicationStop:
- location: scripts/stop_server
timeout: 300
runas: root
今回のサンプルは、シンプルな静的Webコンテンツということがわかりますね。元の画面に戻り、[Next Step]をクリックします。
Deployment Groupの構成
先ほど説明した、Deployment Groupの構成画面が表示されます。まずはグループ名を入力、[Add Instance]には、グループに含むEC2インスタンスを指定します。ウィザードでは、先ほどCloudFormationで作成した3台のEC2インスタンスが既に含まれていますが、別途Nameタグ単位で個別のEC2インスタンスやAutoScalingGroup名単位でAutoScalingのインスタンスをCodeDeployの配下に置くことができます。今回はEC2インスタンス3台のまま、[Next Step]で進みます。
続いて、CodeDeployサービスからデプロイに関するリソースへアクセスするためのロールを選択します。これも先ほどのCloudFormationによって作成されたものがあるので、それを選択します。
ちなみに、ロールの中身は以下のような感じでした。AutoScalingのフックでデプロイプロセスが走る様子が透けて見えますね。
{
"Statement": [
{
"Action": [
"ec2:Describe*"
],
"Effect": "Allow",
"Resource": [
"*"
]
},
{
"Action": [
"autoscaling:CompleteLifecycleAction",
"autoscaling:DeleteLifecycleHook",
"autoscaling:DescribeLifecycleHooks",
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:PutLifecycleHook",
"autoscaling:RecordLifecycleActionHeartbeat"
],
"Effect": "Allow",
"Resource": [
"*"
]
}
]
}
Deploymentの構成
残るは、実際のデプロイ自体の構成です。デプロイ対象のインスタンスに対して、デプロイプロセスを何台ずつ平行して走らせるかを選択します。1台ずつ、半分、全部の3パターンから選択します。自分でデプロイ構成を追加することもできます。
確認と実行
これで設定は完了です。設定内容を確認し、[Deploy Now]で初回のデプロイがスタートします。
Deployment一覧画面に切り替わり、[Status]がCreatedになっていることがわかります。
[Status]がSucceededに変われば、デプロイ完了です。[Instances]という項目が追加され、3台とも完了していることがわかります。
動作確認
では、出来上がった環境を確認してみましょう。CloudFormationによって作成されたEC2インスタンス3台を確認します。
いずれか1台にWebブラウザでHTTP接続してみます。
サンプルのWebページが表示されました!
また、CodeDeployエージェントの様子も確認してみます。EC2インスタンスのUser Dataを確認すると、codedeploy-agentパッケージがインストールされていることがわかります。
#!/bin/bash -ex
yum update -y aws-cfn-bootstrap
yum install -y aws-cli
# Helper function.
function error_exit
{
/opt/aws/bin/cfn-signal -e 1 -r "$1" 'https://cloudformation-waitcondition-us-west-2.s3-us-west-2.amazonaws.com/arn%3Aaws%3Acloudformation%3Aus-west-2%3A602604506680%3Astack/CodeDeploySampleStack-mkb5ryhkt9/c2f2fe10-6a9a-11e4-bb71-50e2414b0a18/WaitHandle
exit 1
}
# Install the AWS CodeDeploy Agent.
cd /home/ec2-user/
aws s3 cp 's3://aws-codedeploy-us-west-2/latest/codedeploy-agent.noarch.rpm' . || error_exit 'Failed to download AWS CodeDeploy Agent.'
yum -y install codedeploy-agent.noarch.rpm || error_exit 'Failed to install AWS CodeDeploy Agent.'
/opt/aws/bin/cfn-init -s arn:aws:cloudformation:us-west-2:602604506680:stack/CodeDeploySampleStack-mkb5ryhkt9/c2f2fe10-6a9a-11e4-bb71-50e2414b0a18 -r LinuxEC2Instance --region us-west-2 || error_exit 'Failed to run cfn-init.'
# All is well, so signal success.
/opt/aws/bin/cfn-signal -e 0 -r "AWS CodeDeploy Agent setup complete." 'https://cloudformation-waitcondition-us-west-2.s3-us-west-2.amazonaws.com/arn%3Aaws%3Acloudformation%3Aus-west-2%3A602604506680%3Astack/CodeDeploySampleStack-mkb5ryhkt9/c2f2fe10-6a9a-11e4-bb71-50e2414b0a18/WaitHandle
SSHでログインし、パッケージの様子を見てみました。
[ec2-user@ip-172-31-XX-XX ~]$ rpm -qi codedeploy-agent
Name : codedeploy-agent
Version : 1.0
Release : 1.643
Architecture: noarch
Install Date: 2014年11月12日 18時40分45秒
Group : Applications/System
Size : 6106031
License : Amazon.com Internal
Signature : (none)
Source RPM : codedeploy-agent-1.0-1.643.src.rpm
Build Date : 2014年11月06日 23時16分16秒
Build Host : sds-tod-workers-2b-i-2c8d5326.us-west-2.amazon.com
Relocations : (not relocatable)
Vendor : Amazon.com
Summary : Provides the required files for CodeDeploy agent to run in EC2 instances
Description :
CodeDeploy instance agent is responsible for doing the actual work of deploying software
on an EC2 instance.
[ec2-user@ip-172-31-XX-XX ~]$ rpm -ql codedeploy-agent
/etc/codedeploy-agent
/etc/codedeploy-agent/conf
/etc/codedeploy-agent/conf/codedeployagent.yml
/etc/rc.d/init.d/codedeploy-agent
/opt/codedeploy-agent
/opt/codedeploy-agent/bin
/opt/codedeploy-agent/bin/codedeploy-agent
/opt/codedeploy-agent/bin/install
/opt/codedeploy-agent/certs
/opt/codedeploy-agent/certs/host-agent-deployment-signer-ca-chain.pem
/opt/codedeploy-agent/lib
/opt/codedeploy-agent/lib/core_ext.rb
/opt/codedeploy-agent/lib/external
/opt/codedeploy-agent/lib/external/README
/opt/codedeploy-agent/lib/instance_agent
/opt/codedeploy-agent/lib/instance_agent.rb
/opt/codedeploy-agent/lib/instance_agent/agent
/opt/codedeploy-agent/lib/instance_agent/agent/base.rb
/opt/codedeploy-agent/lib/instance_agent/codedeploy_plugin
/opt/codedeploy-agent/lib/instance_agent/codedeploy_plugin/application_specification
/opt/codedeploy-agent/lib/instance_agent/codedeploy_plugin/application_specification/ace_info.rb
/opt/codedeploy-agent/lib/instance_agent/codedeploy_plugin/application_specification/acl_info.rb
/opt/codedeploy-agent/lib/instance_agent/codedeploy_plugin/application_specification/application_specification.rb
/opt/codedeploy-agent/lib/instance_agent/codedeploy_plugin/application_specification/context_info.rb
/opt/codedeploy-agent/lib/instance_agent/codedeploy_plugin/application_specification/file_info.rb
/opt/codedeploy-agent/lib/instance_agent/codedeploy_plugin/application_specification/linux_permission_info.rb
/opt/codedeploy-agent/lib/instance_agent/codedeploy_plugin/application_specification/mode_info.rb
/opt/codedeploy-agent/lib/instance_agent/codedeploy_plugin/application_specification/range_info.rb
/opt/codedeploy-agent/lib/instance_agent/codedeploy_plugin/application_specification/script_info.rb
/opt/codedeploy-agent/lib/instance_agent/codedeploy_plugin/codedeploy_control.rb
:
[ec2-user@ip-172-31-XX-XX ~]$
Amazon独自開発の、Rubyで書かれたエージェントのようですね。
まとめ
サンプルでの実行でしたが、CodeDeployの雰囲気を感じていただけたでしょうか?やはり技術的にアツいのはCI連携だと思うので、また試してみてブログにアップしたいと思います!