起動テンプレートを使用して、Amazon Linux または Ubuntu を実行している Amazon EC2 インスタンスに CodeDeploy エージェントを自動的にインストールする方法を教えてください。

所要時間2分
0

Linux または Ubuntu を実行している Amazon Elastic Compute Cloud (Amazon EC2) インスタンスに、AWS CodeDeploy エージェントを自動的にインストールする方法を教えてください。

解決方法

起動テンプレートを作成する場合、ユーザーデータフィールドを使用して、インスタンスの起動時に実行する設定スクリプトを追加できます。このシェルスクリプトは、すべての AWS リージョンとサポートされている Amazon Linux および Ubuntu ディストリビューションに CodeDeploy エージェントをインストールします。

注: AUTOUPDATE 変数を true にすることで、起動時に自動更新するよう CodeDeploy を設定することができます。

例えば、Amazon EC2 ユーザーデータフィールドに次の設定スクリプトを追加できます。

重要: 次のスクリプトは Linux 用 EC2 でのみ機能します。Windows 環境では、スクリプトは失敗します。

#!/bin/bash -xe

## Code Deploy Agent Bootstrap Script##


exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
AUTOUPDATE=false

function installdep(){

if [ ${PLAT} = "ubuntu" ]; then

  apt-get -y update
  # Satisfying even ubuntu older versions.
  apt-get -y install jq awscli ruby2.0 || apt-get -y install jq awscli ruby



elif [ ${PLAT} = "amz" ]; then
  yum -y update
  yum install -y aws-cli ruby jq

fi

}

function platformize(){

#Linux OS detection#
 if hash lsb_release; then
   echo "Ubuntu server OS detected"
   export PLAT="ubuntu"


elif hash yum; then
  echo "Amazon Linux detected"
  export PLAT="amz"

 else
   echo "Unsupported release"
   exit 1

 fi
}


function execute(){

if [ ${PLAT} = "ubuntu" ]; then

  cd /tmp/
  wget https://aws-codedeploy-${REGION}.s3.amazonaws.com/latest/install
  chmod +x ./install

  if ./install auto; then
    echo "Installation completed"
      if ! ${AUTOUPDATE}; then
            echo "Disabling Auto Update"
            sed -i '/@reboot/d' /etc/cron.d/codedeploy-agent-update
            chattr +i /etc/cron.d/codedeploy-agent-update
            rm -f /tmp/install
      fi
    exit 0
  else
    echo "Installation script failed, please investigate"
    rm -f /tmp/install
    exit 1
  fi

elif [ ${PLAT} = "amz" ]; then

  cd /tmp/
  wget https://aws-codedeploy-${REGION}.s3.amazonaws.com/latest/install
  chmod +x ./install

    if ./install auto; then
      echo "Installation completed"
        if ! ${AUTOUPDATE}; then
            echo "Disabling auto update"
            sed -i '/@reboot/d' /etc/cron.d/codedeploy-agent-update
            chattr +i /etc/cron.d/codedeploy-agent-update
            rm -f /tmp/install
        fi
      exit 0
    else
      echo "Installation script failed, please investigate"
      rm -f /tmp/install
      exit 1
    fi

else
  echo "Unsupported platform ''${PLAT}''"
fi

}

platformize
installdep
REGION=$(curl -s 169.254.169.254/latest/dynamic/instance-identity/document | jq -r ".region")
execute

関連情報

Auto Scaling グループの起動テンプレートを作成する

AWS公式
AWS公式更新しました 1年前
コメントはありません

関連するコンテンツ