시작 템플릿을 사용하여 Amazon Linux 또는 Ubuntu를 실행하는 Amazon EC2 인스턴스에 CodeDeploy 에이전트를 자동으로 설치하려면 어떻게 해야 합니까?

2분 분량
0

Linux 또는 Ubuntu를 실행하는 Amazon Elastic Compute Cloud(Amazon EC2) 인스턴스에 AWS CodeDeploy 에이전트를 자동으로 설치하려면 어떻게 해야 하나요?

해결 방법

시작 템플릿을 생성할 때 User data 필드를 사용하여 인스턴스가 시작될 때 실행되는 구성 스크립트를 추가할 수 있습니다. 이 셸 스크립트는 모든 AWS 리전 및 지원되는 Amazon Linux 및 Ubuntu 배포에 대해 CodeDeploy 에이전트를 설치합니다.

참고: AUTOUPDATE 변수를 true로 설정하여 부팅 시 자동 업데이트에 CodeDeploy를 구성할 수 있습니다.

예를 들어 Amazon EC2 사용자 데이터(User data) 필드에 다음 구성 스크립트를 추가할 수 있습니다.

중요: 다음 스크립트는 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 공식업데이트됨 일 년 전