无法在具有Amazon Linux 2的Lightsail实例中安装CodeDeploy 1 CodeDeploy代理 2实例角色 3部署组 4部署 5结果

问题描述

由于对only being able to use Amazon Linux并不特别满意(也希望使用Amazon Linux 2),所以使用两个OS版本并添加相同的脚本创建了两个实例

mkdir /etc/codedeploy-agent/

mkdir /etc/codedeploy-agent/conf

cat <<EOT >> /etc/codedeploy-agent/conf/codedeploy.onpremises.yml

---

aws_access_key_id: ACCESS

aws_secret_access_key: SECRET

iam_user_arn: arn:aws:iam::525221857828:user/GeneralUser

region: eu-west-2

EOT

wget https://aws-codedeploy-us-west-2.s3.us-west-2.amazonaws.com/latest/install

chmod +x ./install

sudo ./install auto

我注意到的两者之间的区别在于,在具有Linux 2的实例中,文件/etc/codedeploy-agent/conf/仅具有一个文件

Amazon Lightsail Linux 2

并且在Linux中有两个文件

Amazon Lightsail Linux

意识到这一点,我在Linux 2实例中创建了一个同名的新文件

touch codedeployagent.yml

,将其权限更改为

-rw-r--r-- 1 root root 261 Oct  2 10:43 codedeployagent.yml

-rwxr-xr-x 1 root root 261 Oct  2 10:43 codedeployagent.yml

File permissions

,并添加了相同的内容

:log_aws_wire: false
:log_dir: '/var/log/aws/codedeploy-agent/'
:pid_dir: '/opt/codedeploy-agent/state/.pid/'
:program_name: codedeploy-agent
:root_dir: '/opt/codedeploy-agent/deployment-root'
:verbose: false
:wait_between_runs: 1
:proxy_uri:
:max_revisions: 5

codedeployagent.yml content

,然后重新启动计算机。不过,这并不能像我跑步时那样解决问题

sudo service codedeploy-agent status

仍然会得到

重定向到/ bin / systemctl状态代码deploy-agent.service单元 找不到codedeploy-agent.service。

Error remains

还要确保所有更新均已到位,重新启动了计算机,但那也不起作用。

Error remains even though updates are in place

解决方法

我可以提供有关要部署CodeDeployGitHubDemo(基于过去的question)的Amazon Linux 2实例的设置的详细信息。

1。 CodeDeploy代理

将以下内容用作UserData(如果不是us-east-1,则可能需要调整区域):

#!/bin/bash

yum update -y
yum install -y ruby wget

cd /home/ec2-user

wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install

chmod +x ./install
./install auto

它不需要硬编码凭据。在我使用的Amazon Linux 2实例上,以下代码可以很好地工作。

2。实例角色

您的实例需要一个适合CodeDeploy的角色。我使用了EC2实例角色,其策略列出为here

{
    "Version": "2012-10-17","Statement": [
        {
            "Action": [
                "s3:Get*","s3:List*"
            ],"Effect": "Allow","Resource": "*"
        }
    ]
}

3。部署组

我在AutoScaling组中有三个用于测试的实例,称为myasg

enter image description here

4。部署

我是从没有负载均衡器的S3部署的:

enter image description here

5。结果

未发现问题,并且部署成功:

enter image description here

网站正在运行(需要在安全组中打开端口80):

enter image description here

更新

对于在Amazon Linux 2上手动安装。您可以sudo su -登录后成为root用户。

mkdir -p /etc/codedeploy-agent/conf

cat <<EOT >> /etc/codedeploy-agent/conf/codedeploy.onpremises.yml
---

aws_access_key_id: ACCESS

aws_secret_access_key: SECRET

iam_user_arn: arn:aws:iam::525221857828:user/GeneralUser

region: eu-west-2

EOT

yum install -y wget ruby

wget https://aws-codedeploy-us-west-2.s3.us-west-2.amazonaws.com/latest/install

chmod +x ./install

env AWS_REGION=eu-west-2 ./install rpm

要检查其状态:

systemctl status codedeploy-agent

有了这个,你应该得到这样的东西

● codedeploy-agent.service - AWS CodeDeploy Host Agent
   Loaded: loaded (/usr/lib/systemd/system/codedeploy-agent.service; enabled; vendor prese
t: disabled)
   Active: active (running) since Sat 2020-10-03 07:18:57 UTC; 3s ago
  Process: 3609 ExecStart=/bin/bash -a -c [ -f /etc/profile ] && source /etc/profile; /opt
/codedeploy-agent/bin/codedeploy-agent start (code=exited,status=0/SUCCESS)
 Main PID: 3623 (ruby)
   CGroup: /system.slice/codedeploy-agent.service
           ├─3623 codedeploy-agent: master 3623
           └─3627 codedeploy-agent: InstanceAgent::Plugins::CodeDeployPlugin::CommandPo...

Oct 03 07:18:57 ip-172-26-8-137.eu-west-2.compute.internal systemd[1]: Starting AWS Cod...
Oct 03 07:18:57 ip-172-26-8-137.eu-west-2.compute.internal systemd[1]: Started AWS Code...
Hint: Some lines were ellipsized,use -l to show in full.

如果您运行

sudo service codedeploy-agent status

您会得到(意味着它正在按预期工作)

The AWS CodeDeploy agent is running as PID 3623

It works!!

要在不运行时开始:

systemctl start codedeploy-agent