Ongoing service disruptions
For the most recent update on ongoing service disruptions affecting the AWS Middle East (UAE) Region (ME-CENTRAL-1), refer to the AWS Health Dashboard. For information on AWS Service migration, see How do I migrate my services to another region?
如何使用非根配置文件的用户配置文件运行 CodeDeploy 代理程序?
我想使用非根配置文件的用户配置文件运行 AWS CodeDeploy 代理程序。
解决方法
以下解决方法与 CodeDeploy 代理程序支持的所有基于 RPM 和 Debian 的 Linux 发行版兼容。要使用 Amazon Linux 亚马逊机器映像 (AMI) 启动 Amazon Elastic Compute Cloud (Amazon EC2) 实例,必须使用 Amazon Linux 2023 (AL2023) 或 Amazon Linux 2 (AL2)。
**先决条件:**在您的 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 网站上的 Introduction。
-
要重新加载 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(高级详细信息)。
-
在代理配置文件的“用户数据”部分中,输入用于安装 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(组名称),输入您的自动扩缩组的名称。
-
对于 Subnet(子网),输入允许实例访问互联网的子网。
-
选择 Next: Configure scaling policies(下一步:配置扩展策略),然后选择策略。
-
完成启动向导中的其余步骤,然后选择 Create AWS Auto Scaling group(创建 AWS Auto Scaling 组)。
验证 CodeDeploy 代理程序是否已安装并在新实例上以正确的用户身份运行
首先,确认您的实例是否正在运行。然后,完成以下步骤:
- 使用 SSH 连接到您的实例。
- 要验证 CodeDeploy 代理程序是否正在该实例上运行,请运行以下命令:
如果命令返回错误,则表明未安装 CodeDeploy 代理程序。安装 CodeDeploy 代理程序。sudo service codedeploy-agent status - 要验证 CodeDeploy 代理程序是否正在以正确的用户名运行,请运行以下命令:
输出示例:ps aux | grep codedeploy-agentThe AWS CodeDeploy agent is running as PID ####

