LoginSignup
22
19

More than 5 years have passed since last update.

Node.js + Express をAWS Elastic Beanstalkで動かす

Posted at

以前と設定の仕方が変わったようで、
npm startを設定しないとExpressが起動されなかったので手順をまとめました。
クライアントはMacです。

IAMユーザーの作成

マネジメントコンソールから必要な権限を持ったIAMユーザーを作成し、認証情報を取得。

awscliのインストール

sudo pip install awscli
aws --version
-------
aws-cli/1.11.74 Python/2.7.13 Darwin/16.6.0 botocore/1.5.55
-------

aws configure
------
AWS Access Key ID [None]: ********
AWS Secret Access Key [None]:  ********
Default region name [None]: ap-northeast-1
Default output format [None]: 
------

ebコマンドのインストール

pip install --upgrade --user awsebcli
vi .bash_profile
-----
export PATH=~/Library/Python/2.7/bin:$PATH
-----

eb --version
------
EB CLI 3.10.1 (Python 2.7.1)
------

Express環境の設定

sudo npm install -g express
sudo npm install -g express-generator

Expressプロジェクトの作成

express test-nodejs
cd test-nodejs/
npm install

git初期設定

git init

cat > .gitignore <<EOT 
node_modules/
.gitignore
.elasticbeanstalk/
EOT

Elastic Beanstalkの初期設定/アプリ作成


eb init --platform node.js --region ap-northeast-1
eb create --sample test-nodejs

eb status
------
Environment details for: test-nodejs
  Application name: test-nodejs
  Region: ap-northeast-1
  Deployed Version: app-e465-170525_182949
  Environment ID: e-v7aa4cmhbi
  Platform: arn:aws:elasticbeanstalk:ap-northeast-1::platform/Node.js running on 64bit Amazon Linux/4.1.0
  Tier: WebServer-Standard
  CNAME: test-nodejs.xxxxxxxxx.ap-northeast-1.elasticbeanstalk.com
  Updated: 2017-05-25 09:31:52.871000+00:00
  Status: Ready
  Health: Green

これでElastic Beanstalk側の環境が完了です。

eb open

スクリーンショット 2017-05-25 20.58.26.png

ここからExpressアプリをデプロイしていきます。

elasticbeanstalkでExpressを動かす設定


mkdir .ebextensions

vi .ebextensions/nodecommand.config
------
option_settings:
  aws:elasticbeanstalk:container:nodejs:
    NodeCommand: "npm start"
-------

ちなみに、これをやらないで進めると、Expressが起動せずに502エラーになってしまう。

スクリーンショット 2017-05-25 18.28.22.png

アプリのデプロイ

vi views/index.jade
-----
extends layout

block content
  h1= title
  p Welcome to AWS
------

git add .
git commit -m "First express app"    
eb deploy

eb open

無事expressアプリが表示されました

スクリーンショット 2017-05-25 18.32.15.png

参考

22
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
19