Study Notes - Deploy Node.js App using EB CLI


前一篇 整理如何利用 Elastic Beanstalk 架設 Wordpress 的筆記,這篇整理如何利用 EB CLI 部署 Node.js Application。

部署 Node.js Application 步驟

  1. 準備 IAM Roles
  2. 安裝 EB CLI
  3. 建立 node.js application - express
  4. 建立 EB Environment: EC2-Classic or VPC

準備 IAM Roles

基本的 EB Application 需要兩個 Roles:EB Service Role, EC2 Role,EB 會幫忙自動建立,熟悉之後,建議自行規劃。

其他 VPC, Subnet, Security Group, EC2 KeyPair 請參考 前一篇 整理。

安裝與設定 EB CLI

安裝: pip install --upgrade --user awsebcli

  • EB CLI 同樣讀取 AWS CLI 的 Profile,所以只要確認好 Profile 有 IAM Policy 就可以了。
  • EB CLI 會依照底下次序使用 AWS Credentials.
    1. CLI options: --profile
    2. 環境變數: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
    3. AWS credential file: ~/.aws/credentials,使用 default profile
    4. AWS CLI profile:指定 profile name
    5. 舊的 EB CLI config,放在 ~/.elasticbeanstalk/config
    6. 詳細說明:Configuration Settings and Precedence

我習慣統一使用 export AWS_DEFAULT_PROFILE, export AWS_DEFAULT_REGION 的方式切換不同的 AWS Account, Region 的 Profile.

建立 node.js application - express

用 express 建立一個 Node Application,步驟如下:

1
2
3
4
5
6
7
8
9
mkdir NodeApp
cd NodeApp
# 初始化 Express Application
express
npm install
# 啟動 application, browse http://localhost:3000
npm start

這邊用 node.js 6.2.2, express 4.15.

express installation

Init EB Application

透過 eb init 初始 EB Application,過程會確認 region, application name, platform (language), EC2 KeyPair.

這邊例子的 Application 叫做:NodeApp

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
~  eb init
Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)
6) ap-south-1 : Asia Pacific (Mumbai)
7) ap-southeast-1 : Asia Pacific (Singapore)
8) ap-southeast-2 : Asia Pacific (Sydney)
9) ap-northeast-1 : Asia Pacific (Tokyo)
10) ap-northeast-2 : Asia Pacific (Seoul)
11) sa-east-1 : South America (Sao Paulo)
12) cn-north-1 : China (Beijing)
13) us-east-2 : US East (Ohio)
14) ca-central-1 : Canada (Central)
15) eu-west-2 : EU (London)
(default is 3): 1
Select an application to use
1) GTCafe-Pay
2) [ Create new Application ]
(default is 2): 2
Enter Application Name
(default is "NodeApp"):
Application NodeApp has been created.
It appears you are using Node.js. Is this correct?
(Y/n): y
Cannot setup CodeCommit because there is no Source Control setup, continuing with initialization
Do you want to set up SSH for your instances?
(Y/n): y
Select a keypair.
1) virginia.lab
2) [ Create new KeyPair ]
(default is 1): 1

初始化之後,會在應用程式新增一個檔案: .elasticbeanstalk/config.yml,內容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
branch-defaults:
default:
environment: null
group_suffix: null
global:
application_name: NodeApp
branch: null
default_ec2_keyname: virginia.lab
default_platform: Node.js
default_region: us-east-1
include_git_submodules: true
instance_profile: null
platform_name: null
platform_version: null
profile: eb-cli
repository: null
sc: null
workspace_type: Application

設定 option_settings

新增一個檔案: .ebextensions/options.config,內容如下:

1
2
3
option_settings:
aws:elasticbeanstalk:container:nodejs:
NodeCommand: "npm start"

指定如何啟動 application,更多 options for node.js 參考 Node.js Platform Options

建立 EB 環境 - EC2-Classic

使用 eb create 建立 EB Environment,第一種做法直接建立在 non-vpc,也就是 EC2-Classic 的做法,他會開建立以下資源:

  • Clasic EC2: t1.micro, EIP
  • ELB (CLB or ALB)
  • Auto Scaling Group, Launch Configuration
  • CloudWatch Alarms
  • Application image in S3

建立歷程如下:

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
eb create
Enter Environment Name
(default is NodeApp-dev): ec2-classic
Enter DNS CNAME prefix
(default is ec2-classic):
Select a load balancer type
1) classic
2) application
(default is 1): 1
Creating application version archive "app-170722_122611".
Uploading NodeApp/app-170722_122611.zip to S3. This may take a while.
Upload Complete.
Environment details for: ec2-classic
Application name: NodeApp
Region: us-east-1
Deployed Version: app-170722_122611
Environment ID: e-pwhi9zpuun
Platform: arn:aws:elasticbeanstalk:us-east-1::platform/Node.js running on 64bit Amazon Linux/4.2.0
Tier: WebServer-Standard
CNAME: ec2-classic.us-east-1.elasticbeanstalk.com
Updated: 2017-07-22 04:26:31.635000+00:00
Printing Status:
INFO: createEnvironment is starting.
... 略 ...
INFO: Environment health has transitioned from Pending to Ok. Initialization completed 14 seconds ago and took 3 minutes.
INFO: Successfully launched environment: ec2-classic

順利的話就可以點選 URL 看到 express 的頁面。

建立 EB 環境 - EC2 in VPC

