📝

Elastic Beanstalk のサンプルアプリを EB CLI からデプロイしてみた

2025/01/15に公開

Elastic Beanstalk 環境に Apache の VirtualHost を構築してみた
新しく発行したアカウントでは上記手順の Cloud9 を使用できなかったため、CloudShell に EB CLI をインストールしてデプロイしてみました。

https://youtu.be/XTEMJLeDM2g

1. EB CLI のインストール

[小ネタ] EB CLI を簡単に使う方法 | DevelopersIO
上記ブログと同様のコマンドでインストールします。

1-1. リポジトリのクローン

$ git clone https://github.com/aws/aws-elastic-beanstalk-cli-setup.git
Cloning into 'aws-elastic-beanstalk-cli-setup'...
remote: Enumerating objects: 325, done.
remote: Counting objects: 100% (30/30), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 325 (delta 9), reused 22 (delta 6), pack-reused 295 (from 1)
Receiving objects: 100% (325/325), 533.74 KiB | 8.90 MiB/s, done.
Resolving deltas: 100% (172/172), done.

1-2. インストールスクリプトの実行

$ python ./aws-elastic-beanstalk-cli-setup/scripts/ebcli_installer.py

***********************************
1. Locating virtualenv installation
***********************************
ERROR: Could not find and "virtualenv" installed. Ensurevirtualenv is installed and that it is in PATH before executingthis script.

******************************************
2. Creating exclusive virtualenv for EBCLI
******************************************
/bin/sh: line 1: virtualenv: command not found

1-3. パスの追加

$ echo 'export PATH="/home/ec2-user/.ebcli-virtual-env/executables:$PATH"' >> ~/.bash_profile && source ~/.bash_profile

1-4. インストールチェック

$ eb --version
EB CLI 3.21.0 (Python 3.9.20 (main, Dec 11 2024, 00:00:00) 
[GCC 11.4.1 20230605 (Red Hat 11.4.1-2)])

2. サンプルアプリの取得

2-1. ディレクトリ作成

$ mkdir nodejs

2-2. ディレクトリ移動

$ cd nodejs

2-3. サンプルアプリ取得
サンプルアプリは以下のドキュメントのリンクから取得します。
Tutorials and samples - AWS Elastic Beanstalk

$ wget https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/samples/nodejs.zip
--2025-01-14 02:54:06--  https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/samples/nodejs.zip
Resolving docs.aws.amazon.com (docs.aws.amazon.com)... 3.165.39.74, 3.165.39.122, 3.165.39.102, ...
Connecting to docs.aws.amazon.com (docs.aws.amazon.com)|3.165.39.74|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2633 (2.6K) [application/zip]
Saving to: ‘nodejs.zip’

nodejs.zip                                     100%[==================================================================================================>]   2.57K  --.-KB/s    in 0s      

2025-01-14 02:54:06 (73.4 MB/s) - ‘nodejs.zip’ saved [2633/2633]

2-4. zip ファイル解凍

$ unzip nodejs.zip
Archive:  nodejs.zip
   creating: .ebextensions/
  inflating: .ebextensions/logging.config  
  inflating: app.js                  
  inflating: cron.yaml               
  inflating: index.html              
  inflating: package.json          

2-5. zip ファイル削除

$ rm nodejs.zip

3. config ファイルの作成

サンプルアプリをそのままデプロイしたところ、以下のエラーが発生しました。

Creating Auto Scaling launch configuration failed Reason: Resource handler returned message: "The Launch Configuration creation operation is not available in your account. Use launch templates to create configuration templates for your Auto Scaling groups.

原因は以下のドキュメントに記載の通り、現状では起動設定がサポートされていないためです。

Launch Templates - AWS Elastic Beanstalk

Starting on October 1, 2024, the Amazon EC2 Auto Scaling service will no longer support the creation of launch configurations for new accounts.

対応方法についても上記ドキュメントに記載の通り以下の 4 つとなります。

  • RootVolumeTypegp3 に設定する
  • BlockDeviceMappingsgp3 が含まれるように設定する
  • DisableIMDSv1true に設定する
  • EnableSpottrue に設定する

今回は以下の GitHub Issue で紹介されている「RootVolumeTypegp3 に設定する」方法を採用しました。

Issue #530 · aws/aws-elastic-beanstalk-cli

3-1. config ファイルの作成

