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.
如何使用非根設定檔的使用者設定檔執行 CodeDeploy 代理程式?
我想要使用非根設定檔的使用者設定檔來執行 AWS CodeDeploy 代理程式。
解決方法
Amazon Linux 2 和 Amazon Linux 2023 取代了 Amazon Linux 1 Amazon Machine Image (AMI)。您無法再將 Amazon Linux 1 AMI 以 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體啟動。以下解決方案與 CodeDeploy 代理程式的所有支援 RPM 和 Debian 型 Linux 發行版相容。
先決條件: 在您的 EC2 執行個體上安裝 CodeDeploy 代理程式,或驗證 CodeDeploy 代理程式是否正在執行個體上執行。
變更 CodeDeploy 代理程式組態檔中的使用者,並授予使用者必要的權限
請完成下列步驟:
-
若要停止執行個體上安裝的 CodeDeploy 主機代理程式,請執行下列命令:
sudo service codedeploy-agent stop
-
若要變更 CodeDeploy 代理程式設定檔中的使用者,請執行以下 sed 串流編輯器命令:
sudo sed -i 's/""/"ec2-user"/g' /etc/init.d/codedeploy-agent
注意: 將 ec2-user 替換為您希望 CodeDeploy 主機代理程式執行的使用者名稱。有關 sed 命令的更多信息,請參閱 GNU 網站上的簡介。
-
若要重新載入 systemd 組態,請執行以下命令:
sudo sed -i 's/#User=codedeploy/User=ec2-user/g' /usr/lib/systemd/system/codedeploy-agent.service sudo systemctl daemon-reload
-
若要授予新使用者所需目錄的權限,請執行下列命令:
sudo chown ec2-user:ec2-user -R /opt/codedeploy-agent/ sudo chown ec2-user:ec2-user -R /var/log/aws/
注意: 將 ec2-user 替換為您希望 CodeDeploy 主機代理程式執行的使用者名稱。
-
若要重新啟動 CodeDeploy 代理程式,請執行下列命令:
sudo service codedeploy-agent start
-
若要確認您的設定檔是否已更新,請執行以下命令:
sudo service codedeploy-agent status
輸出範例:
The AWS CodeDeploy agent is running as PID ####
-
若要確認正在執行的程序以及執行這些程序的使用者,請執行以下命令:
ps aux | grep codedeploy-agent
建立啟動組態範本和 AWS Auto Scaling 群組,以自動化使用者變更程序
請完成下列步驟:
-
開啟 Amazon EC2 主控台。
-
在導覽窗格中,選擇 Launch Configurations (啟動組態)。
-
選擇 Create launch configuration (建立啟動組態)。
-
選取 Amazon Linux AMI。
-
選擇 Next: Configure details (下一步:設定詳細資訊)。
-
在 IAM role (IAM 角色) 中,選擇預先設定的 AWS Identity and Access Management (IAM) 角色。該角色必須授予您的執行個體存取 Amazon Simple Storage Service (Amazon S3) 資源的權限。
-
選擇 Advanced Details (進階詳細資訊)。
-
在代理程式組態檔的 User data (使用者資料) 區段中,輸入 install the CodeDeploy agent (安裝 CodeDeploy 代理程式) 的命令。然後,更新檔案以使用特定使用者。
對於 IMDSv1,請使用下列指令碼:#!/bin/bash REGION=$(curl 169.254.169.254/latest/meta-data/placement/availability-zone/ | sed 's/[a-z]$//') yum -y update yum install ruby wget -y cd /home/ec2-user wget https://aws-codedeploy-$REGION.s3.amazonaws.com/latest/install chmod +x ./install ./install auto service codedeploy-agent stop #adduser username <--- this is only required if you use a username that does not already exist sed -i 's/""/"ec2-user"/g' /etc/init.d/codedeploy-agent sed -i 's/#User=codedeploy/User=ec2-user/g' /usr/lib/systemd/system/codedeploy-agent.service systemctl daemon-reload chown ec2-user:ec2-user -R /opt/codedeploy-agent/ chown ec2-user:ec2-user -R /var/log/aws/ service codedeploy-agent start
對於 IMDSv2,請使用下列指令碼:
#!/bin/bash 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" http://169.254.169.254/latest/meta-data/placement/availability-zone/ | sed 's/[a-z]$//') yum -y update yum install ruby wget -y cd /home/ec2-user wget https://aws-codedeploy-$REGION.s3.amazonaws.com/latest/install chmod +x ./install ./install auto service codedeploy-agent stop #adduser username <--- this is only required if you use a username that does not already exist sed -i 's/""/"ec2-user"/g' /etc/init.d/codedeploy-agent sed -i 's/#User=codedeploy/User=ec2-user/g' /usr/lib/systemd/system/codedeploy-agent.service systemctl daemon-reload chown ec2-user:ec2-user -R /opt/codedeploy-agent/ chown ec2-user:ec2-user -R /var/log/aws/ service codedeploy-agent start
注意: 將 ec2-user 替換為您希望 CodeDeploy 主機代理程式執行的使用者名稱。當您啟動使用定義啟動組態的新執行個體時,上述範例程式碼會自動執行。
-
完成啟動精靈中的剩餘步驟。然後,選擇 Create launch configuration (建立啟動組態)。
-
選擇 Create an AWS Auto Scaling group using this launch configuration (使用此啟動組態建立 AWS Auto Scaling 群組)。
-
在 Group name (群組名稱) 中,輸入 Auto Scaling 群組的名稱。
-
在 Subnet (子網路),輸入允許執行個體存取網際網路的子網路。
-
選擇 Next: Configure scaling policies (下一步:設定擴展政策),然後選擇一個政策。
-
完成啟動精靈中的其餘步驟,然後選擇建立 AWS Auto Scaling 群組。
確認 CodeDeploy 代理程式是否已安裝,並且在新執行個體上使用正確的使用者執行
首先,確認您的執行個體正在執行。然後,完成下列步驟:
- 使用 SSH 連線到執行個體。
- 若要確認 CodeDeploy 代理程式是否正在執行個體上執行,請執行下列命令:
如果該命令傳回錯誤,則表示您沒有安裝 CodeDeploy 代理程式。安裝 CodeDeploy 代理程式。sudo service codedeploy-agent status
- 若要確認 CodeDeploy 代理程式是否使用正確的使用者名稱執行,請執行下列命令:
輸出範例:ps aux | grep codedeploy-agent
The AWS CodeDeploy agent is running as PID ####
相關內容
- 已提問 4 個月前lg...
- 已提問 4 個月前lg...
- 已提問 3 個月前lg...
- 已提問 1 年前lg...
- 已提問 7 個月前lg...
- AWS 官方已更新 5 個月前
- AWS 官方已更新 2 年前
- AWS 官方已更新 3 年前