實務上會建議把 EC2 建立在 VPC 裡面,以下是我的建立設定:

  • ELB 放在 Public Subnet
  • EC2 放在 Public Subnet with EIP for Testing (因為沒有 NAT Instance)
  • 指定 EC2 IAM Role, EB Service Role
  • 指定 EC2 Instance Type using t2.nano (省錢)
  • 指定 EC2 的 SG
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
eb create Dev1-VPC \
--vpc.id vpc-abcdefg \
--vpc.elbsubnets subnet-1234567,subnet-7654321 \
--vpc.securitygroup sg-123456789 \
--vpc.ec2subnets subnet-1234567,subnet-7654321 \
--vpc.elbpublic \
--vpc.publicip \
--elb-type application \
--instance_profile EB_EC2-Role \
--instance_type t2.nano \
--keyname virginia.lab \
--platform node.js \
--region us-east-1 \
--scale 1 \
--service-role EB-Service-Role

如果需要帶環境變數給 application,像是連 RDS,可以使用: --envvars RDS_DB_NAME=wordpress,RDS_HOSTNAME=<random-dbname>.us-east-1.rds.amazonaws.com,RDS_PASSWORD=<password> 帶相關的變數

完整的 options 說明參考:eb create

EB 狀態

eb status 取得環境的狀態,可以指定 env name,如下:

1
2
3
4
5
6
7
8
9
10
11
12
~  eb status NodeApp-Dev1
Environment details for: NodeApp-Dev1
Application name: NodeApp
Region: us-east-1
Deployed Version: app-170722_124332
Environment ID: e-jcpmzfa7ak
Platform: arn:aws:elasticbeanstalk:us-east-1::platform/Node.js running on 64bit Amazon Linux/4.2.0
Tier: WebServer-Standard
CNAME: NodeApp-Dev1.bxut2pspqv.us-east-1.elasticbeanstalk.com
Updated: 2017-07-22 07:52:50.299000+00:00
Status: Terminating
Health: Red

eb config

1
2
eb config save NodeApp-Dev1 --cfg NodeApp-Dev1
Workspace/NodeApp/.elasticbeanstalk/saved_configs/NodeApp-Dev1.cfg.yml

使用 config 重建新環境:

1
eb create --cfg NodeApp-Dev1

Questions

哪些常用的 EB CLI?

整理以下:

  • Lifecycle: init, create, config, deploy, use, clone, terminate
  • Message: status, health, events, logs
  • infomration: appversion, list, platform, printenv

要知道有哪一些 node.js 版本:eb platform list

底下是一個使用情境:

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
# 找到有哪一些環境
~  eb list
GTCafe-Pay-Dev
* NodeApp-Dev1
# 切換不同的環境:
~  eb use NodeApp-Dev1
# 複製環境
~  eb clone
Enter name for Environment Clone
(default is NodeApp-Dev1-clone): NodeApp-Dev2
Enter DNS CNAME prefix
(default is NodeApp-Dev2):
Environment details for: NodeApp-Dev2
Application name: NodeApp
Region: us-east-1
Deployed Version: app-170722_161138
Environment ID: e-bmnvy3e2gs
Platform: arn:aws:elasticbeanstalk:us-east-1::platform/Node.js running on 64bit Amazon Linux/4.2.0
Tier: WebServer-Standard
CNAME: NodeApp-Dev2.us-east-1.elasticbeanstalk.com
Updated: 2017-07-22 08:33:13.793000+00:00
... 略 ...
INFO: Successfully launched environment: NodeApp-Dev2
# 切換環境
~  eb use NodeApp-Dev2
# 顯示環境狀態
~  eb status
Environment details for: NodeApp-Dev2
Application name: NodeApp
Region: us-east-1
Deployed Version: app-170722_161138
Environment ID: e-bmnvy3e2gs
Platform: arn:aws:elasticbeanstalk:us-east-1::platform/Node.js running on 64bit Amazon Linux/4.2.0
Tier: WebServer-Standard
CNAME: NodeApp-Dev2.us-east-1.elasticbeanstalk.com
Updated: 2017-07-22 08:37:21.351000+00:00
Status: Ready
Health: Green
~  eb health
~  eb logs
~  eb terminate
The environment "NodeApp-Dev2" and all associated instances will be terminated.
To confirm, type the environment name: NodeApp-Dev2
INFO: terminateEnvironment is starting.
INFO: Deleted CloudWatch alarm named: awseb-e-bmnvy3e2gs-stack-AWSEBCloudwatchAlarmHigh-HKSCZ06VXFVF
INFO: Deleted CloudWatch alarm named: awseb-e-bmnvy3e2gs-stack-AWSEBCloudwatchAlarmLow-Y7CLMSR42ARI
... 略 ...
INFO: Deleting SNS topic for environment NodeApp-Dev2.
INFO: terminateEnvironment completed successfully.

options.config 有哪些參數可以設定?

參考 Node.js Platform Options 的說明,有完整 options 的使用清單。

Namespace: aws:elasticbeanstalk:container:nodejs 最常用的如下:

  • NodeCommand: 啟動 Node App 的字,像是 app.js, npm start
  • NodeVersion: 指定 Node.js 的版本,像是 4.4.6,詳細支援的版本參考:Elastic Beanstalk Supported Platforms
  • ProxyServer: 指定使用哪一種 Reverse Proxy,有 nginx, apache.

延伸閱讀 (站內)

參考資料


Comments