跳至内容

如何使用非根配置文件的用户配置文件运行 CodeDeploy 代理程序?

3 分钟阅读
0

我想使用非根配置文件的用户配置文件运行 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 代理程序配置文件中更改用户,并授予用户所需的权限

完成以下步骤:

  1. 使用 SSH 连接到您的 EC2 实例

  2. 要停止安装在该实例上的 CodeDeploy 主机代理程序,请运行以下命令:

    sudo service codedeploy-agent stop
  3. 要在 CodeDeploy 代理程序配置文件中更改用户,请运行以下 sed 流编辑器命令:

    sudo sed -i 's/""/"ec2-user"/g' /etc/init.d/codedeploy-agent

    **注意:**请将 ec2-user 替换为您希望 CodeDeploy 主机代理程序在运行时使用的用户名。有关 sed 命令的详细信息,请参阅 GNU 网站上的 Introduction

  4. 要重新加载 systemd 配置,请运行以下命令:

    sudo sed -i 's/#User=codedeploy/User=ec2-user/g' /usr/lib/systemd/system/codedeploy-agent.service
    sudo systemctl daemon-reload
  5. 要向新用户授予对所需目录的权限,请运行以下命令:

    sudo chown ec2-user:ec2-user -R /opt/codedeploy-agent/
    sudo chown ec2-user:ec2-user -R /var/log/aws/

    **注意:**请将 ec2-user 替换为您希望 CodeDeploy 主机代理程序在运行时使用的用户名。

  6. 要重新启动 CodeDeploy 代理程序,请运行以下命令:

    sudo service codedeploy-agent start
  7. 要确认您的配置文件是否已更新,请运行以下命令:

    sudo service codedeploy-agent status

    输出示例:

    The AWS CodeDeploy agent is running as PID ####
  8. 要验证正在运行哪些进程以及正在运行这些进程的用户,请运行以下命令:

    ps aux | grep codedeploy-agent

创建启动配置模板和 AWS Auto Scaling 组,以自动执行用户更改流程

完成以下步骤:

  1. 打开 Amazon EC2 控制台

  2. 在导航窗格中,选择 Launch Configurations(启动配置)。

  3. 选择 Create launch configuration(创建启动配置)。

  4. 选择 Amazon Linux AMI。

  5. 选择 Next: Configure details(下一步:配置详细信息)。

  6. 对于 IAM role(IAM 角色),选择预配置的 AWS Identity and Access Management (IAM) 角色。该角色必须向您的实例授予访问 Amazon Simple Storage Service (Amazon S3) 资源的权限。

  7. 选择 Advanced Details(高级详细信息)。

  8. 在代理配置文件的“用户数据”部分中,输入用于安装 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 主机代理程序在运行时使用的用户名。当启动使用定义的启动配置的新实例时,上述示例代码会自动运行。

  9. 完成启动向导中的其余步骤。然后,选择 Create launch configuration(创建启动配置)。

  10. 选择 Create an AWS Auto Scaling group using this launch configuration(使用此启动配置创建 AWS Auto Scaling 组)。

  11. 对于 Group name(组名称),输入您的自动扩缩组的名称。

  12. 对于 Subnet(子网),输入允许实例访问互联网的子网。

  13. 选择 Next: Configure scaling policies(下一步:配置扩展策略),然后选择策略。

  14. 完成启动向导中的其余步骤,然后选择 Create AWS Auto Scaling group(创建 AWS Auto Scaling 组)。

验证 CodeDeploy 代理程序是否已安装并在新实例上以正确的用户身份运行

首先,确认您的实例是否正在运行。然后,完成以下步骤:

  1. 使用 SSH 连接到您的实例
  2. 要验证 CodeDeploy 代理程序是否正在该实例上运行,请运行以下命令:
    sudo service codedeploy-agent status
    如果命令返回错误,则表明未安装 CodeDeploy 代理程序。安装 CodeDeploy 代理程序
  3. 要验证 CodeDeploy 代理程序是否正在以正确的用户名运行,请运行以下命令:
    ps aux | grep codedeploy-agent
    输出示例:
    The AWS CodeDeploy agent is running as PID ####
AWS 官方已更新 1 年前