Knowledge Center Monthly Newsletter - March 2025
Stay up to date with the latest from the Knowledge Center. See all new and updated Knowledge Center articles published in the last month and re:Post’s top contributors.
Como faço para usar as configurações de inicialização para instalar automaticamente o agente CodeDeploy em uma instância do EC2 executando Amazon Linux ou Ubuntu?
Quero instalar automaticamente o agente AWS CodeDeploy em uma instância do Amazon Elastic Compute Cloud (Amazon EC2) executando Linux ou Ubuntu.
Solução
Ao Criar um modelo de execução, use o campo Dados do usuário para adicionar um script de configuração que é executado quando a instância é iniciada. O shell script instala o agente CodeDeploy em todas as regiões da AWS e nas distribuições Amazon Linux e Ubuntu compatíveis.
Para configurar o CodeDeploy para atualização automática na inicialização, defina a variável AUTOUPDATE como true. Por exemplo, adicione o seguinte script de configuração no campo Dados do usuário do Amazon EC2 com base na versão de metadados da sua instância.
**Importante:**O script a seguir funciona somente no EC2 para Linux. Em ambientes Windows, o script falha.
IMSDv1:
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 #!/bin/bash -xe ## CodeDeploy Agent Bootstrap Script## exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1AUTOUPDATE=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
IMSDv2:
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 #!/bin/bash -xe ## CodeDeploy 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 TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` REGION=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r ".region") execute
Informações relacionadas
Crie um modelo de inicialização para o grupo do Auto Scaling

Conteúdo relevante
- feita há 3 meseslg...
- feita há 10 diaslg...
- feita há 11 diaslg...
- feita há 3 meseslg...
- AWS OFICIALAtualizada há 2 meses
- AWS OFICIALAtualizada há um mês
- AWS OFICIALAtualizada há um ano