$ nano .ebextensions/my_environment.config

# 以下の内容でファイルを作成
option_settings:
  - namespace: "aws:autoscaling:launchconfiguration"
    option_name: "RootVolumeType"
    value: "gp3"

3-2. config ファイルの確認

$ cat .ebextensions/my_environment.config
option_settings:
  - namespace: "aws:autoscaling:launchconfiguration"
    option_name: "RootVolumeType"
    value: "gp3"

4. Elastic Beanstalk 環境の初期設定

eb init - AWS Elastic Beanstalk

4-1. 環境の初期設定

$ eb init -i

# リージョン選択
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) cn-northwest-1 : China (Ningxia)
14) us-east-2 : US East (Ohio)
15) ca-central-1 : Canada (Central)
16) eu-west-2 : EU (London)
17) eu-west-3 : EU (Paris)
18) eu-north-1 : EU (Stockholm)
19) eu-south-1 : EU (Milano)
20) ap-east-1 : Asia Pacific (Hong Kong)
21) me-south-1 : Middle East (Bahrain)
22) af-south-1 : Africa (Cape Town)
23) ap-southeast-3 : Asia Pacific (Jakarta)
24) ap-northeast-3 : Asia Pacific (Osaka)
(default is 3): 9

# アプリケーション名設定
Enter Application Name
(default is "nodejs"): 
Application nodejs has been created.

# プラットフォームの確認
It appears you are using Node.js. Is this correct?
(Y/n):
Select a platform branch.
1) Node.js 22 running on 64bit Amazon Linux 2023
2) Node.js 20 running on 64bit Amazon Linux 2023
3) Node.js 18 running on 64bit Amazon Linux 2023
4) Node.js 18 running on 64bit Amazon Linux 2
(default is 1):

# SSH キーペアの設定
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): 

Select a keypair.
1) aws-eb
2) [ Create new KeyPair ]
(default is 1):

5. Elastic Beanstalk 環境の作成

eb create - AWS Elastic Beanstalk

5-1. 環境の作成

$ eb create --single

# 環境名の設定
Enter Environment Name
(default is nodejs-dev): 

# CNAME の設定
Enter DNS CNAME prefix
(default is nodejs-dev2): 

# スポットフリートリクエストの設定
Would you like to enable Spot Fleet requests for this environment? (y/N):

# 環境作成状況
Updated: 2025-01-14 03:10:15.065000+00:00
Printing Status:
2025-01-14 03:10:14    INFO    createEnvironment is starting.
2025-01-14 03:10:15    INFO    Using elasticbeanstalk-ap-northeast-1-050752614541 as Amazon S3 storage bucket for environment data.
2025-01-14 03:10:38    INFO    Created security group named: awseb-e-g39npvgwgg-stack-AWSEBSecurityGroup-go96ggQgWM83
2025-01-14 03:10:53    INFO    Created EIP: 13.230.60.145
2025-01-14 03:11:09    INFO    Waiting for EC2 instances to launch. This may take a few minutes.
2025-01-14 03:12:00    INFO    Instance deployment completed successfully.
2025-01-14 03:12:17    INFO    Application available at nodejs-dev2.ap-northeast-1.elasticbeanstalk.com.
2025-01-14 03:12:18    INFO    Successfully launched environment: nodejs-dev

5-2. コンソールからの確認

ドメインにアクセスするとサンプルアプリが表示されます。

EB CLI からのデプロイができました。

6. 環境の削除

eb terminate - AWS Elastic Beanstalk

6-1. 環境を削除します。

& eb terminate --all
The application "nodejs" and all its resources will be deleted.
This application currently has the following:
Running environments: 1
Configuration templates: 0
Application versions: 1

To confirm, type the application name: nodejs

Removing application versions from s3.
2025-01-14 03:16:43    INFO    deleteApplication is starting.
2025-01-14 03:16:43    INFO    Validating environment before performing delete
2025-01-14 03:16:43    INFO    Environment validation complete
2025-01-14 03:16:44    INFO    Invoking Environment Termination workflows.
2025-01-14 03:19:16    INFO    The environment termination step is done.
2025-01-14 03:19:17    INFO    The application has been deleted successfully.

まとめ

今回は Elastic Beanstalk のサンプルアプリを CloudShell にインストールした EB CLI からデプロイしてみました。
どなたかの参考になれば幸いです。

参考資料

Discussion