In che modo è possibile lanciare i modelli per installare automaticamente l'agente CodeDeploy su un'istanza Amazon EC2 che esegue Amazon Linux o Ubuntu?

2 minuti di lettura
0

Come faccio a installare automaticamente l'agente AWS CodeDeploy su un'istanza di Amazon Elastic Compute Cloud (Amazon EC2) che esegue Linux o Ubuntu?

Risoluzione

Quando si crea un modello di avvio, è possibile utilizzare il campo Dati utente per aggiungere uno script di configurazione che viene eseguito all'avvio dell'istanza. Questo script di shell installa l'agente CodeDeploy per tutte le regioni AWS e supporta le distribuzioni Amazon Linux e Ubuntu.

Nota: è possibile configurare CodeDeploy per l'aggiornamento automatico all'avvio impostando la variabile AUTOUPDATE su true.

Ad esempio, puoi aggiungere il seguente script di configurazione nel campo Dati utente di Amazon EC2:

Importante: il seguente script funziona solo in EC2 per Linux. In ambienti Windows, lo script fallisce.

#!/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

Informazioni correlate

Creazione di un modello di avvio per un gruppo con scalabilità automatica

AWS UFFICIALE
AWS UFFICIALEAggiornata un anno fa