bash - 通过用户数据在 EC2 实例上安装 CodeDeploy 代理

标签 bash amazon-web-services amazon-ec2 aws-cloudformation aws-code-deploy

我希望在 EC2 实例上安装 CodeDeploy 代理,并通过 CloudFormation 部署用户数据。 Here is the script I'm using from AWS

但是,当我将脚本放置在 CloudFormation 中 EC2 实例的 UserData 属性中时,我从 linter 中收到此错误:

E1019 Parameter PLAT for Fn::Sub not found at Resources/Server/Properties/UserData/Fn::Base64/Fn::Sub

这是片段:

UserData:
        'Fn::Base64': !Sub |
          #!/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 "Instalation 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 "Instalation 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 "Instalation 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 "Instalation 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

我在这里缺少什么?当使用通过控制台创建实例时,此脚本在用户数据中运行良好。提前致谢!

最佳答案

向@jordanm 的评论致敬。这解决了我的问题。

关于bash - 通过用户数据在 EC2 实例上安装 CodeDeploy 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69931111/

相关文章:

linux - perl 没有正确评估 bash 脚本

bash - bash 脚本应该如何确定 Maven 项目及其依赖项的类路径?

sql - Hibernate + SQL Server 性能变量

javascript - Dialogue.Directive 不起作用 发出处理程序时?

amazon-web-services - 如何识别定义了IAM角色的CloudFormation模板?

amazon-ec2 - Amazon RDS 终端节点内部

amazon-web-services - AWS CLI - JMESPath 查询以按标签查找 vpc-id

linux - 替换 sed 中的控制字符

Bash 查找将多个文件替换为新文件

jenkins - 是否有可能让 Jenkins 更快地从附加云创建工作人员?