LoginSignup
33
36

More than 5 years have passed since last update.

Elastic Beanstalk 環境カスタマイズメモ

Last updated at Posted at 2014-09-30

概要

Elastic Beanstalk 使ってますか?
アプリケーションのデプロイがとても簡便になるため、オススメです。

最近では Docker にも対応し、Immutable Infrastructure とも相性が良いです。

本稿では Docker on Elastic Beanstalk で作成した環境をカスタマイズする方法について述べます。

前提

環境のカスタマイズ① - オートスケーリング等インフラ全体の設定

カスタマイズ方法

.elasticbeanstalk 配下に生成される optionsettings.yourapp-env ファイルを変更します。

設定可能なパラメータはこちらから: オプションの値 - AWS Elastic Beanstalk

例: autoscaling の MaxSize/MinSize を変更

.elasticbeanstalk/optionsettings.yourapp-env
[aws:autoscaling:asg]
MaxSize=4
MinSize=1

例: ELB の subnet を変更

.elasticbeanstalk/optionsettings.yourapp-env
[aws:ec2:vpc]
VPCId=vpc-xxxxxxxx
ELBScheme=public
ELBSubnets=subnet-xxxxxxxx

反映方法

eb update を実行することで反映されます。

$ eb update
Warning. File "config" has incorrect access permission and can be accessed by other users.
Update environment? [y/n]: y
Updating environment "yourapp-env". This may take a few minutes.
2014-09-30 14:31:27 INFO    Updating environment yourapp-env's configuration settings.
2014-09-30 14:32:10 INFO    Successfully deployed new configuration to environment.
2014-09-30 14:32:10 INFO    Environment update completed successfully.
Update of environment "yourapp-env" has completed.

スクリーンショット 2014-09-30 14.38.08.png

環境のカスタマイズ② - ミドルウェアのインストール等、インスタンス単位の設定

サーバに共通した設定やミドルウェアをインストールしたい場合があるかと思います。
わざわざ IMAGE を作成せずとも、設定ファイルを作成するだけで対応することが可能です。

カスタマイズ方法

.ebextensions 配下に *.config というファイルを配置することで、環境のカスタマイズが行われます。
これらはファイル名の昇順で実行されます。

参考: AWS Elastic Beanstalk 環境のカスタマイズと設定 - AWS Elastic Beanstalk

例: swap 領域の確保

デフォルトでは t2.micro インスタンスには swap 領域がありません。
すぐに OOM エラーとなってしまうので作ってあげます。

.ebextensions/00_add_swap.config
commands:
  01_enable_sudo:
    command: "echo Defaults:root !requiretty >> /etc/sudoers"
    test: "test ! -e /home/ec2-user/swapdone"
  02_dd:
    command: "sudo dd if=/dev/zero of=/swapfile bs=1M count=1024"
    test: "test ! -e /home/ec2-user/swapdone"
  03_mkswap:
    command: "sudo mkswap /swapfile"
    test: "test ! -e /home/ec2-user/swapdone"
  04_swapon:
    command: "sudo swapon /swapfile"
    test: "test ! -e /home/ec2-user/swapdone"
  05_swapdone:
    command: "touch /home/ec2-user/swapdone"
    test: "test ! -e /home/ec2-user/swapdone"

参考: Amazon EC2 t1.micro インスタンスでswapを使用する - blogですかい

例: NewRelic のインストール

サーバ監視のため NewRelic エージェントを導入します。

.ebextensions/01_newrelic.config
packages:
  yum:
    newrelic-sysmond: []
  rpm:
    newrelic: http://download.newrelic.com/pub/newrelic/el5/i386/newrelic-repo-5-3.noarch.rpm
commands:
  01_configure_new_relic:
    command: nrsysmond-config --set license_key=YOUR-LICENSE-KEY
  02_start_new_relic:
    command: /etc/init.d/newrelic-sysmond start

例: Shellshock 対応

2014-09-30 現在、EB 上のインスタンスが参照するリポジトリには最新版の bash が置かれていません。
手動で bash の rpm を追加し、インストールしてあげます。

.ebextensions/02_update_bash.config
packages:
  yum:
    bash: []
  rpm:
    bash: http://packages.us-east-1.amazonaws.com/2014.03/updates/e10f5b547e18/x86_64/Packages/bash-4.1.2-15.19.amzn1.x86_64.rpm

参考: ElasticBeanstalk - Shellshock 対応 @ Elastic Beanstalk - Qiita

反映方法

git aws.push を実行することで反映されます。

$ git aws.push
Updating the AWS Elastic Beanstalk environment yknot-staging...
Environment update initiated successfully.

スクリーンショット 2014-09-30 14.27.01.png

参考

33
36
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
33
36