BeanstalkでのパッケージのバージョンがAMIでのバージョンと異なる原因

aws

User-Dataとは

EC2インスタンス起動時に、シェルスクリプトを走らせたりcloud-initディレクティブを適用できる機能。 コンソールではインスタンスの詳細の設定の、高度な詳細のところから設定できる。

BeanstalkでのUser-Data

実はBeanstalkでも使われていて、CloudFormationで設定されている。

"    /bin/bash /tmp/ebbootstrap.sh ",

...

"Fn::FindInMap": [
    "AWSEBOptions",
    "options",
    "UserDataScript"
]
" > /tmp/ebbootstrap.sh ",

...

"AWSEBOptions": {
    "options": {
        "UserDataScript": "https://s3-ap-northeast-1.amazonaws.com/elasticbeanstalk-env-resources-ap-northeast-1/stalks/eb_node_js_4.0.1.90.2/lib/UserDataScript.sh",
        "guid": "f08557fc43ac",
    }
}

このshellの中では、時計を同期させたり、awsebユーザーを作成したりするほかに、 User-Dataで渡しているguidとAMI作成時に焼かれたguidが一致していない(is_baked=false)場合yum updateが走るようになっている。 そのためAMIを変更するとAMIでのバージョンとBeanstalkで立ち上がったときのバージョンが異なることがあって、固定するにはUser-Dataのguidを併せて変更する必要がある。

GUID=$7

function update_yum_packages
{
  if is_baked update_yum_packages_$GUID; then
    log yum update has already been done.
  else
    log Updating yum packages.
    yum --exclude=aws-cfn-bootstrap update -y || echo Warning: cannot update yum packages. Continue...
    mark_installed update_yum_packages_$GUID

    # Update system-release RPM package will reset the .repo files
    # Update the mirror list again after yum update
    update_mirror_list

    log Completed updating yum packages. 
  fi
}

function is_baked
{
	if [[ -f /etc/elasticbeanstalk/baking_manifest/$1 ]]; then
    true
	else
    false
	fi
}

function mark_installed
{
    mkdir -p /etc/elasticbeanstalk/baking_manifest/
    echo `date -u` > /etc/elasticbeanstalk/baking_manifest/$1-manifest
}

is_baked=trueでのログ。

$ cat /var/log/messages | grep yum
[eb-cfn-init]: yum repo has already been locked to f08557fc43ac.
[eb-cfn-init]: yum update has already been done.

is_baked=falseでのログ。

$ cat /var/log/messages | grep yum
[eb-cfn-init]: Completed yum repo version locking.
[eb-cfn-init]: Updating yum packages.
yum[1597]: Updated: *****
...