LoginSignup
2
1

More than 5 years have passed since last update.

AWS Elastic Beanstalkで、デプロイ時に一つのインスタンスにだけ実行

Last updated at Posted at 2018-01-30

Elastic Beanstalk へのデプロイで、一つのインスタンスにだけ実行するには container_commandsleader_only というフラグを立てれば良いと思われるが、上手く動作しなかった。しかもオートスケール時には動作しないらしい。

ので、ELB に問い合わせて自身のインスタンスIDと比較するという方法を取った。下記のスクリプトを配置すれば可能。

#!/usr/bin/env bash

if aws elb describe-load-balancers --query 'LoadBalancerDescriptions[].Instances' --region ap-northeast-1 --output text \
    | cat -n \
    | grep $(curl http://169.254.169.254/latest/meta-data/instance-id) \
    | awk '{print $1}' \
    | grep -q 3
then
    echo 'HOME=/tmp' | sudo tee /var/spool/cron/webapp
    echo '* * * * * php /var/app/current/artisan schedule:run >> /dev/null 2>&1' | sudo tee -a /var/spool/cron/webapp
fi
  • grep -q 3 は決め打ちの番号なので、環境で調整する必要あり。
  • インスタンスの中で curl http://169.254.169.254/latest/meta-data/instance-id とすると、そのインスタンス自身のIDが取得できる。
  • IAMロールの設定が別途必要。Elastic Beanstalkに振られているIAMロールに AWSElasticBeanstalkFullAccess を付与すれば良い。

参考

2
1
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
2
1