beanstalkでNode.jsを動かしてみた (2) deploy編
前回のエントリでAMIの用意が出来たので、実際にアプリケーションをdeployしてみましょう。
アプリケーション作成
deployするためのサンプルアプリケーションを作成します。
この例ではexpressを使っていますが、app.jsというファイル名で置いた任意のアプリケーションを実行できます。
その場合、待ち受けポートは3000ポートを使用するように気をつけてください。
$ express node create : node create : node/package.json create : node/app.js create : node/public create : node/public/javascripts create : node/public/images create : node/public/stylesheets create : node/public/stylesheets/style.css create : node/routes create : node/routes/index.js create : node/views create : node/views/layout.jade create : node/views/index.jade dont forget to install dependencies: $ cd node && npm install $ cd node/ $ npm install $ vi routes/index.js $ cat routes/index.js /* * GET home page. */ exports.index = function(req, res){ res.render('index', { title: 'Elastic Beanstalk!' }) }; $ node-dev app.js Express server listening on port 3000 in development mode
アプリケーションをhttp://localhost:3000で動作を確認します。
deploy
大丈夫そうなので、gitでdeployしてみます。
初回はPHPアプリケーションとして起動してしまうため、ちょっとおまじない(echo node > index.html)が必要となります。
(これを忘れるとアプリケーションがhealthyでないと判断されdeployに失敗してしまいます)
$ echo node > index.html $ git init . Initialized empty Git repository in /Users/matsui/work/node/.git/ $ git add . $ git commit -m "1st commit" $ AWSDevTools-RepositorySetup.sh $ git aws.config AWS Access Key: ACCESSKEY AWS Secret Key: SECRETKEY AWS Region [default to us-east-1]: ap-northeast-1 AWS Host [default to git.elasticbeanstalk.us-east-1.amazonaws.com]: git.elasticbeanstalk.ap-northeast-1.amazonaws.com AWS Elastic Beanstalk Application: node AWS Elastic Beanstalk Environment: nodetest $ git aws.push Counting objects: 339, done. Delta compression using up to 2 threads. Compressing objects: 100% (300/300), done. Writing objects: 100% (339/339), 246.23 KiB, done. Total 339 (delta 20), reused 0 (delta 0) remote: remote: error: Unable to deploy application version: No Environment found for EnvironmentName = 'nodetest'. remote: To https://ACCESSKEY:SIGNAATURE@git.elasticbeanstalk.ap-northeast-1.amazonaws.com/repos/6e6f6465/nodetest + 484fa97...d5ab5a8 HEAD -> master (forced update)
まだEnvironmentがないのでdeployに失敗しましたが、S3にはあがったので、このバージョンを使って、Environmentを作成します。
Environment作成
Management Consoleで作業を行っていきます。
Beanstalkタブでアプリケーションを選び、Launch New Environmentをクリックします。
のように、先ほどアップロードしたバージョンを選び、コンテナにPHPを選びます。
しばらくすると、Environmentの作成が完了し、アクセスできるようになります。
しかし、AMIがPHP用のものなので、index.htmlを表示するだけの状態となっています。
Environment設定変更(AMIの指定)
Environmentの設定を変更して、Node.js用のAMI(ami-14c87815)を指定します。
Apply Changesを押すとEC2インスタンスの入れ替えが行われます。しばらくすると...
来ました!Node.jsアプリケーションをBeanstalkにdeployする事ができました。
この設定を保存しておけば、次回以降はすぐにこのAMIを使う事ができます。
EnvironmentのActionsから、Save Configurationをクリックします。
最後に
Websocketを使用する際には注意が必要です。それはまた別エントリにて...
Enjoy!